• 如何在Java中将Excel转化为XML格式文件


    将用户上传的Excel文件转化成XML文件

    package test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    import com.sppm.kds.entity.Policy;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    
    public class ImportTest {
    
        @Test
        public void test01(){
            File file = new File("E:\import\语料.xls");
            readExcelAddXML(file,"E:\import");
        }
        
        public void readExcelAddXML(File f,String path){
            OutputStreamWriter pw = null;
            try {
                List<Policy> list = readExcelFileOutList(f);
                //String tempFile = "E:\import\test.xml";
                File file = new File(path,"tianlong.xml");
                if (!file.exists()) {
                    file.createNewFile();
                }
                //确认流的输出文件和编码格式,此过程创建了“test.txt”实例
                pw = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
                
                pw.write("<docs>"+"
    ");
                pw.write("<lst>"+"
    ");
                pw.write("<num>"+list.size()+"</num>"+"
    ");
                pw.write("</lst>"+"
    ");
                for(int i=0;i <list.size();i++){
                    pw.write("<doc>"+"
    ");
                    pw.write("<id>"+list.get(i).getId()+"</id>"+"
    ");
                    pw.write("<title>"+list.get(i).getTitle()+"</title>"+"
    ");
                    pw.write("<department>"+list.get(i).getDepatment()+"</department>"+"
    ");
                    pw.write("<release_date>"+list.get(i).getRelease_date()+"</release_date>"+"
    ");
                    pw.write("<timeliness>"+list.get(i).getTimeliness()+"</timeliness>"+"
    ");
                    pw.write("<province>"+list.get(i).getProvince()+"</province>"+"
    ");
                    pw.write("<category>"+list.get(i).getCategory()+"</category>"+"
    ");
                    pw.write("<effect_level>"+list.get(i).getEffect_level()+"</effect_level>"+"
    ");
                    pw.write("<source>"+list.get(i).getSource()+"</source>"+"
    ");
                    pw.write("<content>"+list.get(i).getContent()+"</content>"+"
    ");
                    pw.write("<post_date>"+list.get(i).getPost_date()+"</post_date>"+"
    ");
                    pw.write("<issued_number>"+list.get(i).getIssued_number()+"</issued_number>"+"
    ");
                    pw.write("<implement_date>"+list.get(i).getImplement_date()+"</implement_date>"+"
    ");
                    pw.write("</doc>");
                    pw.flush();
                }
                pw.write("
    "+"</docs>");
                pw.flush();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    pw.close();//关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        private static List<Policy> readExcelFileOutList(File f) {  
            System.out.println("进入到readExcelFileOutList()");
            List<Policy> list = new ArrayList<Policy>();
            InputStream is = null;
            try{  
               is = new FileInputStream(f);  
               Workbook rwb = Workbook.getWorkbook(is);  
               //获得总的Sheets,得到sheet的层数
               Sheet[] sheets = rwb.getSheets();  
               int sheetLen = sheets.length;  
               //获得第一个Sheets 的结果  
               jxl.Sheet rs = rwb.getSheet(0);   
               int num_row = rs.getRows();//得到行数  
               int num_column=rs.getColumns();//得到列数
               
               System.out.println("行:"+num_row+"列:"+num_column);
               for(int j=1;j < num_row ;j++){  
                   Cell[] cell  = rs.getRow(j);//得到第j行的所有值  
                     
                   for(int column_index=0;column_index<num_column;column_index++){  
                       String id = cell[column_index++].getContents();
                       String title= cell[column_index++].getContents();
                       String department = cell[column_index++].getContents();
                       String release_date = cell[column_index++].getContents();
                       String timeliness= cell[column_index++].getContents();
                       String province = cell[column_index++].getContents();
                       String category= cell[column_index++].getContents();
                       String effect_level = cell[column_index++].getContents();
                       String source = cell[column_index++].getContents();
                       String content = cell[column_index++].getContents();
                       String post_date= cell[column_index++].getContents();
                       String issued_number = cell[column_index++].getContents();
                       String implement_date = cell[column_index++].getContents();
                       list.add(new Policy(id, title, department, release_date, timeliness, province, category, effect_level, source, content, post_date,issued_number,implement_date));
                   }  
                }  
            } catch(Exception ex) {
                ex.printStackTrace();  
            } finally {
                try {
                    if(is != null) is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return list;
       } 
        
    }

    愿我们漂泊半生, 归来仍少年!
  • 相关阅读:
    《P3953 [NOIP2017 提高组] 逛公园》
    《P4180 [BJWC2010]严格次小生成树》
    《济南icpc补题》
    《levil的因子和》
    《洛谷P2704 [NOI2001]炮兵阵地》
    《Codeforces Round #689 (Div. 2, based on Zed Code Competition)》
    《2174: Leapin' Lizards》
    《3820: Revenge of Fibonacci 》
    马拉车求最长回文子串
    二分训练
  • 原文地址:https://www.cnblogs.com/Lonnn/p/6890217.html
Copyright © 2020-2023  润新知