原文地址
通用数据生成 后面不会重复写
| private List<DemoData> data() { |
| List<DemoData> list = ListUtils.newArrayList(); |
| for (int i = 0; i < 10; i++) { |
| DemoData data = new DemoData(); |
| data.setString("字符串" + i); |
| data.setDate(new Date()); |
| data.setDoubleData(0.56); |
| list.add(data); |
| } |
| return list; |
| } |
示例代码
Demo代码地址
最简单的写

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class DemoData { |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| |
| |
| |
| @ExcelIgnore |
| private String ignore; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void simpleWrite() { |
| |
| |
| |
| |
| String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| EasyExcel.write(fileName, DemoData.class) |
| .sheet("模板") |
| .doWrite(() -> { |
| |
| return data(); |
| }); |
| |
| |
| fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); |
| |
| |
| fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| ExcelWriter excelWriter = null; |
| try { |
| excelWriter = EasyExcel.write(fileName, DemoData.class).build(); |
| WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); |
| excelWriter.write(data(), writeSheet); |
| } finally { |
| |
| if (excelWriter != null) { |
| excelWriter.finish(); |
| } |
| } |
| } |
根据参数只导出指令列
excel示例

代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void excludeOrIncludeWrite() { |
| String fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| |
| Set<String> excludeColumnFiledNames = new HashSet<String>(); |
| excludeColumnFiledNames.add("date"); |
| |
| EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板") |
| .doWrite(data()); |
| |
| fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| Set<String> includeColumnFiledNames = new HashSet<String>(); |
| includeColumnFiledNames.add("date"); |
| |
| EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板") |
| .doWrite(data()); |
| } |
指定写入的列

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class IndexData { |
| @ExcelProperty(value = "字符串标题", index = 0) |
| private String string; |
| @ExcelProperty(value = "日期标题", index = 1) |
| private Date date; |
| |
| |
| |
| @ExcelProperty(value = "数字标题", index = 3) |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| @Test |
| public void indexWrite() { |
| String fileName = TestFileUtil.getPath() + "indexWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, IndexData.class).sheet("模板").doWrite(data()); |
| } |
复杂头写入
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class ComplexHeadData { |
| @ExcelProperty({"主标题", "字符串标题"}) |
| private String string; |
| @ExcelProperty({"主标题", "日期标题"}) |
| private Date date; |
| @ExcelProperty({"主标题", "数字标题"}) |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| @Test |
| public void complexHeadWrite() { |
| String fileName = TestFileUtil.getPath() + "complexHeadWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, ComplexHeadData.class).sheet("模板").doWrite(data()); |
| } |
重复多次写入
excel示例

代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void repeatedWrite() { |
| |
| String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; |
| ExcelWriter excelWriter = null; |
| try { |
| |
| excelWriter = EasyExcel.write(fileName, DemoData.class).build(); |
| |
| WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); |
| |
| for (int i = 0; i < 5; i++) { |
| |
| List<DemoData> data = data(); |
| excelWriter.write(data, writeSheet); |
| } |
| } finally { |
| |
| if (excelWriter != null) { |
| excelWriter.finish(); |
| } |
| } |
| |
| |
| fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; |
| try { |
| |
| excelWriter = EasyExcel.write(fileName, DemoData.class).build(); |
| |
| for (int i = 0; i < 5; i++) { |
| |
| WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build(); |
| |
| List<DemoData> data = data(); |
| excelWriter.write(data, writeSheet); |
| } |
| } finally { |
| |
| if (excelWriter != null) { |
| excelWriter.finish(); |
| } |
| } |
| |
| |
| fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; |
| try { |
| |
| excelWriter = EasyExcel.write(fileName).build(); |
| |
| for (int i = 0; i < 5; i++) { |
| |
| WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).head(DemoData.class).build(); |
| |
| List<DemoData> data = data(); |
| excelWriter.write(data, writeSheet); |
| } |
| } finally { |
| |
| if (excelWriter != null) { |
| excelWriter.finish(); |
| } |
| } |
| } |
日期、数字或者自定义格式转换
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class ConverterData { |
| |
| |
| |
| @ExcelProperty(value = "字符串标题", converter = CustomStringStringConverter.class) |
| private String string; |
| |
| |
| |
| @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") |
| @ExcelProperty("日期标题") |
| private Date date; |
| |
| |
| |
| @NumberFormat("#.##%") |
| @ExcelProperty(value = "数字标题") |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| @Test |
| public void converterWrite() { |
| String fileName = TestFileUtil.getPath() + "converterWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data()); |
| } |
图片导出
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| @ContentRowHeight(100) |
| @ColumnWidth(100 / 8) |
| public class ImageDemoData { |
| private File file; |
| private InputStream inputStream; |
| |
| |
| |
| @ExcelProperty(converter = StringImageConverter.class) |
| private String string; |
| private byte[] byteArray; |
| |
| |
| |
| |
| |
| private URL url; |
| |
| |
| |
| |
| |
| |
| private WriteCellData<Void> writeCellDataFile; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void imageWrite() throws Exception { |
| String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; |
| try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) { |
| List<ImageDemoData> list = ListUtils.newArrayList(); |
| ImageDemoData imageDemoData = new ImageDemoData(); |
| list.add(imageDemoData); |
| |
| imageDemoData.setByteArray(FileUtils.readFileToByteArray(new File(imagePath))); |
| imageDemoData.setFile(new File(imagePath)); |
| imageDemoData.setString(imagePath); |
| imageDemoData.setInputStream(inputStream); |
| imageDemoData.setUrl(new URL( |
| "https://raw.githubusercontent.com/alibaba/easyexcel/master/src/test/resources/converter/img.jpg")); |
| |
| |
| |
| |
| |
| |
| WriteCellData<Void> writeCellData = new WriteCellData<>(); |
| imageDemoData.setWriteCellDataFile(writeCellData); |
| |
| writeCellData.setType(CellDataTypeEnum.STRING); |
| writeCellData.setStringValue("额外的放一些文字"); |
| |
| |
| List<ImageData> imageDataList = new ArrayList<>(); |
| ImageData imageData = new ImageData(); |
| imageDataList.add(imageData); |
| writeCellData.setImageDataList(imageDataList); |
| |
| imageData.setImage(FileUtils.readFileToByteArray(new File(imagePath))); |
| |
| imageData.setImageType(ImageType.PICTURE_TYPE_PNG); |
| |
| |
| |
| imageData.setTop(5); |
| imageData.setRight(40); |
| imageData.setBottom(5); |
| imageData.setLeft(5); |
| |
| |
| imageData = new ImageData(); |
| imageDataList.add(imageData); |
| writeCellData.setImageDataList(imageDataList); |
| imageData.setImage(FileUtils.readFileToByteArray(new File(imagePath))); |
| imageData.setImageType(ImageType.PICTURE_TYPE_PNG); |
| imageData.setTop(5); |
| imageData.setRight(5); |
| imageData.setBottom(5); |
| imageData.setLeft(50); |
| |
| |
| imageData.setRelativeFirstRowIndex(0); |
| imageData.setRelativeFirstColumnIndex(0); |
| imageData.setRelativeLastRowIndex(0); |
| |
| |
| imageData.setRelativeLastColumnIndex(1); |
| |
| |
| EasyExcel.write(fileName, ImageDemoData.class).sheet().doWrite(list); |
| } |
| } |
超链接、备注、公式、指定单个单元格的样式、单个单元格多种样式
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class WriteCellDemoData { |
| |
| |
| |
| |
| |
| private WriteCellData<String> hyperlink; |
| |
| |
| |
| |
| |
| |
| private WriteCellData<String> commentData; |
| |
| |
| |
| |
| |
| |
| private WriteCellData<String> formulaData; |
| |
| |
| |
| |
| |
| |
| private WriteCellData<String> writeCellStyle; |
| |
| |
| |
| |
| |
| |
| private WriteCellData<String> richText; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void writeCellDataWrite() { |
| String fileName = TestFileUtil.getPath() + "writeCellDataWrite" + System.currentTimeMillis() + ".xlsx"; |
| WriteCellDemoData writeCellDemoData = new WriteCellDemoData(); |
| |
| |
| WriteCellData<String> hyperlink = new WriteCellData<>("官方网站"); |
| writeCellDemoData.setHyperlink(hyperlink); |
| HyperlinkData hyperlinkData = new HyperlinkData(); |
| hyperlink.setHyperlinkData(hyperlinkData); |
| hyperlinkData.setAddress("https://github.com/alibaba/easyexcel"); |
| hyperlinkData.setHyperlinkType(HyperlinkType.URL); |
| |
| |
| WriteCellData<String> comment = new WriteCellData<>("备注的单元格信息"); |
| writeCellDemoData.setCommentData(comment); |
| CommentData commentData = new CommentData(); |
| comment.setCommentData(commentData); |
| commentData.setAuthor("Jiaju Zhuang"); |
| commentData.setRichTextStringData(new RichTextStringData("这是一个备注")); |
| |
| commentData.setRelativeLastColumnIndex(1); |
| commentData.setRelativeLastRowIndex(1); |
| |
| |
| WriteCellData<String> formula = new WriteCellData<>(); |
| writeCellDemoData.setFormulaData(formula); |
| FormulaData formulaData = new FormulaData(); |
| formula.setFormulaData(formulaData); |
| |
| |
| formulaData.setFormulaValue("REPLACE(123456789,1,1,2)"); |
| |
| |
| WriteCellData<String> writeCellStyle = new WriteCellData<>("单元格样式"); |
| writeCellStyle.setType(CellDataTypeEnum.STRING); |
| writeCellDemoData.setWriteCellStyle(writeCellStyle); |
| WriteCellStyle writeCellStyleData = new WriteCellStyle(); |
| writeCellStyle.setWriteCellStyle(writeCellStyleData); |
| |
| writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
| |
| writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex()); |
| |
| |
| WriteCellData<String> richTest = new WriteCellData<>(); |
| richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING); |
| writeCellDemoData.setRichText(richTest); |
| RichTextStringData richTextStringData = new RichTextStringData(); |
| richTest.setRichTextStringDataValue(richTextStringData); |
| richTextStringData.setTextString("红色绿色默认"); |
| |
| WriteFont writeFont = new WriteFont(); |
| writeFont.setColor(IndexedColors.RED.getIndex()); |
| richTextStringData.applyFont(0, 2, writeFont); |
| |
| writeFont = new WriteFont(); |
| writeFont.setColor(IndexedColors.GREEN.getIndex()); |
| richTextStringData.applyFont(2, 4, writeFont); |
| |
| List<WriteCellDemoData> data = new ArrayList<>(); |
| data.add(writeCellDemoData); |
| EasyExcel.write(fileName, WriteCellDemoData.class).inMemory(true).sheet("模板").doWrite(data); |
| } |
根据模板写入
模版示例

