依賴jarjava
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency>
ExcelUtils.javaapache
package javax.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.math.BigDecimal; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Excel 工具類(兼容xls和xlsx) * * @author Logan * @version 1.0.0 * @createDate 2019-03-07 * */ public class ExcelUtils { private static final String XLS = "xls"; private static final String XLSX = "xlsx"; private static final DateFormat FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); /** * 輸出數據到自定義模版的Excel輸出流 * * @param excelTemplate 自定義模版文件 * @param data 數據 * @param outputStream Excel輸出流 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static void writeDataToTemplateOutputStream(File excelTemplate, List<List<Object>> data, OutputStream outputStream) throws IOException { Workbook book = ExcelUtils.getWorkbookFromExcel(excelTemplate); ExcelUtils.writeDataToWorkbook(null, data, book, 0); ExcelUtils.writeWorkbookToOutputStream(book, outputStream); } /** * 從Excel文件獲取Workbook對象 * * @param excelFile Excel文件 * @return Workbook對象 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static Workbook getWorkbookFromExcel(File excelFile) throws IOException { try ( InputStream inputStream = new FileInputStream(excelFile); ) { if (excelFile.getName().endsWith(XLS)) { return new HSSFWorkbook(inputStream); } else if (excelFile.getName().endsWith(XLSX)) { return new XSSFWorkbook(inputStream); } else { throw new IOException("文件類型錯誤"); } } } /** * 把Workbook對象內容輸出到Excel文件 * * @param book Workbook對象 * @param file Excel文件 * @throws FileNotFoundException 找不到文件異常,文件已建立,實際不存在該異常 * @throws IOException 輸入輸出異常 */ public static void writeWorkbookToFile(Workbook book, File file) throws FileNotFoundException, IOException { if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } try ( OutputStream outputStream = new FileOutputStream(file); ) { writeWorkbookToOutputStream(book, outputStream); } } /** * 把Workbook對象輸出到Excel輸出流 * * @param book Workbook對象 * @param outputStream Excel輸出流 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static void writeWorkbookToOutputStream(Workbook book, OutputStream outputStream) throws IOException { book.write(outputStream); } /** * 輸出數據到Workbook對象中指定頁碼 * * @param title 標題,寫在第一行,可傳null * @param data 數據 * @param book Workbook對象 * @param page 輸出數據到Workbook指定頁碼的頁面數 */ public static void writeDataToWorkbook(List<String> title, List<List<Object>> data, Workbook book, int page) { Sheet sheet = book.getSheetAt(page); Row row = null; Cell cell = null; // 設置表頭 if (null != title && !title.isEmpty()) { row = sheet.getRow(0); if (null == row) { row = sheet.createRow(0); } for (int i = 0; i < title.size(); i++) { cell = row.getCell(i); if (null == cell) { cell = row.createCell(i); } cell.setCellValue(title.get(i)); } } List<Object> rowData = null; for (int i = 0; i < data.size(); i++) { row = sheet.getRow(i + 1); if (null == row) { row = sheet.createRow(i + 1); } rowData = data.get(i); if (null == rowData) { continue; } for (int j = 0; j < rowData.size(); j++) { cell = row.getCell(j); if (null == cell) { cell = row.createCell(j); } setValue(cell, rowData.get(j)); } } } /** * 讀取Excel文件第一頁 * * @param pathname 文件路徑名 * @return 第一頁數據集合 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static List<List<Object>> readExcelFirstSheet(String pathname) throws IOException { File file = new File(pathname); return readExcelFirstSheet(file); } /** * 讀取Excel文件第一頁 * * @param file Excel文件 * @return 第一頁數據集合 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static List<List<Object>> readExcelFirstSheet(File file) throws IOException { try ( InputStream inputStream = new FileInputStream(file); ) { if (file.getName().endsWith(XLS)) { return readXlsFirstSheet(inputStream); } else if (file.getName().endsWith(XLSX)) { return readXlsxFirstSheet(inputStream); } else { throw new IOException("文件類型錯誤"); } } } /** * 讀取xls格式Excel文件第一頁 * * @param inputStream Excel文件輸入流 * @return 第一頁數據集合 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static List<List<Object>> readXlsFirstSheet(InputStream inputStream) throws IOException { Workbook workbook = new HSSFWorkbook(inputStream); return readExcelFirstSheet(workbook); } /** * 讀取xlsx格式Excel文件第一頁 * * @param inputStream Excel文件輸入流 * @return 第一頁數據集合 * @throws IOException 錯誤時拋出異常,由調用者處理 */ public static List<List<Object>> readXlsxFirstSheet(InputStream inputStream) throws IOException { Workbook workbook = new XSSFWorkbook(inputStream); return readExcelFirstSheet(workbook); } /** * 讀取Workbook第一頁 * * @param book Workbook對象 * @return 第一頁數據集合 */ public static List<List<Object>> readExcelFirstSheet(Workbook book) { return readExcel(book, 0); } /** * 讀取指定頁面的Excel * * @param book Workbook對象 * @param page 頁碼 * @return 指定頁面數據集合 */ public static List<List<Object>> readExcel(Workbook book, int page) { List<List<Object>> list = new ArrayList<>(); Sheet sheet = book.getSheetAt(page); for (int i = 0; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); // 若是當前行爲空,則加入空,保持行號一致 if (null == row) { list.add(null); continue; } List<Object> columns = new ArrayList<>(); for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); columns.add(getValue(cell)); } list.add(columns); } return list; } /** * 解析單元格中的值 * * @param cell 單元格 * @return 單元格內的值 */ private static Object getValue(Cell cell) { if (null == cell) { return null; } Object value = null; switch (cell.getCellType()) { case BOOLEAN: value = cell.getBooleanCellValue(); break; case NUMERIC: // 日期類型,轉換爲日期 if (DateUtil.isCellDateFormatted(cell)) { value = cell.getDateCellValue(); } // 數值類型 else { // 默認返回double,建立BigDecimal返回準確值 value = new BigDecimal(cell.getNumericCellValue()); } break; default: value = cell.toString(); break; } return value; } /** * 設置單元格值 * * @param cell 單元格 * @param value 值 */ private static void setValue(Cell cell, Object value) { if (null == cell) { return; } if (null == value) { cell.setCellValue((String) null); } else if (value instanceof Boolean) { cell.setCellValue((Boolean) value); } else if (value instanceof Date) { cell.setCellValue(FORMAT.format((Date) value)); } else if (value instanceof Double) { cell.setCellValue((Double) value); } else { cell.setCellValue(value.toString()); } } }
.xss