POI 代码优化
This commit is contained in:
@@ -21,13 +21,17 @@ import java.util.Set;
|
||||
|
||||
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.CellStyle;
|
||||
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.HorizontalAlignment;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
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.util.CellRangeAddress;
|
||||
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)
|
||||
* @author ThinkGem
|
||||
* @version 2018-08-11
|
||||
* @version 2020-2-19
|
||||
*/
|
||||
public class ExcelExport implements Closeable{
|
||||
|
||||
@@ -348,24 +352,24 @@ public class ExcelExport implements Closeable{
|
||||
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
|
||||
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(CellStyle.ALIGN_CENTER);
|
||||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
Font titleFont = wb.createFont();
|
||||
titleFont.setFontName("Arial");
|
||||
titleFont.setFontHeightInPoints((short) 16);
|
||||
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||
titleFont.setBold(true);
|
||||
style.setFont(titleFont);
|
||||
styles.put("title", style);
|
||||
|
||||
style = wb.createCellStyle();
|
||||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
||||
style.setBorderRight(CellStyle.BORDER_THIN);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
||||
style.setBorderLeft(CellStyle.BORDER_THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
||||
style.setBorderTop(CellStyle.BORDER_THIN);
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
||||
style.setBorderBottom(CellStyle.BORDER_THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
||||
Font dataFont = wb.createFont();
|
||||
dataFont.setFontName("Arial");
|
||||
@@ -375,29 +379,29 @@ public class ExcelExport implements Closeable{
|
||||
|
||||
style = wb.createCellStyle();
|
||||
style.cloneStyleFrom(styles.get("data"));
|
||||
style.setAlignment(CellStyle.ALIGN_LEFT);
|
||||
style.setAlignment(HorizontalAlignment.LEFT);
|
||||
styles.put("data1", style);
|
||||
|
||||
style = wb.createCellStyle();
|
||||
style.cloneStyleFrom(styles.get("data"));
|
||||
style.setAlignment(CellStyle.ALIGN_CENTER);
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
styles.put("data2", style);
|
||||
|
||||
style = wb.createCellStyle();
|
||||
style.cloneStyleFrom(styles.get("data"));
|
||||
style.setAlignment(CellStyle.ALIGN_RIGHT);
|
||||
style.setAlignment(HorizontalAlignment.RIGHT);
|
||||
styles.put("data3", style);
|
||||
|
||||
style = wb.createCellStyle();
|
||||
style.cloneStyleFrom(styles.get("data"));
|
||||
// style.setWrapText(true);
|
||||
style.setAlignment(CellStyle.ALIGN_CENTER);
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
||||
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
||||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
Font headerFont = wb.createFont();
|
||||
headerFont.setFontName("Arial");
|
||||
headerFont.setFontHeightInPoints((short) 10);
|
||||
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||
headerFont.setBold(true);
|
||||
headerFont.setColor(IndexedColors.WHITE.getIndex());
|
||||
style.setFont(headerFont);
|
||||
styles.put("header", style);
|
||||
@@ -636,28 +640,28 @@ public class ExcelExport implements Closeable{
|
||||
// }
|
||||
//
|
||||
// // 创建一个Sheet表,并导入数据
|
||||
// ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null);
|
||||
// for (int i = 0; i < dataList.size(); i++) {
|
||||
// Row row = ee.addRow();
|
||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||
// ee.addCell(row, j, dataList.get(i).get(j));
|
||||
// try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){
|
||||
//
|
||||
// for (int i = 0; i < dataList.size(); i++) {
|
||||
// Row row = ee.addRow();
|
||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||
// ee.addCell(row, j, dataList.get(i).get(j));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 再创建一个Sheet表,并导入数据
|
||||
// ee.createSheet("表格2", "表格标题2", headerList, null);
|
||||
// for (int i = 0; i < dataList.size(); i++) {
|
||||
// Row row = ee.addRow();
|
||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||
// ee.addCell(row, j, dataList.get(i).get(j)+"2");
|
||||
//
|
||||
// // 再创建一个Sheet表,并导入数据
|
||||
// ee.createSheet("表格2", "表格标题2", headerList, null);
|
||||
// for (int i = 0; i < dataList.size(); i++) {
|
||||
// Row row = ee.addRow();
|
||||
// for (int j = 0; j < dataList.get(i).size(); j++) {
|
||||
// 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.");
|
||||
//
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.ss.formula.eval.ErrorEval;
|
||||
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.DateUtil;
|
||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||
@@ -48,7 +49,7 @@ import com.jeesite.common.utils.excel.annotation.ExcelFields;
|
||||
/**
|
||||
* 导入Excel文件(支持“XLS”和“XLSX”格式)
|
||||
* @author ThinkGem
|
||||
* @version 2018-08-11
|
||||
* @version 2020-2-19
|
||||
*/
|
||||
public class ExcelImport implements Closeable {
|
||||
|
||||
@@ -262,7 +263,7 @@ public class ExcelImport implements Closeable {
|
||||
try{
|
||||
Cell cell = row.getCell(column);
|
||||
if (cell != null){
|
||||
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
|
||||
if (cell.getCellTypeEnum() == CellType.NUMERIC){
|
||||
val = cell.getNumericCellValue();
|
||||
if (HSSFDateUtil.isCellDateFormatted(cell)) {
|
||||
val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
|
||||
@@ -273,36 +274,36 @@ public class ExcelImport implements Closeable {
|
||||
val = new DecimalFormat("0").format(val);
|
||||
}
|
||||
}
|
||||
}else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
|
||||
}else if (cell.getCellTypeEnum() == CellType.STRING) {
|
||||
val = cell.getStringCellValue();
|
||||
}else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
|
||||
}else if (cell.getCellTypeEnum() == CellType.FORMULA){
|
||||
try {
|
||||
val = cell.getStringCellValue();
|
||||
} catch (Exception e) {
|
||||
FormulaEvaluator evaluator = cell.getSheet().getWorkbook()
|
||||
.getCreationHelper().createFormulaEvaluator();
|
||||
evaluator.evaluateFormulaCell(cell);
|
||||
evaluator.evaluateFormulaCellEnum(cell);
|
||||
CellValue cellValue = evaluator.evaluate(cell);
|
||||
switch (cellValue.getCellType()) {
|
||||
case Cell.CELL_TYPE_NUMERIC:
|
||||
switch (cellValue.getCellTypeEnum()) {
|
||||
case NUMERIC:
|
||||
val = cellValue.getNumberValue();
|
||||
break;
|
||||
case Cell.CELL_TYPE_STRING:
|
||||
case STRING:
|
||||
val = cellValue.getStringValue();
|
||||
break;
|
||||
case Cell.CELL_TYPE_BOOLEAN:
|
||||
case BOOLEAN:
|
||||
val = cellValue.getBooleanValue();
|
||||
break;
|
||||
case Cell.CELL_TYPE_ERROR:
|
||||
case ERROR:
|
||||
val = ErrorEval.getText(cellValue.getErrorValue());
|
||||
break;
|
||||
default:
|
||||
val = cell.getCellFormula();
|
||||
}
|
||||
}
|
||||
}else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
|
||||
}else if (cell.getCellTypeEnum() == CellType.BOOLEAN){
|
||||
val = cell.getBooleanCellValue();
|
||||
}else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){
|
||||
}else if (cell.getCellTypeEnum() == CellType.ERROR){
|
||||
val = cell.getErrorCellValue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user