excel示例

代码
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void templateWrite() { |
| String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; |
| String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data()); |
| } |
列宽、行高
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| @ContentRowHeight(10) |
| @HeadRowHeight(20) |
| @ColumnWidth(25) |
| public class WidthAndHeightData { |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| |
| |
| |
| @ColumnWidth(50) |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| @Test |
| public void widthAndHeightWrite() { |
| String fileName = TestFileUtil.getPath() + "widthAndHeightWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, WidthAndHeightData.class).sheet("模板").doWrite(data()); |
| } |
注解形式自定义样式
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| |
| @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10) |
| |
| @HeadFontStyle(fontHeightInPoints = 20) |
| |
| @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17) |
| |
| @ContentFontStyle(fontHeightInPoints = 20) |
| public class DemoStyleData { |
| |
| @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 14) |
| |
| @HeadFontStyle(fontHeightInPoints = 30) |
| |
| @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40) |
| |
| @ContentFontStyle(fontHeightInPoints = 30) |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| } |
| |
| |
| |
| |
| @Data |
| |
| @HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10) |
| |
| @HeadFontStyle(fontHeightInPoints = 20) |
| |
| @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17) |
| |
| @ContentFontStyle(fontHeightInPoints = 20) |
| public class DemoStyleData { |
| |
| @HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 14) |
| |
| @HeadFontStyle(fontHeightInPoints = 30) |
| |
| @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40) |
| |
| @ContentFontStyle(fontHeightInPoints = 30) |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void annotationStyleWrite() { |
| String fileName = TestFileUtil.getPath() + "annotationStyleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, DemoStyleData.class).sheet("模板").doWrite(data()); |
| } |
自定义样式
excel示例

