java读取目录下的多个xml文件,并写入excel
2021-08-12 本文已影响0人
jinhm007
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<String> 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<String,String> map= getData(path);
// writeExcel(map);
String path="C:\\Users\\Jhm\\Desktop\\test";
List<String> fileLists =getFile(path);
WritableSheet sheet=iniExcel();
iniExcel();
for(int i=0;i<fileLists.size();i++){
Map<String,String> map=getData(fileLists.get(i));
System.out.println(map.size());
writeExcel(map,i);
}
CloseExcel();
}
/**
* 遍历文件夹下的目录,获取文件路径
* @param path
* @return
*/
private static List<String> 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<String,String> getData(String path) throws IOException {
Map<String,String> 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 ="<string name=\"app_name\">XRecorder</string>";
String regex ="<string name=\"(.*?)\">(.*?)</string>";
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<String,String> map,int count) throws WriteException {
Label label =null;
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
int i=0;
int j=0;
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
label=new Label(0,i,entry.getKey());// 列,行,值
i++;
sheet.addCell(label);
}
for (Map.Entry<String, String> 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