public void detailExport() { String sourceSystem = getPara("source_system"); String dataDate = getPara("data_date"); Integer pointsType = getParaToInt("points_type"); List<ZjPointsConsumeDetail> zjPointsConsumeDetails = zjPointsConsumeDetailService.findByDate(sourceSystem, dataDate, pointsType); File modelFile = new File(PathKit.getWebRootPath() + File.separator + "excel" + File.separator +"pointsConsumeDetails.xlsx"); File outputFile = new File(PathKit.getWebRootPath() + File.separator + "temp" + File.separator + "積分消費明細對帳單.xlsx"); ExcelHelper excelHelper = SpringContextHolder.getBean(ExcelHelper.class); excelHelper.exportExcelFile("zjPointsConsumeDetailExcel", modelFile, outputFile, zjPointsConsumeDetails); renderFile(outputFile); }
其中,exportExcelFile有四個參數,分別是String mapping,File modelFile,File outputFile,List<> dataList。mapping對應ZjPointsConsumeDetailExcel 類,ZjPointsConsumeDetailExcel 的做用是將數據庫中的字段與excel中展現的字段一一對應,並可進行特殊字段的轉換,代碼以下:數據庫
package com.cwl.excel; import com.cwl.plugin.poi.ExcelField; import com.cwl.plugin.poi.ExcelModel; @ExcelModel(name="zjPointsConsumeDetailExcel", rowCount="4") public class ZjPointsConsumeDetailExcel { @ExcelField(fieldName = "id", index = "0", title = "序號", type = ExcelField.CELL_TYPE_NUMERIC) private Long id; @ExcelField(fieldName = "consume_kind", index = "1", title = "消費分類", type = ExcelField.CELL_TYPE_STRING) private String consumeKind; @ExcelField(fieldName = "consume_abstract", index = "2", title = "消費摘要", type = ExcelField.CELL_TYPE_STRING) private String consumeAbstract; @ExcelField(fieldName = "points_type", index = "3", title = "積分類型", type = ExcelField.CELL_TYPE_NUMERIC, convert = {"0:普通積分", "1:白金積分"}) private String pointsTypeName; @ExcelField(fieldName = "consume_points", index = "4", title = "消費分值", type = ExcelField.CELL_TYPE_NUMERIC) private Long consumePoints; @ExcelField(fieldName = "consume_time", index = "5", title = "消費時間", type = ExcelField.CELL_TYPE_STRING) private String consumeTime; @ExcelField(fieldName = "related_order", index = "6", title = "關聯訂單號", type = ExcelField.CELL_TYPE_STRING) private String relatedOrder; }
以上僅是如何使用,有空補上源碼。app
總結ui
導入:讀取Sheet信息,而且保存至數據庫。
導出:讀取數據庫的信息,轉成Sheet。.net
使用poi導出excelexcel
參考博客:使用poi實現導入導出code
/** * 導出數據至Excel文件 * @param excelColumns 報表頭信息 * @param excelHeadConvertMap 須要對數據進行特殊轉換的列 * @param modelFile 模板Excel文件 * @param outputFile 導出文件 * @param dataList 導入excel報表的數據來源 * @return void * 2012-4-19 上午10:04:30 */ public void exportExcelFile(ExcelHead head, File modelFile, File outputFile, List<?> dataList) { // 讀取導出excel模板 InputStream inp = null; Workbook wb = null; try { inp = new FileInputStream(modelFile); wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); // 生成導出數據 buildExcelData(sheet, head, dataList); // 導出到文件中 FileOutputStream fileOut = new FileOutputStream(outputFile); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (InvalidFormatException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } /** * 生成導出至Excel文件的數據 * @param sheet 工做區間 * @param excelColumns excel表頭 * @param excelHeadMap excel表頭對應實體屬性 * @param excelHeadConvertMap 須要對數據進行特殊轉換的列 * @param dataList 導入excel報表的數據來源 * @auther <a href="mailto:hubo@feinno.com">hubo</a> * @return void * 2012-4-19 上午09:36:37 */ private void buildExcelData(Sheet sheet, ExcelHead head, List<?> dataList) { List<ExcelColumn> excelColumns = head.getColumns(); Map<String, Map> excelHeadConvertMap = head.getColumnsConvertMap(); // 將表結構轉換成Map Map<Integer, String> excelHeadMap = convertExcelHeadToMap(excelColumns); // 從第幾行開始插入數據 int startRow = head.getRowCount(); int order = 1; //數據循環 for (Object obj : dataList) { Row row = sheet.createRow(startRow++); ////字段循環(經過字段名,拿到對象該字段的值) for (int j = 0; j < excelColumns.size(); j++) { Cell cell = row.createCell(j); cell.setCellType(excelColumns.get(j).getType()); String fieldName = excelHeadMap.get(j); if(fieldName != null) { Object valueObject = BeanUtil.getProperty(obj, fieldName); // 若是存在須要轉換的字段信息,則進行轉換 if(excelHeadConvertMap != null && excelHeadConvertMap.get(fieldName) != null) { valueObject = excelHeadConvertMap.get(fieldName).get(valueObject); } if(valueObject == null) { cell.setCellValue(""); } else if (valueObject instanceof Integer) { cell.setCellValue((Integer)valueObject); } else if (valueObject instanceof String) { cell.setCellValue((String)valueObject); } else if (valueObject instanceof Date) { cell.setCellValue(new JDateTime((Date)valueObject).toString("YYYY-MM-DD")); } else { cell.setCellValue(valueObject.toString()); } } else { cell.setCellValue(order++); } } } }
poi的使用及簡單介紹orm
1.建立工做簿 (WORKBOOK) HSSFWorkbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); 2.建立工做表(SHEET) HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("new sheet"); HSSFSheet sheet2 = wb.createSheet("second sheet"); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); 3.建立單元格(CELL) HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. HSSFRow row = sheet.createRow((short)0); // Create a cell and put a value in it. HSSFCell cell = row.createCell((short)0); cell.setCellValue(1); // Or do it on one line. row.createCell((short)1).setCellValue(1.2); row.createCell((short)2).setCellValue("This is a string"); row.createCell((short)3).setCellValue(true); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();