本文整理了,使用Apache POI 框架解析、讀取Excel文件,過程中,程序代碼出現的一些問題,並解決
1、.xls 和 .xlsx
我們知道Excel文檔,在Windows下,分為Excel2003 和 Excel2007.兩者有一些區別,最直觀的,就是后綴名不一樣,分別是 .xls 和 .xlsx
使用Apache POI 解析時,需要區別對待。用不同的API去解析。
但是,卻也提供了一個,統一去解析的API,那就是 org.apache.poi.ss.usermodel.WorkbookFactory;
開發,運行的時候,需要導入這樣的幾個 .jar 包
不然,就會出問題:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
這個錯誤是由於POI包是默認不支持excel2007操作的,要加入一個xmlbeans的包xbean.jar
使用POI庫,在實例化XSSFWorkbook對象時,報 Java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions 的錯誤,經檢查,是因為官方包里默認是不包含xmlbean.jar包的,需要自己添加xmlbeans.jar這個包
public void parseXml(String filename) { Workbook wb = null; try { wb = WorkbookFactory.create(new File(filename)); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { System.out.print(getCellValue(cell) + "---"); save(getCellValue(cell) + "---"); } System.out.println(); } } catch (EncryptedDocumentException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) { e.printStackTrace(); } }
2、解析文件時出現:lang.RuntimeException: Unexpected record type (org.apache.poi.hssf.record.DefaultRowHeightRecord)
解決:excel打開另存一下就可以了
3、一個可以遍歷,解析文件夾下,全部Excel文件內容的代碼
解析的內容,保存到了一個 .txt 文件中
已調試通過,如下:
public class ParseExcel { public static void main(String[] args) throws IOException { String path = "C:\\Users\\文件夾目錄路徑\\Desktop\\a01hos\\img"; File f = new File(path); File[] files = f.listFiles(); System.out.println(files.length); File[] filesxls = f.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { if (name.endsWith(".xls") || name.endsWith(".xlsx")) { return true; } return false; } }); System.out.println("Excel文件有: " + filesxls.length); for (File f2 : filesxls) { String fileDirectPathName = f2.getCanonicalPath(); System.out.println(fileDirectPathName); // System.out.println("文件名: " + f2.getName()); new ParseExcel().parseXml(fileDirectPathName); } } public void parseXml(String filename) { Workbook wb = null; try { wb = WorkbookFactory.create(new File(filename)); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { System.out.print(getCellValue(cell) + "---"); save(getCellValue(cell) + "---"); } System.out.println(); } } catch (EncryptedDocumentException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Object getCellValue(Cell cell) { int type = cell.getCellType(); String show = null; switch (type) { case Cell.CELL_TYPE_BLANK:// 空值 show = null; break; case Cell.CELL_TYPE_BOOLEAN:// Boolean show = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR:// 故障 show = String.valueOf(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA:// 公式 show = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC:// 數字 show = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING:// 字符串 show = cell.getStringCellValue(); break; default: show = null; } return show; } /** * 保存字符串到文本中 * * @param str */ public boolean save(String str) { boolean flag = false; // 聲明操作標記 String fileName = "file/test.txt"; // 定義文件名 File f = new File(fileName); if(!f.exists()){ try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileWriter fw = null; // 用來寫入字符文件的便捷類 PrintWriter out = null; // 向文本輸出流打印對象的格式化表示形式類 try { fw = new FileWriter(f, true); // 創建一個FileWriter out = new PrintWriter(fw); // 創建一個PrintWriter,以追加方式將內容插入到最后一行 out.println(str); // 將字符串打印到文本中 out.flush(); // 刷新緩存 flag = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 關閉PrintWriter if (out != null) { out.close(); out = null; } // 關閉FileWriter if (fw != null) { fw.close(); fw = null; } } catch (IOException e) { e.printStackTrace(); } } return flag; } }
掃個紅包吧!
Donate捐贈
如果我的文章幫助了你,可以贊賞我 6.66 元給我支持,讓我繼續寫出更好的內容)
(微信) (支付寶)
微信/支付寶 掃一掃