POI,跨行,跨列使用 DEMO參考
阿新 • • 發佈:2022-04-22
poi
後端程式碼:
/** * 匯出 */ @PostMapping("/test") public void test(HttpServletResponse response) { List<CustomerExcel.CustomerListExcel> list = new ArrayList<>(); CustomerExcel.CustomerListExcel c1 = new CustomerExcel.CustomerListExcel("1", "1", "1"); CustomerExcel.CustomerListExcel c2= new CustomerExcel.CustomerListExcel("2", "2", "2"); CustomerExcel.CustomerListExcel c3 = new CustomerExcel.CustomerListExcel("3", "3", "3"); list.add(c1); list.add(c2); list.add(c3); // String fileName = "fileName"; String title = "title"; String sheetName= "sheetName"; Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), CustomerExcel.CustomerListExcel.class, list); Sheet sheet = workbook.getSheet(sheetName); // 下標0開始插入2行 sheet.shiftRows(0, sheet.getLastRowNum(), 4, true, false); // 第1行, 建立2個單元格Row row0 = sheet.getRow(0); row0.createCell(0).setCellValue("總成交金額: 100.00"); row0.createCell(1).setCellValue("總利潤金額: 80.00"); // 第2行, 跨列合併3個單元格(多個單元格有值,取第一個) Row row1 = sheet.getRow(1); row1.createCell(0).setCellValue("跨列: 100.00"); row1.createCell(1).setCellValue("總利潤金額: 80.00"); row1.createCell(2).setCellValue("總利潤金額: 80.00"); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2)); // 第3行,4行, 跨行合併 Row row2 = sheet.getRow(2); row2.createCell(1).setCellValue("跨行: 100.00"); Row row3 = sheet.getRow(3); row3.createCell(1).setCellValue("跨行: 101.00"); sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1)); try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); workbook.write(response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } }
實體類
package net.hp.common.model.DTO.response.profitsharing; import cn.afterturn.easypoi.excel.annotation.Excel; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.NoArgsConstructor; import net.hp.common.model.DTO.response.charging.ChargingPileReportDTO; import java.io.Serializable; import java.util.Date; import java.util.List; /** * @Description 測試Excel * @Date 2022年03月08 11:01:18 * @Author 郭明華 */ @Data public class CustomerExcel implements Serializable { private static final long serialVersionUID = 1L; /** * 交易總金額 */ @Excel(name = "交易金額", orderNum = "0", width = 15) private String totalMoney; /** * 交易金額 */ @Excel(name = "交易金額", orderNum = "2", width = 15) private String totalOrderMoney; /** * 列表資料 */ List<CustomerExcel.CustomerListExcel> list; public CustomerExcel() { } public CustomerExcel(String totalMoney, String totalOrderMoney) { this.totalMoney = totalMoney; this.totalOrderMoney = totalOrderMoney; } @Data public static class CustomerListExcel { public CustomerListExcel() { } public CustomerListExcel(String orderNo, String money, String orderMoney) { this.orderNo = orderNo; this.money = money; this.orderMoney = orderMoney; } /** * 服務商號 */ @Excel(name = "訂單號", orderNum = "0", width = 25) private String orderNo; /** * 交易金額 */ @Excel(name = "交易金額", orderNum = "1", width = 15) private String money; /** * 實際分潤金額 */ @Excel(name = "實際分潤金額", orderNum = "2", width = 15) private String orderMoney; } }