将Excel文件生成xml文件

一.下载jxl.jar(用来读取excel文件)和jdom(用来生成xml文件)

二.准备需要转换的数据,如图

 

excel中的数据

三.代码部分

1.先写个类来处理如何生成xml

 

  
  
  
  
  1. import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.util.ArrayList;  
  4. import org.jdom.*;  
  5. import org.jdom.output.XMLOutputter;  
  6.  
  7. public class GenEduXml {  
  8.     Document myDoc;  
  9.     Element myRoot;  
  10.     Element subRoot;  
  11.       
  12.     public void init() throws IOException, JDOMException{  
  13.         myDoc = new Document();         //创建document  
  14.         Attribute att1 = new Attribute("grade","3");          
  15.         Attribute att2 = new Attribute("version","高级");  
  16.         ArrayList<Attribute> list = new ArrayList<Attribute>();  
  17.         list.add(att1);  
  18.         list.add(att2);  
  19.         myRoot = new Element("subChannel").setAttribute("id","英语").  
  20.                 addContent(new Element("questions").setAttributes(list));     
  21.         //此处注意用setAttribute设置属性时,属性个数大于1时参数为Collection类  
  22.           
  23.         myDoc.setRootElement(myRoot);       //设置根节点  
  24.         subRoot = myRoot.getChild("questions");    //移动到需要插入数据的节点  
  25.     }  
  26.       
  27.     //写一个用来插入excel中一行数据的函数,注意如何形成所需结构  
  28.     public void addOneItem(String question,String anwser,String option1,String option2,String option3){  
  29.         subRoot.addContent(new Element("questionItem").setAttribute("answerid",anwser).  
  30.                 addContent(new Element("text").setText(question)).  
  31.                 addContent(new Element("answer").setAttribute("id","1").setText(option1)).  
  32.                 addContent(new Element("answer").setAttribute("id","2").setText(option2)).  
  33.                 addContent(new Element("answer").setAttribute("id","3").setText(option3)));  
  34.     }  
  35.     //将数据导出为xml文件的函数  
  36.     public void toXml()throws IOException, JDOMException{  
  37.         XMLOutputter   XMLOut   =   new   XMLOutputter();  
  38.         XMLOut.output(myDoc,   new   FileOutputStream("test1.xml"));  
  39.     }  

2.写主函数类ExcelToXml,主函数中需要读取excel并调用GenEduXml

 

 

  
  
  
  
  1. import java.io.File;   
  2. import jxl.*;   
  3.  
  4. public class ExcelToXml {  
  5.     public static void main(String args[]){  
  6.         try{  
  7.             Workbook workbook = Workbook.getWorkbook(new File("D:\\myfile.xls"));  
  8.             Sheet sheet = workbook.getSheet(0);     //获得第一个sheet  
  9.               
  10.             GenEduXml gen = new GenEduXml();    //创建生成xml文件的实例  
  11.             gen.init();  
  12.               
  13.             Cell[] a = sheet.getColumn(0);  //获得第一列  
  14.             Cell[] b = sheet.getColumn(1);  
  15.             Cell[] c = sheet.getColumn(2);   
  16.             Cell[] d = sheet.getColumn(3);   
  17.             Cell[] e = sheet.getColumn(4);   
  18.               
  19.             for(int i=0;i<a.length;i++){  
  20.                 gen.addOneItem(a[i].getContents(), b[i].getContents(), c[i].getContents(), d[i].getContents(), e[i].getContents());  
  21.             }  
  22.             workbook.close();  
  23.             gen.toXml();      
  24.         }catch(Exception e){  
  25.             e.printStackTrace();  
  26.         }  
  27.         System.out.println("Xml has been biult!");  
  28.     }  

这样就可以形成所需的xml文件了

你可能感兴趣的:(职场,休闲,Excel生成Xml)