java用poi讀取Excel表格中的數據
Java讀寫Excel的包是Apache POI(項目地址:http://poi.apache.org/),因此需要先獲取POI的jar包,本實驗使用的是POI 3.9穩定版。
Apache POI 代碼例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
本例子可以讀取Microsoft Office Excel 2003/2007/2010,具體代碼及註釋如下:
讀取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的內容,例如:HSSFWorkbook
讀取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的內容,例如:XSSFWorkbook
讀取兩種格式使用 import org.apache.poi.ss.usermodel.* 包的內容,例如:Workbook
引入包如下:
1 import org.apache.poi.ss.usermodel.Cell; 2 import org.apache.poi.ss.usermodel.Row; 3 import org.apache.poi.ss.usermodel.Sheet; 4 import org.apache.poi.ss.usermodel.Workbook; 5 import org.apache.poi.ss.usermodel.WorkbookFactory; 6 import org.apache.poi.ss.usermodel.DateUtil;
【其中的DateUtil不是必須的】
1 /** 2 * 讀取Excel測試,兼容 Excel 2003/2007/2010 3 */ 4 public String readExcel() 5 { 6 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 7 try { 8 //同時支持Excel 2003、2007 9 File excelFile = new File("/home/zht/test.xls"); //創建文件對象 10 FileInputStream is = newFileInputStream(excelFile); //文件流 11 Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的 12 int sheetCount = workbook.getNumberOfSheets(); //Sheet的數量 13 //遍歷每個Sheet 14 for (int s = 0; s < sheetCount; s++) { 15 Sheet sheet = workbook.getSheetAt(s); 16 int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數 17 //遍歷每一行 18 for (int r = 0; r < rowCount; r++) { 19 Row row = sheet.getRow(r); 20 int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數 21 //遍歷每一列 22 for (int c = 0; c < cellCount; c++) { 23 Cell cell = row.getCell(c); 24 int cellType = cell.getCellType(); 25 String cellValue = null; 26 switch(cellType) { 27 case Cell.CELL_TYPE_STRING: //文本 28 cellValue = cell.getStringCellValue(); 29 break; 30 case Cell.CELL_TYPE_NUMERIC: //數字、日期 31 if(DateUtil.isCellDateFormatted(cell)) { 32 cellValue = fmt.format(cell.getDateCellValue()); //日期型 33 } 34 else { 35 cellValue = String.valueOf(cell.getNumericCellValue()); //數字 36 } 37 break; 38 case Cell.CELL_TYPE_BOOLEAN: //布爾型 39 cellValue = String.valueOf(cell.getBooleanCellValue()); 40 break; 41 case Cell.CELL_TYPE_BLANK: //空白 42 cellValue = cell.getStringCellValue(); 43 break; 44 case Cell.CELL_TYPE_ERROR: //錯誤 45 cellValue = "錯誤"; 46 break; 47 case Cell.CELL_TYPE_FORMULA: //公式 48 cellValue = "錯誤"; 49 break; 50 default: 51 cellValue = "錯誤"; 52 } 53 System.out.print(cellValue + " "); 54 } 55 System.out.println(); 56 } 57 } 58 59 } 60 catch (Exception e) { 61 e.printStackTrace(); 62 } 63 64 return Action.SUCCESS; 65 }
如果執行的代碼的過程中報出如下錯誤:
poi導入excel表格數據時Cannot get a text value from a numeric cell
異常描述:在導入excel的時候在獲取excel單元格數據的時候會出現Cannot get a text value from a numeric cell的異常拋出。
異常原因:poi讀取excel單元格的數據,cell有不同的數據類型(CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_FORMULA),如果cell中的數據是數值的話,如果你沒有給他設置cell的類型的話。默認會認為是CELL_TYPE_NUMERICl類型,如果從一個NUMBER類型的Cell使用.cell.getStringCellValue()讀取出一個字符串就會出錯。
解決的方法:在讀取數據之前,設置cell的類型為CELL_TYPE_STRING;
cell.setCellType(Cell.CELL_TYPE_STRING);
所以,上面的代碼可以簡單寫成:
1 /** 2 * 讀取Excel測試,兼容 Excel 2003/2007/2010 3 */ 4 public String readExcel() 5 { 6 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 7 try { 8 //同時支持Excel 2003、2007 9 File excelFile = new File("/home/zht/test.xls"); //創建文件對象 10 FileInputStream is = new FileInputStream(excelFile); //文件流 11 Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的 12 int sheetCount = workbook.getNumberOfSheets(); //Sheet的數量 13 //遍歷每個Sheet 14 for (int s = 0; s < sheetCount; s++) { 15 Sheet sheet = workbook.getSheetAt(s); 16 int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數 17 //遍歷每一行 18 for (int r = 0; r < rowCount; r++) { 19 Row row = sheet.getRow(r); 20 int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數 21 //遍歷每一個單元格 22 for (int c = 0; c < cellCount; c++) { 23 Cell cell = row.getCell(c); 24 int cellType = cell.getCellType(); 25 String cellValue = null; 26 27 //在讀取單元格內容前,設置所有單元格中內容都是字符串類型 28 cell.setCellType(Cell.CELL_TYPE_STRING); 29 30 //按照字符串類型讀取單元格內數據 31 cellValue = cell.getStringCellValue(); 32 33 /*在這裏可以對每個單元格中的值進行二次操作轉化*/ 34 35 System.out.print(cellValue + " "); 36 } 37 System.out.println(); 38 } 39 } 40 41 } 42 catch (Exception e) { 43 e.printStackTrace(); 44 } 45 46 return Action.SUCCESS; 47 }
項目中原來就有maven的相關poi依賴,具體如下:
1 maven依賴配置: 2 <!--poi--> 3 <dependency> 4 <groupId>fakepath</groupId> 5 <artifactId>poi-ooxml-schemas</artifactId> 6 <version>3.14-20160307</version> 7 8 <groupId>fakepath</groupId> 9 <artifactId>poi-scratchpad</artifactId> 10 <version>3.14-20160307</version> 11 12 <groupId>fakepath</groupId> 13 <artifactId>poi-ooxml</artifactId> 14 <version>3.14-20160307</version> 15 16 <groupId>fakepath</groupId> 17 <artifactId>poi-examples</artifactId> 18 <version>3.14-20160307</version> 19 20 <groupId>fakepath</groupId> 21 <artifactId>poi-excelant</artifactId> 22 <version>3.14-20160307</version> 23 24 <groupId>fakepath</groupId> 25 <artifactId>poi</artifactId> 26 <version>3.14-20160307</version> 27 28 <groupId>fakepath</groupId> 29 <artifactId>xmlbeans</artifactId> 30 <version>2.6.0</version> 31 32 </dependency>
java用poi讀取Excel表格中的數據