1 import java.io.BufferedWriter; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStreamWriter; 9 import java.util.HashMap; 10 11 import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 12 import org.apache.poi.ss.usermodel.Cell; 13 import org.apache.poi.ss.usermodel.Row; 14 import org.apache.poi.ss.usermodel.Sheet; 15 import org.apache.poi.ss.usermodel.Workbook; 16 import org.apache.poi.ss.usermodel.WorkbookFactory; 17 18 public class Excels2Xml { 19 20 private static HashMap<String, File> map = new HashMap<>(); 21 22 /** 23 * 输入文件路径,对于嵌套的目录不进行读取 24 * 25 * @param inPath 26 * @return 27 */ 28 public static HashMap<String, File> getFiles(String inPath) { 29 if (null == inPath || inPath.trim().length() == 0) 30 return null; 31 File filex = new File(inPath); 32 if (filex.isDirectory()) { 33 File[] files = filex.listFiles(); 34 if (files.length > 0) { 35 for (File file : files) { 36 if (file.isFile() && (file.getName().contains(".xlsx") || file.getName().contains(".xls"))) 37 map.put(file.getName(), file); 38 } 39 } 40 } else if (filex.isFile()) { 41 map.put(filex.getName(), filex); 42 } 43 return map; 44 } 45 46 /** 47 * 48 * @param map 49 * @param outPath 50 */ 51 public static void excels2Xml(HashMap<String, File> map, String outPath) { 52 if (map == null) 53 return; 54 map.forEach((fileName, file) -> { 55 BufferedWriter bw = null; 56 InputStream is = null; 57 try { 58 String fn = fileName.replace(".xlsx", ".xml"); 59 if (fn == fileName) { 60 fn = fileName.replace(".xls", ".xml"); 61 } 62 bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outPath + fn)), "utf-8")); 63 is = new FileInputStream(file.getPath()); 64 Workbook hssfWorkbook = WorkbookFactory.create(is); 65 if (null != hssfWorkbook) 66 bw.write("<xmls>\n"); 67 for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { 68 Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); 69 if (hssfSheet == null) { 70 continue; 71 } 72 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { 73 Row hssfRow = hssfSheet.getRow(rowNum); 74 if (hssfRow != null) { 75 Cell content = hssfRow.getCell(0); 76 Cell channel = hssfRow.getCell(2); 77 bw.write("<m1>\n"); 78 bw.write("<content>" + content + "</content>\n"); 79 bw.write("<channel>" + channel + "</channel>\n"); 80 bw.write("</m1>\n\n"); 81 } 82 } 83 } 84 bw.write("<xmls>\n"); 85 } catch (FileNotFoundException e) { 86 System.out.println("FileNotFoundException..."); 87 e.printStackTrace(); 88 } catch (InvalidFormatException e) { 89 System.out.println("InvalidFormatException..."); 90 e.printStackTrace(); 91 } catch (IOException e) { 92 System.out.println("IOException..."); 93 e.printStackTrace(); 94 } finally { 95 try { 96 if (bw != null) 97 bw.close(); 98 if (is != null) 99 is.close(); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } 103 } 104 }); 105 } 106 107 public static void main(String[] args) throws Exception { 108 long begin = System.currentTimeMillis(); 109 excels2Xml(getFiles("D:\\EXCEL\\"), "D:\\EXCEL\\xml\\"); 110 long end = System.currentTimeMillis(); 111 System.out.println("The program consume " + (end - begin) / 1000 / 60.0 + " seconds"); 112 } 113 }
说明:
本实例代码主要实现功能为:将D:\\EXCEL目录下的01.xlsx,02.xls文件,对应转换成01.xml、02.xml文件生成到对应的目录中
注意:
输出路径记得加上“\\”
时间: 2024-10-08 12:06:29