POI导出

String filePath = ResourceUtils.getURL(“classpath:”).getPath() + “exportExcel/problemsList.xls”;
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(filePath));
// 获取准备好的样式
HSSFCellStyle cellStyle = workbook.getSheetAt(0).getRow(1).getCell(0).getCellStyle();
// workbook有内容的工作簿
HSSFSheet sheet = workbook.getSheetAt(0);
// 行数(从0开始,0是列名,模板里已经写好了,所以从第二行那个开始)
int rowIndex = 1;
// list是存放的数据
for (NationalProblems hs : list) {
// 创建行
HSSFRow row = sheet.createRow(rowIndex);
// 创建列
HSSFCell cell = row.createCell(0);
// 往第一列添加内容
// 序号
cell.setCellValue(rowIndex);
// 使用的样式
cell.setCellStyle(cellStyle);

//            审计年度
            cell = row.createCell(1);
            cell.setCellValue(hs.getEnd()+'年');
            cell.setCellStyle(cellStyle);
//            问题标题
            cell = row.createCell(2);
            cell.setCellValue(hs.getTitle());
            cell.setCellStyle(cellStyle);
//            问题类别
            cell = row.createCell(3);
            cell.setCellValue(hs.getType());
            cell.setCellStyle(cellStyle);
//            问题描述
            cell = row.createCell(4);
            cell.setCellValue(hs.getDescribe());
            cell.setCellStyle(cellStyle);
//            整改状态
            cell = row.createCell(5);
            LVResults lvs = getZgztResult();
            for (LVResult lvResult : lvs) {
                if (hs.getStatus().equals(lvResult.getValue().toString())) {
                    hs.setStatus(lvResult.label);
                    break;
                }
            }
            cell.setCellValue(hs.getStatus());
            cell.setCellStyle(cellStyle);

//            行数
            rowIndex++;
        }

// 相当与下载Excel
// 输出流
ServletOutputStream outputStream = response.getOutputStream();
// 一个流 两个头
response.setHeader(“Content-Disposition”, “attachment; filename=” + URLEncoder.encode(“整改问题统计列表.xls”, “UTF-8”));
workbook.write(outputStream);
outputStream.close();
workbook.close();

    } catch (IOException e) {
        e.printStackTrace();
	}finally {
        if (workbook != null) {
            workbook.close();
        }
    }
### 导出 POI 数据的方法 为了实现 POI(兴趣点)数据的导出功能,可以采用 Apache POI 库来创建并写入 Excel 文件。以下是基于给定的需求和现有代码片段构建的具体解决方案。 #### 准备工作 确保项目中已引入 Apache POI 的依赖项。对于 Maven 项目,在 `pom.xml` 中添加如下配置: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> ``` #### 创建控制器接口 定义用于触发导出操作的服务端 API 接口。此接口接收前端传递的数据列表作为参数,并返回下载链接或直接响应浏览器以启动文件下载流程[^1]。 ```java @RestController @RequestMapping("/api/export") public class ExportController { @PostMapping("/pois") public void exportPoiData(@RequestBody List<Map<String, Object>> poiList, HttpServletResponse response) throws IOException { // 调用服务层方法处理实际逻辑 PoiExportService.export(poiList, response); } } ``` #### 实现核心业务逻辑 编写负责组装表格结构和服务于 HTTP 请求的核心组件——`PoiExportService` 类中的静态方法 `export()` 。该函数接受两个参数:一个是包含所有待导出记录的对象集合;另一个则是来自 Servlet 容器的标准输出流实例,以便后续通过它向客户端发送二进制字节序列形成最终文档[^2]。 ```java import org.apache.poi.hssf.usermodel.HSSFWorkbook; import javax.servlet.http.HttpServletResponse; public class PoiExportService { private static final String EXCEL_TYPE = "application/vnd.ms-excel"; /** * 将POI数据转换成Excel格式并通过HTTP响应写出. */ public static void export(List<Map<String, Object>> dataList, HttpServletResponse resp) throws IOException { try (HSSFWorkbook workbook = new HSSFWorkbook()) { var sheet = workbook.createSheet("POIs"); int rowIndex = 0; // 设置表头样式 var headStyle = createHeadCellStyle(workbook); // 构建表头行 Row headerRow = sheet.createRow(rowIndex++); Set<String> keysSet = null != dataList && !dataList.isEmpty() ? dataList.get(0).keySet() : Collections.emptySet(); AtomicInteger colIndex = new AtomicInteger(); for (String key : keysSet) { Cell cell = headerRow.createCell(colIndex.getAndIncrement()); cell.setCellValue(key); cell.setCellStyle(headStyle); } // 设置内容样式 var contentStyle = createContentCellStyle(workbook); // 填充主体部分 for (var record : dataList) { Row row = sheet.createRow(rowIndex++); colIndex.set(0); for (Object value : record.values()) { Cell cell = row.createCell(colIndex.getAndIncrement()); if (value instanceof Number numberValue) { cell.setCellValue(numberValue.doubleValue()); } else { cell.setCellValue(String.valueOf(value)); } cell.setCellStyle(contentStyle); } } // 配置响应头部信息 resp.setContentType(EXCEL_TYPE); resp.setHeader("Content-Disposition", "attachment;filename=POIData.xls"); resp.flushBuffer(); // 输出至客户端 workbook.write(resp.getOutputStream()); } } // 表头单元格样式设定... private static HSSFCellStyle createHeadCellStyle(HSSFWorkbook wb){ // ...此处省略具体实现细节... return style; } // 正文单元格样式设定... private static HSSFCellStyle createContentCellStyle(HSSFWorkbook wb){ // ...此处省略具体实现细节... return style; } } ``` 上述代码展示了如何利用 Java 和 Apache POI 来动态生成 Excel 文档的过程。注意这里仅提供了基本框架,针对特定应用场景可能还需要进一步调整和完善,比如增加异常捕获机制、优化性能以及增强用户体验等方面的工作[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值