代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void handlerStyleWrite() { |
| |
| |
| |
| String fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
| |
| headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); |
| WriteFont headWriteFont = new WriteFont(); |
| headWriteFont.setFontHeightInPoints((short)20); |
| headWriteCellStyle.setWriteFont(headWriteFont); |
| |
| WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); |
| |
| contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
| |
| contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); |
| WriteFont contentWriteFont = new WriteFont(); |
| |
| contentWriteFont.setFontHeightInPoints((short)20); |
| contentWriteCellStyle.setWriteFont(contentWriteFont); |
| |
| HorizontalCellStyleStrategy horizontalCellStyleStrategy = |
| new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); |
| |
| |
| EasyExcel.write(fileName, DemoData.class) |
| .registerWriteHandler(horizontalCellStyleStrategy) |
| .sheet("模板") |
| .doWrite(data()); |
| |
| |
| |
| fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx"; |
| EasyExcel.write(fileName, DemoData.class) |
| .registerWriteHandler(new CellWriteHandler() { |
| @Override |
| public void afterCellDispose(CellWriteHandlerContext context) { |
| |
| |
| if (BooleanUtils.isNotTrue(context.getHead())) { |
| |
| |
| WriteCellData<?> cellData = context.getFirstCellData(); |
| |
| |
| |
| |
| WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); |
| writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); |
| |
| writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
| |
| |
| } |
| } |
| }).sheet("模板") |
| .doWrite(data()); |
| |
| |
| |
| |
| |
| fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx"; |
| EasyExcel.write(fileName, DemoData.class) |
| .registerWriteHandler(new CellWriteHandler() { |
| @Override |
| public void afterCellDispose(CellWriteHandlerContext context) { |
| |
| |
| if (BooleanUtils.isNotTrue(context.getHead())) { |
| Cell cell = context.getCell(); |
| |
| Workbook workbook = context.getWriteWorkbookHolder().getWorkbook(); |
| |
| |
| CellStyle cellStyle = workbook.createCellStyle(); |
| cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); |
| |
| cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| cell.setCellStyle(cellStyle); |
| |
| |
| |
| |
| |
| context.getFirstCellData().setWriteCellStyle(null); |
| } |
| } |
| }).sheet("模板") |
| .doWrite(data()); |
| } |
合并单元格
excel示例

