|
| 1 | +package io.github.yyfcode.fastexcel.demo.controller; |
| 2 | + |
| 3 | +import javax.servlet.ServletOutputStream; |
| 4 | +import javax.servlet.http.HttpServletResponse; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import io.github.yyfcode.fastexcel.builder.WorkbookBuilder; |
| 8 | +import io.swagger.v3.oas.annotations.Operation; |
| 9 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 10 | +import org.apache.poi.ss.usermodel.HorizontalAlignment; |
| 11 | +import org.apache.poi.ss.usermodel.IndexedColors; |
| 12 | +import org.apache.poi.ss.usermodel.VerticalAlignment; |
| 13 | +import org.apache.poi.ss.usermodel.Workbook; |
| 14 | +import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| 15 | +import org.springframework.http.MediaType; |
| 16 | +import org.springframework.stereotype.Controller; |
| 17 | +import org.springframework.web.bind.annotation.PostMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Justice |
| 22 | + */ |
| 23 | +@Tag(name = "test excel write") |
| 24 | +@Controller |
| 25 | +@RequestMapping("excelWrite") |
| 26 | +public class ExcelWriteController { |
| 27 | + |
| 28 | + @Operation(summary = "Simple write") |
| 29 | + @PostMapping(value = "simpleWrite", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
| 30 | + public void simpleWrite(HttpServletResponse response) throws Exception { |
| 31 | + Workbook workbook = new WorkbookBuilder(new SXSSFWorkbook()) |
| 32 | + .createSheet("Sheet 1") |
| 33 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 34 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 35 | + .createSheet("Sheet 2") |
| 36 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 37 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 38 | + .createSheet("Sheet 3") |
| 39 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 40 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 41 | + .build(); |
| 42 | + try (ServletOutputStream out = response.getOutputStream()) { |
| 43 | + response.setHeader("Content-disposition", "attachment; filename=simpleWrite.xlsx"); |
| 44 | + workbook.write(out); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Operation(summary = "Default cell style") |
| 49 | + @PostMapping(value = "defaultCellStyle", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
| 50 | + public void defaultCellStyle(HttpServletResponse response) throws Exception { |
| 51 | + // Default cell style |
| 52 | + Workbook workbook = WorkbookBuilder.builder() |
| 53 | + .createSheet("Sheet 1") |
| 54 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 55 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 56 | + .createSheet("Sheet 2") |
| 57 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 58 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 59 | + .createSheet("Sheet 3") |
| 60 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 61 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 62 | + .build(); |
| 63 | + try (ServletOutputStream out = response.getOutputStream()) { |
| 64 | + response.setHeader("Content-disposition", "attachment; filename=defaultCellStyle.xlsx"); |
| 65 | + workbook.write(out); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Operation(summary = "Custom cell style") |
| 70 | + @PostMapping(value = "customCellStyle", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
| 71 | + public void customCellStyle(HttpServletResponse response) throws Exception { |
| 72 | + Workbook workbook = new WorkbookBuilder(new SXSSFWorkbook()) |
| 73 | + // Set global cell style |
| 74 | + .matchingAll() |
| 75 | + .setFontHeight(20) |
| 76 | + .setFontName("Arial") |
| 77 | + .setVerticalAlignment(VerticalAlignment.CENTER) |
| 78 | + .setAlignment(HorizontalAlignment.CENTER) |
| 79 | + .addCellStyle() |
| 80 | + .createSheet("Sheet 1") |
| 81 | + // Set cell style of Sheet 1 |
| 82 | + .matchingAll() |
| 83 | + .setFontColor(IndexedColors.RED) |
| 84 | + .addCellStyle() |
| 85 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 86 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 87 | + .createSheet("Sheet 2") |
| 88 | + // Set cell style of Sheet 2 |
| 89 | + .matchingAll() |
| 90 | + .setFontColor(IndexedColors.BLUE) |
| 91 | + .addCellStyle() |
| 92 | + // Set cell style of Sheet 2 and row number > 0 |
| 93 | + .matchingCell(cell -> cell.getRowIndex() > 0) |
| 94 | + .setFillBackgroundColor(IndexedColors.GREY_25_PERCENT) |
| 95 | + .setFillForegroundColor(IndexedColors.GREY_25_PERCENT) |
| 96 | + .addCellStyle() |
| 97 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 98 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 99 | + .createSheet("Sheet 3") |
| 100 | + // Set cell style of Sheet 3 |
| 101 | + .matchingAll() |
| 102 | + .setFontColor(IndexedColors.GREEN) |
| 103 | + .setItalic(true) |
| 104 | + .addCellStyle() |
| 105 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 106 | + .createRow(new Object[]{"cell1", "cell2", "cell3"}) |
| 107 | + .build(); |
| 108 | + try (ServletOutputStream out = response.getOutputStream()) { |
| 109 | + response.setHeader("Content-disposition", "attachment; filename=customCellStyle.xlsx"); |
| 110 | + workbook.write(out); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Operation(summary = "Custom column style") |
| 115 | + @PostMapping(value = "customColumnStyle", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
| 116 | + public void customColumnStyle(HttpServletResponse response) throws Exception { |
| 117 | + Workbook workbook = WorkbookBuilder.builder() |
| 118 | + .setDefaultColumnWidth(40) |
| 119 | + .createSheet("Sheet 1") |
| 120 | + .matchingColumn(0) |
| 121 | + .setDataFormat("yyyy-MM-dd") |
| 122 | + .addCellStyle() |
| 123 | + .matchingColumn(1) |
| 124 | + .setDataFormat("#.##00") |
| 125 | + .addCellStyle() |
| 126 | + .matchingColumn(2) |
| 127 | + .setDataFormat("[=1]\"male\";[=2]\"female\"") |
| 128 | + .addCellStyle() |
| 129 | + .createRow(new Object[]{new Date(), 22.121f, 1}) |
| 130 | + .createRow(new Object[]{new Date(), 123.1d, 2}) |
| 131 | + .build(); |
| 132 | + try (ServletOutputStream out = response.getOutputStream()) { |
| 133 | + response.setHeader("Content-disposition", "attachment; filename=customColumnStyle.xlsx"); |
| 134 | + workbook.write(out); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Operation(summary = "CellRange merge") |
| 139 | + @PostMapping(value = "cellRangeMerge", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
| 140 | + public void cellRangeMerge(HttpServletResponse response) throws Exception { |
| 141 | + Workbook workbook = new WorkbookBuilder(new SXSSFWorkbook()) |
| 142 | + .createSheet("Sheet 1") |
| 143 | + .createRow(new Object[]{"cell1", "cell2", "cell3", "cell4"}) |
| 144 | + .createRow(new Object[]{"cell1", "cell2", "cell3", "cell4"}) |
| 145 | + .createRow(new Object[]{"cell1", "cell2", "cell3", "cell4"}) |
| 146 | + .createRow(new Object[]{"cell1", "cell2", "cell3", "cell4"}) |
| 147 | + .addCellRange(1, 2, 1, 2) |
| 148 | + .merge() |
| 149 | + .build(); |
| 150 | + try (ServletOutputStream out = response.getOutputStream()) { |
| 151 | + response.setHeader("Content-disposition", "attachment; filename=cellRangeMerge.xlsx"); |
| 152 | + workbook.write(out); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments