java读取目录下的多个xml文件,并写入excel

package xmldemo;

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

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetXml {
    public static List lists =new ArrayList<>();
    public static WritableWorkbook workbook;
    public  static WritableSheet sheet;



    public static void main(String[] args) throws IOException, WriteException {
        //String path ="C:\\Users\\Jhm\\Desktop\\2.0.1strings 2\\values-de\\strings.xml";
        //LinkedHashMap map= getData(path);
      // writeExcel(map);
        String path="C:\\Users\\Jhm\\Desktop\\test";
        List fileLists =getFile(path);
        WritableSheet sheet=iniExcel();
        iniExcel();

        for(int i=0;i map=getData(fileLists.get(i));
            System.out.println(map.size());
            writeExcel(map,i);

        }

        CloseExcel();




    }

    /**
     * 遍历文件夹下的目录,获取文件路径
     * @param path
     * @return
     */

    private static List getFile(String path) {
        // get file list where the path has
        File file = new File(path);
        // get the folder list
        File[] array = file.listFiles();

        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {
                System.out.println("*****" + array[i].getPath());
                lists.add(array[i].getPath());
            } else if (array[i].isDirectory()) {
                getFile(array[i].getPath());
            }
        }
        return  lists;
    }

    /**
     * 用正则表达式解析xml,获取xml下的文件内容
     * @param path
     * @return
     * @throws IOException
     */

    private static Map getData(String path) throws IOException {
        Map xmldata= new LinkedHashMap<>();
        File file=new File(path);
        FileInputStream fis =new FileInputStream(file);
        BufferedReader reader =new BufferedReader(new InputStreamReader(fis));
       // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\test.csv")));

        String st;
        //String regex ="XRecorder";
        String regex ="(.*?)";
        Pattern pattern =Pattern.compile(regex);
        while ((st=reader.readLine())!=null){
            if(st.contains("name")){
                Matcher matcher =pattern.matcher(st);
                while (matcher.find()){
                    //xmldata.put(matcher.group(1),matcher.group(2));
                 //   String ss=matcher.group(1)+','+matcher.group(2)+"\r\n";
                    //System.out.println(ss);
                    xmldata.put(matcher.group(1),matcher.group(2));
                   // bw.write(ss);

                }
            }

        }
        //bw.close();
     //  xmldata.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + xmldata.get(key)));
       /* for(String key:xmldata.keySet()){
            System.out.println("key:"+key+" "+"Value:"+xmldata.get(key));
        }*/
        return  xmldata;
    }

    /**
     * 初始化创建excel
     * @return
     * @throws IOException
     */
    private  static  WritableSheet  iniExcel()  throws IOException {
        File xlsFile = new File("D://write.xls");
        // 创建一个工作簿
         workbook = Workbook.createWorkbook(xlsFile);
        // 创建一个工作表
         sheet = workbook.createSheet("sheet1", 0);

        return  sheet;
    }
    

    private  static  void CloseExcel() throws IOException,WriteException{
        workbook.write();
        workbook.close();

    }

    /**
     * 写入excel
     * @param map
     * @param count
     * @throws WriteException
     */
    private  static  void  writeExcel(Map map,int count) throws  WriteException {


        Label label =null;
        Iterator> it = map.entrySet().iterator();
        int i=0;
        int j=0;
        while (it.hasNext()) {
            Map.Entry entry = it.next();

            label=new Label(0,i,entry.getKey());// 列,行,值
            i++;
            sheet.addCell(label);
        }


        for (Map.Entry m :map.entrySet())  {
            //System.out.println(m.getKey()+"--\t"+m.getValue());
            label=new Label(count+1,j,m.getValue());// 列,行,值
            j++;
            sheet.addCell(label);

        }


    }


}


+源文件:


微信截图_03.png
微信截图_01.png

+处理后的文件:


微信截图_02.png

你可能感兴趣的:(java读取目录下的多个xml文件,并写入excel)