方法一
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| |
| |
| public class DemoMergeData { |
| |
| @ContentLoopMerge(eachRow = 2) |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题") |
| private Date date; |
| @ExcelProperty("数字标题") |
| private Double doubleData; |
| } |
方法二
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void mergeWrite() { |
| |
| String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| EasyExcel.write(fileName, DemoMergeData.class).sheet("模板").doWrite(data()); |
| |
| |
| fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0); |
| |
| EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data()); |
| } |
使用table去写入
excel示例

代码
| |
| |
| |
| |
| |
| @Test |
| public void tableWrite() { |
| String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| ExcelWriter excelWriter = null; |
| try { |
| excelWriter = EasyExcel.write(fileName, DemoData.class).build(); |
| |
| WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build(); |
| |
| WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build(); |
| WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build(); |
| |
| excelWriter.write(data(), writeSheet, writeTable0); |
| |
| excelWriter.write(data(), writeSheet, writeTable1); |
| } finally { |
| |
| if (excelWriter != null) { |
| excelWriter.finish(); |
| } |
| } |
| } |
动态头,实时生成头写入
excel示例

代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void dynamicHeadWrite() { |
| String fileName = TestFileUtil.getPath() + "dynamicHeadWrite" + System.currentTimeMillis() + ".xlsx"; |
| EasyExcel.write(fileName) |
| |
| .head(head()).sheet("模板") |
| |
| .doWrite(data()); |
| } |
| |
| private List<List<String>> head() { |
| List<List<String>> list = new ArrayList<List<String>>(); |
| List<String> head0 = new ArrayList<String>(); |
| head0.add("字符串" + System.currentTimeMillis()); |
| List<String> head1 = new ArrayList<String>(); |
| head1.add("数字" + System.currentTimeMillis()); |
| List<String> head2 = new ArrayList<String>(); |
| head2.add("日期" + System.currentTimeMillis()); |
| list.add(head0); |
| list.add(head1); |
| list.add(head2); |
| return list; |
| } |
自动列宽(不太精确)
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class LongestMatchColumnWidthData { |
| @ExcelProperty("字符串标题") |
| private String string; |
| @ExcelProperty("日期标题很长日期标题很长日期标题很长很长") |
| private Date date; |
| @ExcelProperty("数字") |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void longestMatchColumnWidthWrite() { |
| String fileName = |
| TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, LongestMatchColumnWidthData.class) |
| .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong()); |
| } |
| |
| private List<LongestMatchColumnWidthData> dataLong() { |
| List<LongestMatchColumnWidthData> list = new ArrayList<LongestMatchColumnWidthData>(); |
| for (int i = 0; i < 10; i++) { |
| LongestMatchColumnWidthData data = new LongestMatchColumnWidthData(); |
| data.setString("测试很长的字符串测试很长的字符串测试很长的字符串" + i); |
| data.setDate(new Date()); |
| data.setDoubleData(1000000000000.0); |
| list.add(data); |
| } |
| return list; |
| } |
自定义拦截器(上面几点都不符合但是要对单元格进行操作的参照这个)
excel示例

