`
方块石
  • 浏览: 59965 次
  • 来自: ...
社区版块
存档分类
最新评论
阅读更多

package test;

import java.io.File;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;

public class Excel {

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
//  WriteExcel excel = new WriteExcel("");
  System.out.println(System.getProperty("user.dir"));
 
//   ReadXML doc = ReadXML.getInstance(System.getProperty("user.dir")+"//src//test//test_zh.xml");
//  ReadXML doc = new ReadXML();
  ReadXML doc1 = ReadXML.xml;
  doc1.getInstance(System.getProperty("user.dir"));
   List list = doc1.getID();
   
   WritableWorkbook book=
    Workbook.createWorkbook(new File("D:\\MyEclipse 6.0\\workspace\\json\\src\\test\\test.xls"));
//    book.
    //添加一个工作表
    WritableSheet sheet=book.createSheet("第一页",1);
    
    for( int i = 0; i< list.size(); i++)
    {
     List tagList = (List)list.get(0);
     List zhList = (List)list.get(1);
     sheet.addCell(new Label(0,0,"词条"));
     sheet.addCell(new Label(1,0,"中文"));
     for(int j = 0; j< tagList.size(); j++)
     {
      String str = (String)tagList.get(j);
      sheet.addCell(new Label(0,j,str));
//      sheet.addCell(new Label(1,0,"中文"));
     }
     
     for(int k = 0; k< zhList.size(); k++)
     {
      String str1 = (String)zhList.get(k);
      sheet.addCell(new Label(1,k,str1));
//      sheet.addCell(new Label(1,0,"中文"));
     }
    }
    
    book.write();
    book.close();   
  
 }

 
}
++++++++++++++++++++++++++++++++++++++++++++

 

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ReadXML {

 static Document doc;
 static ReadXML xml;
 
 
 public static ReadXML getReadXML()
 {
  xml = new ReadXML();
  return xml;
 }
 public static void getInstance(String path) throws Exception
 {
  File file = new File(path);
  if(file.isDirectory())
  {
   File[] fileList = file.listFiles();
   for(int i = 0 ; i < fileList.length ; i++)
   {
    File nodeFile = fileList[i];
    System.out.println(nodeFile);
    getInstance("00="+nodeFile.toString());
   }
   
   
  }
  else
  {
   String fileName = file.getName();
//   System.out.println(file.getPath());
   if(fileName.indexOf("zh") > 0)
   {
    getbuild(fileName);
   }
  }
//  return xml;
 }
 
 public static List getbuild(String path) throws Exception
 {
//   xml = new ReadXML();
//  InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
  
  
   Reader isr = new InputStreamReader(new FileInputStream(path),"UTF-8");
  SAXBuilder build = new SAXBuilder();
   doc = build.build(isr);
   isr.close();
   List tagList = new ArrayList();
   List zhList = new ArrayList();
   List enList = new ArrayList();
   List list = new ArrayList();
   Element root = doc.getRootElement();
   List<Element> elementlist  = root.getChildren();
   for(int i = 0; i < elementlist.size(); i++)
   {
    Element child = elementlist.get(i);
    Attribute id = child.getAttribute("id");
    String idCon = id.getValue();
    String con = child.getValue();
    tagList.add(idCon);
    zhList.add(con);
    System.out.println(child.getValue());
//    System.out.println(child.getAttributeValue("id"));
    System.out.println(idCon);
   }
   list.add(tagList);
   list.add(zhList);
   return list;
//  return xml;
 }
 
 public static List  getID()
 {
  List tagList = new ArrayList();
  List zhList = new ArrayList();
  List enList = new ArrayList();
  List list = new ArrayList();
  Element root = doc.getRootElement();
  List<Element> elementlist  = root.getChildren();
  for(int i = 0; i < elementlist.size(); i++)
  {
   Element child = elementlist.get(i);
   Attribute id = child.getAttribute("id");
   String idCon = id.getValue();
   String con = child.getValue();
   tagList.add(idCon);
   zhList.add(con);
   System.out.println(child.getValue());
//   System.out.println(child.getAttributeValue("id"));
   System.out.println(idCon);
  }
  list.add(tagList);
  list.add(zhList);
  return list;
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics