POI簡介
Apache POI 是用Java編寫的免費開源的跨平台的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“簡潔版的模糊實現”。
- HSSF : 讀寫 Microsoft Excel XLS 格式文檔
- XSSF : 讀寫 Microsoft Excel OOXML XLSX 格式文檔
- SXSSF : 讀寫 Microsoft Excel OOXML XLSX 格式文檔
- HWPF : 讀寫 Microsoft Word DOC 格式文檔
- HSLF : 讀寫 Microsoft PowerPoint 格式文檔
- HDGF : 讀 Microsoft Visio 格式文檔
- HPBF : 讀 Microsoft Publisher 格式文檔
- HSMF : 讀 Microsoft Outlook 格式文檔
三、POI常用類說明
類名 說明
HSSFWorkbook Excel的文檔對象
HSSFSheet Excel的表單
HSSFRow Excel的行
HSSFCell Excel的格子單元
HSSFFont Excel字體
HSSFDataFormat 格子單元的日期格式
HSSFHeader Excel文檔Sheet的頁眉
HSSFFooter Excel文檔Sheet的頁腳
HSSFCellStyle 格子單元樣式
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 錯誤信息表
官網鏈接:https://poi.apache.org/components/index.html
需要的jar包:
poi-ooxml-XXX.jar
poi-ooxml-schemas-XXX.jar
poi-scratchpad-XXX.jar
POI的不同版本在代碼編寫上有差異,舊版本的某些代碼不適用於新版本,后面詳細展開。
maven導入
<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.0.1</version> </dependency> </dependencies>
主程序讀取Excel
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class FetchExcel { public static void main(String[] args){ String filePath = "D:\\測試文件\\test.xlsx"; InputStream fis = null; try{ fis = new FileInputStream(filePath); Workbook wb = null; if(filePath.endsWith((".xlsx"))){ wb = new XSSFWorkbook(fis); }else if(filePath.endsWith(".xls") || filePath.endsWith(".et")){ wb = new HSSFWorkbook(fis); } fis.close(); /* 讀Excel文字內容 */ // 獲取第一個sheet表,也可使用sheet表名獲取 Sheet sheet = wb.getSheetAt(0); // 獲取行 Iterator<Row> rows = sheet.rowIterator(); Row row; Cell cell; while(rows.hasNext()){ row = rows.next(); //獲取單元格 Iterator<Cell> cells =row.cellIterator(); while (cells.hasNext()){ cell = cells.next(); //POIUtil是另一個類 String cellValue = POIUtil.getCellValue(cell); System.out.print(cellValue + " "); } System.out.println(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (null != fis){ try{ fis.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
獲取cell中的值並返回String類型工具類
import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.CellType; //獲取cell中的值並返回String類型工具類 public class POIUtil { public static String getCellValue(Cell cell) { String cellValue = ""; if (null != cell) { //以下是判斷數據類型 switch (cell.getCellType()) { case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { //判斷是否為日期類型 Date date = cell.getDateCellValue(); DateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); cellValue = formater.format(date); } else { DecimalFormat df = new DecimalFormat("####.####"); cellValue = df.format(cell.getNumericCellValue()); } break; case STRING: cellValue = cell.getStringCellValue(); break; case BOOLEAN: cellValue = cell.getBooleanCellValue() + ""; break; case BLANK: //空值 cellValue = ""; case ERROR: cellValue = "非法字符"; break; } } return cellValue; } }
測試結果:
POI版本問題:
看了很多博客,在判斷單元格是什么數據類型時,使用的代碼如下:
switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: break; case Cell.CELL_TYPE_STRING: cellValue = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: cellValue = “錯誤類型”; break; }
Cell.CELL_TYPE_XXX: 的格式在POI4.0.1的版本是報錯的,這種寫法兼容哪個版本不清楚。
改為以下格式后正常:
switch (cell.getCellType()) { case NUMERIC: break; case STRING: cellValue = cell.getStringCellValue(); break; case BOOLEAN: cellValue = cell.getBooleanCellValue() + ""; break; case BLANK: //空值 cellValue = ""; case ERROR: cellValue = "非法字符"; break; } }
_NONE(-1), // none類型
NUMERIC(0), // 數值類型
STRING(1), // 字符串類型
FORMULA(2), // 公式類型
BLANK(3), // 空格類型
BOOLEAN(4), // 布爾類型
ERROR(5); // 錯誤