自定义拦截器
| |
| |
| |
| |
| |
| @Slf4j |
| public class CustomCellWriteHandler implements CellWriteHandler { |
| |
| @Override |
| public void afterCellDispose(CellWriteHandlerContext context) { |
| Cell cell = context.getCell(); |
| |
| log.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex()); |
| if (BooleanUtils.isTrue(context.getHead()) && cell.getColumnIndex() == 0) { |
| CreationHelper createHelper = context.getWriteSheetHolder().getSheet().getWorkbook().getCreationHelper(); |
| Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL); |
| hyperlink.setAddress("https://github.com/alibaba/easyexcel"); |
| cell.setHyperlink(hyperlink); |
| } |
| } |
| |
| } |
| |
| |
| |
| |
| |
| @Slf4j |
| public class CustomSheetWriteHandler implements SheetWriteHandler { |
| |
| @Override |
| public void afterSheetCreate(SheetWriteHandlerContext context) { |
| log.info("第{}个Sheet写入成功。", context.getWriteSheetHolder().getSheetNo()); |
| |
| |
| CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 2, 0, 0); |
| DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper(); |
| DataValidationConstraint constraint = helper.createExplicitListConstraint(new String[] {"测试1", "测试2"}); |
| DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList); |
| context.getWriteSheetHolder().getSheet().addValidationData(dataValidation); |
| } |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void customHandlerWrite() { |
| String fileName = TestFileUtil.getPath() + "customHandlerWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CustomSheetWriteHandler()) |
| .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data()); |
| } |
插入批注
excel示例

定义拦截器
| |
| |
| |
| |
| |
| @Slf4j |
| public class CommentWriteHandler implements RowWriteHandler { |
| |
| @Override |
| public void afterRowDispose(RowWriteHandlerContext context) { |
| if (BooleanUtils.isTrue(context.getHead())) { |
| Sheet sheet = context.getWriteSheetHolder().getSheet(); |
| Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch(); |
| |
| Comment comment = |
| drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short)1, 0, (short)2, 1)); |
| |
| comment.setString(new XSSFRichTextString("创建批注!")); |
| |
| sheet.getRow(0).getCell(1).setCellComment(comment); |
| } |
| } |
| |
| } |
| |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void commentWrite() { |
| String fileName = TestFileUtil.getPath() + "commentWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| |
| EasyExcel.write(fileName, DemoData.class).inMemory(Boolean.TRUE).registerWriteHandler(new CommentWriteHandler()) |
| .sheet("模板").doWrite(data()); |
| } |
可变标题处理(包括标题国际化等)
excel示例

对象
| @Getter |
| @Setter |
| @EqualsAndHashCode |
| public class ConverterData { |
| |
| |
| |
| @ExcelProperty(value = "字符串标题", converter = CustomStringStringConverter.class) |
| private String string; |
| |
| |
| |
| @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") |
| @ExcelProperty("日期标题") |
| private Date date; |
| |
| |
| |
| @NumberFormat("#.##%") |
| @ExcelProperty(value = "数字标题") |
| private Double doubleData; |
| } |
| @Data |
| public class ConverterData { |
| |
| |
| |
| @ExcelProperty(value = "字符串标题", converter = CustomStringStringConverter.class) |
| private String string; |
| |
| |
| |
| @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") |
| @ExcelProperty("日期标题") |
| private Date date; |
| |
| |
| |
| @NumberFormat("#.##%") |
| @ExcelProperty(value = "数字标题") |
| private Double doubleData; |
| } |
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void variableTitleWrite() { |
| |
| String fileName = TestFileUtil.getPath() + "variableTitleWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName, ConverterData.class).head(variableTitleHead()).sheet("模板").doWrite(data()); |
| } |
| |
| private List<List<String>> variableTitleHead() { |
| List<List<String>> list = ListUtils.newArrayList(); |
| List<String> head0 = ListUtils.newArrayList(); |
| head0.add("string" + System.currentTimeMillis()); |
| List<String> head1 = ListUtils.newArrayList(); |
| head1.add("number" + System.currentTimeMillis()); |
| List<String> head2 = ListUtils.newArrayList(); |
| head2.add("date" + System.currentTimeMillis()); |
| list.add(head0); |
| list.add(head1); |
| list.add(head2); |
| return list; |
| } private List<List<String>> variableTitleHead() { |
| List<List<String>> list = new ArrayList<>(); |
| List<String> head0 = new ArrayList<>(); |
| head0.add("string" + System.currentTimeMillis()); |
| List<String> head1 = new ArrayList<>(); |
| head1.add("number" + System.currentTimeMillis()); |
| List<String> head2 = new ArrayList<>(); |
| head2.add("date" + System.currentTimeMillis()); |
| list.add(head0); |
| list.add(head1); |
| list.add(head2); |
| return list; |
| } |
不创建对象的写
excel示例

代码
| |
| |
| |
| @Test |
| public void noModelWrite() { |
| |
| String fileName = TestFileUtil.getPath() + "noModelWrite" + System.currentTimeMillis() + ".xlsx"; |
| |
| EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); |
| } |
| |
| private List<List<String>> head() { |
| List<List<String>> list = ListUtils.newArrayList(); |
| List<String> head0 = ListUtils.newArrayList(); |
| head0.add("字符串" + System.currentTimeMillis()); |
| List<String> head1 = ListUtils.newArrayList(); |
| head1.add("数字" + System.currentTimeMillis()); |
| List<String> head2 = ListUtils.newArrayList(); |
| head2.add("日期" + System.currentTimeMillis()); |
| list.add(head0); |
| list.add(head1); |
| list.add(head2); |
| return list; |
| } |
| |
| private List<List<Object>> dataList() { |
| List<List<Object>> list = ListUtils.newArrayList(); |
| for (int i = 0; i < 10; i++) { |
| List<Object> data = ListUtils.newArrayList(); |
| data.add("字符串" + i); |
| data.add(new Date()); |
| data.add(0.56); |
| list.add(data); |
| } |
| return list; |
| } |
web中的写
Demo地址
代码
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @GetMapping("download") |
| public void download(HttpServletResponse response) throws IOException { |
| |
| response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| response.setCharacterEncoding("utf-8"); |
| |
| String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); |
| response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()); |
| } |
web中的写并且失败的时候返回json
代码
| |
| |
| |
| |
| |
| @GetMapping("downloadFailedUsingJson") |
| public void downloadFailedUsingJson(HttpServletResponse response) throws IOException { |
| |
| try { |
| response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| response.setCharacterEncoding("utf-8"); |
| |
| String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); |
| response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| |
| EasyExcel.write(response.getOutputStream(), DownloadData.class).autoCloseStream(Boolean.FALSE).sheet("模板") |
| .doWrite(data()); |
| } catch (Exception e) { |
| |
| response.reset(); |
| response.setContentType("application/json"); |
| response.setCharacterEncoding("utf-8"); |
| Map<String, String> map = MapUtils.newHashMap(); |
| map.put("status", "failure"); |
| map.put("message", "下载文件失败" + e.getMessage()); |
| response.getWriter().println(JSON.toJSONString(map)); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)