1. 程式人生 > 實用技巧 >JAVA Poi工具類

JAVA Poi工具類

  Apache POI 簡介是用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對Microsoft Office(Excel、WORD、PowerPoint、Visio等)格式檔案讀和寫的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“可憐的模糊實現”。   HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能 (office97-2003版本支援,.xls)   XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能(office 2007以後的版本支援,.xlsx)   HWPF - 提供讀寫Microsoft Word DOC格式檔案的功能   HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能   HDGF - 提供讀Microsoft Visio格式檔案的功能   HPBF - 提供讀Microsoft Publisher格式檔案的功能   HSMF - 提供讀Microsoft Outlook格式檔案的功能

1.pom

 1 <dependency>
 2   <groupId>org.apache.poi</groupId>
 3   <artifactId>poi</artifactId>
 4   <version>3.14</version>
 5 </dependency>
 6 <dependency>
 7   <groupId>org.apache.poi</groupId>
 8   <artifactId>poi-ooxml</artifactId>
9 <version>3.14</version> 10 </dependency>

2.工具類

  1 import java.io.FileNotFoundException;
  2 import java.io.IOException;
  3 import java.io.InputStream;
  4 import java.text.SimpleDateFormat;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
8 import org.apache.poi.ss.usermodel.Cell; 9 import org.apache.poi.ss.usermodel.Row; 10 import org.apache.poi.ss.usermodel.Sheet; 11 import org.apache.poi.ss.usermodel.Workbook; 12 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 13 import org.springframework.web.multipart.MultipartFile; 14 15 public class POIUtils { 16 private final static String xls = "xls"; 17 private final static String xlsx = "xlsx"; 18 private final static String DATE_FORMAT = "yyyy/MM/dd"; 19 /** 20 * 讀入excel檔案,解析後返回 21 * @param file 22 * @throws IOException 23 */ 24 public static List<String[]> readExcel(MultipartFile file) throws IOException { 25 //檢查檔案 26 checkFile(file); 27 //獲得Workbook工作薄物件 28 Workbook workbook = getWorkBook(file); 29 //建立返回物件,把每行中的值作為一個數組,所有行作為一個集合返回 30 List<String[]> list = new ArrayList<String[]>(); 31 if(workbook != null){ 32 for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){ 33 //獲得當前sheet工作表 34 Sheet sheet = workbook.getSheetAt(sheetNum); 35 if(sheet == null){ 36 continue; 37 } 38 //獲得當前sheet的開始行 39 int firstRowNum = sheet.getFirstRowNum(); 40 //獲得當前sheet的結束行 41 int lastRowNum = sheet.getLastRowNum(); 42 //迴圈除了第一行的所有行 43 for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++){ 44 //獲得當前行 45 Row row = sheet.getRow(rowNum); 46 if(row == null){ 47 continue; 48 } 49 //獲得當前行的開始列 50 int firstCellNum = row.getFirstCellNum(); 51 //獲得當前行的列數 52 int lastCellNum = row.getPhysicalNumberOfCells(); 53 String[] cells = new String[row.getPhysicalNumberOfCells()]; 54 //迴圈當前行 55 for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){ 56 Cell cell = row.getCell(cellNum); 57 cells[cellNum] = getCellValue(cell); 58 } 59 list.add(cells); 60 } 61 } 62 workbook.close(); 63 } 64 return list; 65 } 66 67 //校驗檔案是否合法 68 public static void checkFile(MultipartFile file) throws IOException{ 69 //判斷檔案是否存在 70 if(null == file){ 71 throw new FileNotFoundException("檔案不存在!"); 72 } 73 //獲得檔名 74 String fileName = file.getOriginalFilename(); 75 //判斷檔案是否是excel檔案 76 if(!fileName.endsWith(xls) && !fileName.endsWith(xlsx)){ 77 throw new IOException(fileName + "不是excel檔案"); 78 } 79 } 80 public static Workbook getWorkBook(MultipartFile file) { 81 //獲得檔名 82 String fileName = file.getOriginalFilename(); 83 //建立Workbook工作薄物件,表示整個excel 84 Workbook workbook = null; 85 try { 86 //獲取excel檔案的io流 87 InputStream is = file.getInputStream(); 88 //根據檔案字尾名不同(xls和xlsx)獲得不同的Workbook實現類物件 89 if(fileName.endsWith(xls)){ 90 //2003 91 workbook = new HSSFWorkbook(is); 92 }else if(fileName.endsWith(xlsx)){ 93 //2007 94 workbook = new XSSFWorkbook(is); 95 } 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 return workbook; 100 } 101 public static String getCellValue(Cell cell){ 102 String cellValue = ""; 103 if(cell == null){ 104 return cellValue; 105 } 106 //如果當前單元格內容為日期型別,需要特殊處理 107 String dataFormatString = cell.getCellStyle().getDataFormatString(); 108 if(dataFormatString.equals("m/d/yy")){ 109 cellValue = new SimpleDateFormat(DATE_FORMAT).format(cell.getDateCellValue()); 110 return cellValue; 111 } 112 //把數字當成String來讀,避免出現1讀成1.0的情況 113 if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ 114 cell.setCellType(Cell.CELL_TYPE_STRING); 115 } 116 //判斷資料的型別 117 switch (cell.getCellType()){ 118 case Cell.CELL_TYPE_NUMERIC: //數字 119 cellValue = String.valueOf(cell.getNumericCellValue()); 120 break; 121 case Cell.CELL_TYPE_STRING: //字串 122 cellValue = String.valueOf(cell.getStringCellValue()); 123 break; 124 case Cell.CELL_TYPE_BOOLEAN: //Boolean 125 cellValue = String.valueOf(cell.getBooleanCellValue()); 126 break; 127 case Cell.CELL_TYPE_FORMULA: //公式 128 cellValue = String.valueOf(cell.getCellFormula()); 129 break; 130 case Cell.CELL_TYPE_BLANK: //空值 131 cellValue = ""; 132 break; 133 case Cell.CELL_TYPE_ERROR: //故障 134 cellValue = "非法字元"; 135 break; 136 default: 137 cellValue = "未知型別"; 138 break; 139 } 140 return cellValue; 141 } 142 }