Apache POI提供了Java程序讀取和寫入MS Office文檔的接口。 對於Excel文檔讀寫: -HSSF:提供讀寫MS Excel的xls文件的功能。 -XSSF:提供讀寫MS Excel的xlsx文件的功能。java
Java使用POI實現對Excel文件的讀寫操做
本篇的ExcelUtils工具類,主要針對於**一行數據(row)做爲一個實例(t)**的狀況使用。c++
Excel對象 | Java對象 |
---|---|
表格(sheet) | List<T> |
行(row) | T 的一個實例 (t) |
單元格(cell) | 對象 t 的某個屬性 |
package com.liziczh.ims.tools; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.util.LinkedList; import java.util.List; /** * Excel文件讀寫工具類:針對一行數據(row)做爲一個實例(t)的狀況 */ public class ExcelUtils { /** * 針對xlsx文件,使用XSSFWorkbook,要求excel版本在2007以上 * * @param T 泛型類,行對象 * @param filepath 文件路徑 * @return * @throws Exception */ public static <T> List<T> readExcel(Class T,String filepath){ try { if(filepath != null && !"".equals(filepath)){ // 工做簿 Workbook xwb = new XSSFWorkbook(new FileInputStream(filepath)); // 表格 Sheet sheet = null; // 行 Row row = null; // 單元格 Cell cell = null; // 表 sheet = xwb.getSheetAt(0); List<T> sheetList = new LinkedList<>(); for(int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++){ // 獲取第i行 row = sheet.getRow(i); // 利用反射生成一個實例 T t = (T) T.newInstance(); // 依此獲取單元格放入對象t中 for(int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++){ // 獲取第i行第j列的單元格, cell = row.getCell(j); // 獲取對象的屬性數組 Field[] fs = t.getClass().getDeclaredFields(); // 設置屬性爲可訪問 fs[j].setAccessible(true); // 類型轉換:將單元格內容先轉爲String再轉爲當前屬性所對應的類型 if(fs[j].getType() == String.class){ fs[j].set(t,fs[j].getType().cast(cell.toString())); } else if (fs[j].getType() == int.class) { fs[j].set(t,new Integer(cell.toString())); } else if(fs[j].getType() == short.class){ fs[j].set(t,new Short(cell.toString())); }else if(fs[j].getType() == long.class){ fs[j].set(t,new Long(cell.toString())); }else if(fs[j].getType() == byte.class){ fs[j].set(t,new Byte(cell.toString())); }else if(fs[j].getType() == float.class){ fs[j].set(t,new Float(cell.toString())); }else if(fs[j].getType() == double.class){ fs[j].set(t,new Double(cell.toString())); } } // 將對象t添加到集合中 sheetList.add(t); } return sheetList; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 針對xlsx文件,使用XSSFWorkbook,要求excel版本在2007以上 * * @param list 數據 * @param T 泛型類,行對象 * @param colNames 表頭信息, * @param filepath 文件路徑 * @return * @throws Exception */ public static <T> void writeExcel(List<T> list,Class T,String[] colNames,String filepath) { if(filepath != null && !"".equals(filepath)){ // 工做簿 Workbook workbook = new XSSFWorkbook(); // 表格 Sheet sheet = workbook.createSheet("0"); // 行 Row row = null; // 單元格 Cell cell = null; // 設置表頭樣式 CellStyle headerStyle = workbook.createCellStyle(); headerStyle.setAlignment(HorizontalAlignment.CENTER); Font headerFont = workbook.createFont(); headerFont.setBold(true); headerStyle.setFont(headerFont); // 經過colNames數組生成表頭 row = sheet.createRow(0); for (int c = 0; c < colNames.length; c++) { cell = row.createCell(c); cell.setCellValue(colNames[c]); cell.setCellStyle(headerStyle); } // 設置單元格樣式 CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // 經過一個List生成表內數據 for (int r = 0; r < list.size(); r++) { // 獲取一個List元素(即一個T的實例) T t = list.get(r); // 獲取對象t的全部屬性 Field[] fs = t.getClass().getDeclaredFields(); // 生成行 row = sheet.createRow(r + 1); // 依此獲取對象t的屬性值 賦予 單元格 for (int j = 0; j < fs.length; j++) { try { // 設置屬性爲可訪問 fs[j].setAccessible(true); // 生成一個單元格 cell = row.createCell(j); // 將屬性值賦予單元格 cell.setCellValue(String.valueOf(fs[j].get(t))); cell.setCellStyle(cellStyle); } catch (IllegalAccessException e) { e.printStackTrace(); } } } // 設置表名 workbook.setSheetName(0, T.getName()); // 生成xlsx文件 FileOutputStream out = null; try { out = new FileOutputStream(new File(filepath)); workbook.write(out); } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }