POI 代码优化

This commit is contained in:
thinkgem
2020-02-19 21:15:22 +08:00
parent 5df57a9f49
commit e8aa8095ce
2 changed files with 51 additions and 46 deletions

View File

@@ -21,13 +21,17 @@ import java.util.Set;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@@ -51,7 +55,7 @@ import com.jeesite.common.utils.excel.annotation.ExcelFields;
/** /**
* 导出Excel文件导出“XLSX”格式支持大数据量导出 @see org.apache.poi.ss.SpreadsheetVersion * 导出Excel文件导出“XLSX”格式支持大数据量导出 @see org.apache.poi.ss.SpreadsheetVersion
* @author ThinkGem * @author ThinkGem
* @version 2018-08-11 * @version 2020-2-19
*/ */
public class ExcelExport implements Closeable{ public class ExcelExport implements Closeable{
@@ -348,24 +352,24 @@ public class ExcelExport implements Closeable{
Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle(); CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);
Font titleFont = wb.createFont(); Font titleFont = wb.createFont();
titleFont.setFontName("Arial"); titleFont.setFontName("Arial");
titleFont.setFontHeightInPoints((short) 16); titleFont.setFontHeightInPoints((short) 16);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); titleFont.setBold(true);
style.setFont(titleFont); style.setFont(titleFont);
styles.put("title", style); styles.put("title", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont(); Font dataFont = wb.createFont();
dataFont.setFontName("Arial"); dataFont.setFontName("Arial");
@@ -375,29 +379,29 @@ public class ExcelExport implements Closeable{
style = wb.createCellStyle(); style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data")); style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_LEFT); style.setAlignment(HorizontalAlignment.LEFT);
styles.put("data1", style); styles.put("data1", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data")); style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_CENTER); style.setAlignment(HorizontalAlignment.CENTER);
styles.put("data2", style); styles.put("data2", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data")); style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_RIGHT); style.setAlignment(HorizontalAlignment.RIGHT);
styles.put("data3", style); styles.put("data3", style);
style = wb.createCellStyle(); style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data")); style.cloneStyleFrom(styles.get("data"));
// style.setWrapText(true); // style.setWrapText(true);
style.setAlignment(CellStyle.ALIGN_CENTER); style.setAlignment(HorizontalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont(); Font headerFont = wb.createFont();
headerFont.setFontName("Arial"); headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10); headerFont.setFontHeightInPoints((short) 10);
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex()); headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont); style.setFont(headerFont);
styles.put("header", style); styles.put("header", style);
@@ -636,28 +640,28 @@ public class ExcelExport implements Closeable{
// } // }
// //
// // 创建一个Sheet表并导入数据 // // 创建一个Sheet表并导入数据
// ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null); // try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){
// for (int i = 0; i < dataList.size(); i++) { //
// Row row = ee.addRow(); // for (int i = 0; i < dataList.size(); i++) {
// for (int j = 0; j < dataList.get(i).size(); j++) { // Row row = ee.addRow();
// ee.addCell(row, j, dataList.get(i).get(j)); // for (int j = 0; j < dataList.get(i).size(); j++) {
// ee.addCell(row, j, dataList.get(i).get(j));
// }
// } // }
// } //
// // // 再创建一个Sheet表并导入数据
// // 再创建一个Sheet表并导入数据 // ee.createSheet("表格2", "表格标题2", headerList, null);
// ee.createSheet("表格2", "表格标题2", headerList, null); // for (int i = 0; i < dataList.size(); i++) {
// for (int i = 0; i < dataList.size(); i++) { // Row row = ee.addRow();
// Row row = ee.addRow(); // for (int j = 0; j < dataList.get(i).size(); j++) {
// for (int j = 0; j < dataList.get(i).size(); j++) { // ee.addCell(row, j, dataList.get(i).get(j)+"2");
// ee.addCell(row, j, dataList.get(i).get(j)+"2"); // }
// } // }
// } //
// // // 输出到文件
// // 输出到文件 // ee.writeFile("target/export.xlsx");
// ee.writeFile("target/export.xlsx");
// //
// // 清理销毁 // }
// ee.dispose();
// //
// log.debug("Export success."); // log.debug("Export success.");
// //

View File

@@ -23,6 +23,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.FormulaEvaluator;
@@ -48,7 +49,7 @@ import com.jeesite.common.utils.excel.annotation.ExcelFields;
/** /**
* 导入Excel文件支持“XLS”和“XLSX”格式 * 导入Excel文件支持“XLS”和“XLSX”格式
* @author ThinkGem * @author ThinkGem
* @version 2018-08-11 * @version 2020-2-19
*/ */
public class ExcelImport implements Closeable { public class ExcelImport implements Closeable {
@@ -262,7 +263,7 @@ public class ExcelImport implements Closeable {
try{ try{
Cell cell = row.getCell(column); Cell cell = row.getCell(column);
if (cell != null){ if (cell != null){
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ if (cell.getCellTypeEnum() == CellType.NUMERIC){
val = cell.getNumericCellValue(); val = cell.getNumericCellValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) { if (HSSFDateUtil.isCellDateFormatted(cell)) {
val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换 val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
@@ -273,36 +274,36 @@ public class ExcelImport implements Closeable {
val = new DecimalFormat("0").format(val); val = new DecimalFormat("0").format(val);
} }
} }
}else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { }else if (cell.getCellTypeEnum() == CellType.STRING) {
val = cell.getStringCellValue(); val = cell.getStringCellValue();
}else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){ }else if (cell.getCellTypeEnum() == CellType.FORMULA){
try { try {
val = cell.getStringCellValue(); val = cell.getStringCellValue();
} catch (Exception e) { } catch (Exception e) {
FormulaEvaluator evaluator = cell.getSheet().getWorkbook() FormulaEvaluator evaluator = cell.getSheet().getWorkbook()
.getCreationHelper().createFormulaEvaluator(); .getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell); evaluator.evaluateFormulaCellEnum(cell);
CellValue cellValue = evaluator.evaluate(cell); CellValue cellValue = evaluator.evaluate(cell);
switch (cellValue.getCellType()) { switch (cellValue.getCellTypeEnum()) {
case Cell.CELL_TYPE_NUMERIC: case NUMERIC:
val = cellValue.getNumberValue(); val = cellValue.getNumberValue();
break; break;
case Cell.CELL_TYPE_STRING: case STRING:
val = cellValue.getStringValue(); val = cellValue.getStringValue();
break; break;
case Cell.CELL_TYPE_BOOLEAN: case BOOLEAN:
val = cellValue.getBooleanValue(); val = cellValue.getBooleanValue();
break; break;
case Cell.CELL_TYPE_ERROR: case ERROR:
val = ErrorEval.getText(cellValue.getErrorValue()); val = ErrorEval.getText(cellValue.getErrorValue());
break; break;
default: default:
val = cell.getCellFormula(); val = cell.getCellFormula();
} }
} }
}else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){ }else if (cell.getCellTypeEnum() == CellType.BOOLEAN){
val = cell.getBooleanCellValue(); val = cell.getBooleanCellValue();
}else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){ }else if (cell.getCellTypeEnum() == CellType.ERROR){
val = cell.getErrorCellValue(); val = cell.getErrorCellValue();
} }
} }