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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-04-22 线性代数