生活随笔
收集整理的這篇文章主要介紹了
easyexcel将对象处理为多列,自增序列
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
概述
主要記錄在開發(fā)中遇到的問題:使用easyexcel導出excel,一般數(shù)據(jù)都是保存在數(shù)據(jù)庫中,如果查詢返回的是一個實體類,且里面有嵌套的實體類對象,這時導出的時候要先對查詢出的數(shù)據(jù)進行業(yè)務邏輯處理,讓它符合導出規(guī)范,但是當數(shù)據(jù)量很大時,容易jvm內(nèi)存,easyexcel(CellDataTypeEnum)只支持基本數(shù)據(jù)類型的導入,如圖1-1所示,可以通過自定義數(shù)據(jù)類型(Converter)進行轉(zhuǎn)換,但是這樣只能返回一種基本的數(shù)據(jù)類型,像這種實體類里面通常會有很多信息,如果都要導出,這種方式也不合適。目前我能想到的方式是通過它提供的各種攔截器處理,大概流程如下:
通過自定義數(shù)據(jù)類型將實體類轉(zhuǎn)換為String類型,然后通過攔截器將字符串拆分,添加對應列的數(shù)據(jù)。
圖1-1 excel內(nèi)部支持的數(shù)據(jù)類型
代碼展示
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Description excel日期轉(zhuǎn)換類 即將Mongo中類型為long的時間戳轉(zhuǎn)化為yyyy-MM-dd HH:mm:ss時間* @author HuangAnting* @date 2022/4/8 10:16
*/
public class DateConverter implements Converter<Long> {@Overridepublic Class<?> supportJavaTypeKey() {//對象屬性類型return Long.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {//CellData屬性類型return CellDataTypeEnum.STRING;}@Overridepublic WriteCellData<?> convertToExcelData(WriteConverterContext<Long> context) throws Exception {//對象屬性轉(zhuǎn)CellDataLong cellValue = context.getValue();new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(cellValue));if (cellValue == null) {return new WriteCellData<>("");} else {return new WriteCellData<>( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(cellValue)));}}
}
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;/*** @Description 自增序號攔截器* @author HuangAnting* @date 2022/4/12 17:27
*/
public class IndexRowWriteHandler implements RowWriteHandler {private static final String FIRST_CELL_NAME = "序號";/*** 序號的樣式,與其他列保持一樣的樣式*/private CellStyle firstCellStyle;private String description;/*** 列號*/private int count = 0;public IndexRowWriteHandler(String description){this.description = description;}@Overridepublic void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {// 每一行首列單元格Cell indexCell = row.createCell(0);if(!isHead){indexCell.setCellValue(++count);try {row.getCell(3).setCellValue(description);}catch (Exception e){}}else {Workbook workbook = writeSheetHolder.getSheet().getWorkbook();firstCellStyle = firstCellStyle(workbook);indexCell.setCellValue(FIRST_CELL_NAME);indexCell.setCellStyle(firstCellStyle);writeSheetHolder.getSheet().setColumnWidth(0, 6 * 256);}}/*** excel首列序號列樣式* @param workbook* @return*/public CellStyle firstCellStyle(Workbook workbook) {CellStyle cellStyle = workbook.createCellStyle();//居中cellStyle.setAlignment(HorizontalAlignment.CENTER);cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 灰色cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//設置邊框cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setBorderLeft(BorderStyle.THIN);cellStyle.setBorderRight(BorderStyle.THIN);cellStyle.setBorderTop(BorderStyle.THIN);//文字Font font = workbook.createFont();font.setBold(Boolean.TRUE);cellStyle.setFont(font);return cellStyle;}
}
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.hat.easyexcel_export.excel.DateConverter;
import com.hat.easyexcel_export.excel.MonPointConverter;
import lombok.Data;/*** @Description 導出實體類* @author HuangAnting* @date 2022/4/12 17:26
*/
@Data
public class Car {/** 名稱*/@ExcelProperty(value = "名稱",index = 1)private String name;/** 價格*/@ExcelProperty(value = "價格",index = 2)private Double price;/** 描述*/@ColumnWidth(15)@ExcelProperty(value = "描述",index = 3)private String description;/** 坐標*/@ExcelProperty(value = "坐標",converter = MonPointConverter.class,index = 4)private MonPoint monPoint;/** 日期*/@ExcelProperty(value = "日期",converter = DateConverter.class)private Long createDate;
}
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.fastjson.JSONObject;
import com.hat.easyexcel_export.entity.MonPoint;
import org.apache.poi.ss.usermodel.*;/*** @Description 行攔截器 將字符串的經(jīng)緯度轉(zhuǎn)換成兩列數(shù)據(jù)* @author HuangAnting* @date 2022/4/12 17:26
*/
public class CustomRowWriteHandler implements RowWriteHandler {/*** 序號的樣式,與其他列保持一樣的樣式*/private CellStyle firstCellStyle;private static final String LON_CELL_NAME = "經(jīng)度";private static final String LAT_CELL_NAME = "緯度";/*** 列號*/@Overridepublic void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean isHead) {Cell cell = row.getCell(4);row.removeCell(cell);Cell lon = row.createCell(4);Cell lat = row.createCell(5);if (!isHead) {String stringCellValue = cell.getStringCellValue();try {MonPoint monPoint = JSONObject.parseObject(stringCellValue, MonPoint.class);lon.setCellValue(monPoint.getLon());lat.setCellValue(monPoint.getLat());}catch (Exception e){}}else {Workbook workbook = writeSheetHolder.getSheet().getWorkbook();firstCellStyle = firstCellStyle(workbook);lon.setCellStyle(firstCellStyle);lat.setCellStyle(firstCellStyle);lon.setCellValue(LON_CELL_NAME);lat.setCellValue(LAT_CELL_NAME);}}/*** excel首列序號列樣式** @param workbook* @return*/public CellStyle firstCellStyle(Workbook workbook) {CellStyle cellStyle = workbook.createCellStyle();//居中cellStyle.setAlignment(HorizontalAlignment.CENTER);cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 灰色cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//設置邊框cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setBorderLeft(BorderStyle.THIN);cellStyle.setBorderRight(BorderStyle.THIN);cellStyle.setBorderTop(BorderStyle.THIN);//文字Font font = workbook.createFont();font.setBold(Boolean.TRUE);cellStyle.setFont(font);return cellStyle;}
}
gitee地址:easyexcel_export
以上僅代表本人對easyexcel的認識和了解,歡迎大家批評指正。
總結(jié)
以上是生活随笔為你收集整理的easyexcel将对象处理为多列,自增序列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。