easyexcel 无模板写入_关于EasyExcel 的一些生成模板,导入导出的使用心得(优化版)...
這里就簡單說了,直接貼代碼
@HeadFontStyle(color =2) 維護表頭第2行的注解
/**
* @author: kuangql@fadada.com
* @date: 2020/11/27 19:53
* @description: TODO
*/
@Data
public class DemoExportEntity {
public static final String bigTitle= "填寫須知: \n" +
"1.第1、2行為固定結(jié)構(gòu),不可更改;以下示例行,導(dǎo)入前請先刪除\n" +
"2.請嚴格按照填寫規(guī)則輸入數(shù)據(jù),不合規(guī)的數(shù)據(jù)無法成功導(dǎo)入 \n" +
"3.測試換行 \n";
/**
*
* org.apache.poi.ss.usermodel.IndexedColors 這個類有所有的顏色枚舉值 2是紅色
* @ HeadFontStyle 注解默認是宋體,黑色,加粗
*
*/
@ExcelProperty(value = {"姓名(必填)"}, index = 0)
@ColumnWidth(30)
@HeadFontStyle(color =2)
private String userName;
@ExcelProperty(value = {"性別(必填)"}, index = 1)
@ColumnWidth(20)
private String userSexName;
@ExcelProperty(value = {"手機號碼(必填)"}, index = 2)
@ColumnWidth(30)
private String userMobile;
@ExcelProperty(value = {"出生年月(必填)"}, index = 3)
@ColumnWidth(30)
private String userBirthday;
@ExcelProperty(value = {"工作單位(必填)"}, index = 4)
@ColumnWidth(20)
private String deptName;
@ExcelProperty(value = {"職務(wù)(必填)"}, index = 5)
@ColumnWidth(20)
private String unitPosition;
@ExcelProperty(value = {"干部類別(必填)"}, index = 6)
@ColumnWidth(20)
private String leaderTypeName;
@ExcelProperty(value = {"用戶狀態(tài)(必填)"}, index = 7)
@ColumnWidth(20)
private String userStatusName;
/**
* 每個模板的首行高度, 換行數(shù)目+2 乘以400
*/
public static int getHeadHeight(){
return (StringUtils.getCharCounts(bigTitle,"\n")+2)*400;
}
}
維護第一行表頭樣式
/**
*
*
* 創(chuàng)建模板
* @author: kuangql@fadada.com
* @date: 2020/11/30 13:48
* @description: TODO
*/
public class CreateTemplateWriteHandler implements SheetWriteHandler {
/**
* 第一行內(nèi)容
*/
private String firstTitle;
/**
* 實體模板類的行高
*/
private int height;
/**
* 實體類 最大的列坐標 從0開始算
*/
private int lastCellIndex;
public CreateTemplateWriteHandler(String firstTitle, int height, int cellCounts) {
this.firstTitle = firstTitle;
this.height = height;
this.lastCellIndex = cellCounts;
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row1 = sheet.createRow(0);
row1.setHeight((short) height);
//字體樣式
Font font = workbook.createFont();
font.setColor((short)2);
Cell cell = row1.createCell(0);
//單元格樣式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setFont(font);
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
//設(shè)置單元格內(nèi)容
cell.setCellValue(firstTitle);
//合并單元格 --> 起始行, 終止行 ,起始列,終止列
sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, lastCellIndex));
}
/**
* 根據(jù)實體類生成模板
*
* @param response
* @param fileName 下 載的文件名,
* @param sheetName sheet名
* @param model 實體類
* @param height 第一行行高
* @param title 第一行表頭內(nèi)容
* @param lastCellIndex 列數(shù)
*/
public static void createTemplate(HttpServletResponse response, String fileName,
String sheetName,
Class> model, int height, String title, int lastCellIndex) {
EasyExcel.write(getOutputStream(fileName, response, ExcelTypeEnum.XLSX), model).
excelType(ExcelTypeEnum.XLSX).sheet(sheetName)
.registerWriteHandler(new CreateTemplateWriteHandler(title, height, lastCellIndex))
.head(model)
.useDefaultStyle(true).relativeHeadRowIndex(1)
.doWrite(null);
}
總結(jié)
以上是生活随笔為你收集整理的easyexcel 无模板写入_关于EasyExcel 的一些生成模板,导入导出的使用心得(优化版)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php curl用法
- 下一篇: Centos6.3下DRBD+Heart