Springboot生成Excel表格

本文介绍了如何在Springboot项目中生成Excel表格。首先,在pom.xml中添加所需的依赖;接着,通过Controller处理数据并写入Excel;然后,创建列表展示超链接;最终,点击导出链接可看到生成的Excel效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、在pom.xml中添加Excel的相关依赖包

<!--        Excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

2、在Controller里写入相关数据

@ResponseBody
@RequestMapping("/carnjExport")
//将列表数据导出为Excel
public void carnjExport(HttpServletResponse response){
    try{
        //设置Excel表格信息
        XSSFWorkbook workbook=new XSSFWorkbook();
        XSSFSheet sheet=workbook.createSheet();
        //创建表头
        XSSFRow row=sheet.createRow(0);
        //数据库相关信息
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("车牌号");
        row.createCell(2).setCellValue("年检时间");
        row.createCell(3).setCellValue("下次年检时间");
        row.createCell(4).setCellValue("送年检人");
        row.createCell(5).setCellValue("年检地点");
        row.createCell(6).setCellValue("年检状态");
        row.createCell(7).setCellValue("添加人");
        row.createCell(8).setCellValue("添加时间");
        //数据填充
        //获取列表数据
        List<Carnj> carnjList=carnjService.showAllCarnj();
        int rowindex=1;
        for(Carnj carnj:carnjList){
            XSSFRow row1= sheet.createRow(rowindex++);
            row1.createCell(0).setCellValue(carnj.getBh());
            row1.createCell(1).setCellValue(carnj.getCarid());
            row1.createCell(2).setCellValue(carnj.getNjsj());
            row1.createCell(3).setCellValue(carnj.getXcnjsj());
            row1.createCell(4).setCellValue(carnj.getSnjr());
            row1.createCell(5).setCellValue(carnj.getNjdd());
            row1.createCell(6).setCellValue(carnj.getNjzt());
            row1.createCell(7).setCellValue(carnj.getTjr());
            row1.createCell(8).setCellValue(carnj.getTjsj());
        }
        //设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition","attachment;filename=carnj.xlsx");
        //将Excel文档写入到响应中
        ServletOutputStream outputStream= response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}

3、在显示列表里给出超链接

<div id="top">
    <span><a href="/showsaveCarnj">年检登记</a>|<a href="/carnjExport">导出Excel</a></span>
</div>

4、实现效果

单击导出实现以下页面:

Spring Boot 提供了很多库来生成 Excel 文件并下载。以下是一些生成 Excel 文件并下载的步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 这些依赖是 Apache POI 库,它提供了 Java 操作 Microsoft Office 格式的工具。 2. 创建 Excel 文件 创建一个 Excel 文件并填充数据。以下是一个简单的例子: ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // Create a header row Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("Name"); headerRow.createCell(1).setCellValue("Age"); // Create some data rows Row dataRow1 = sheet.createRow(1); dataRow1.createCell(0).setCellValue("John"); dataRow1.createCell(1).setCellValue(25); Row dataRow2 = sheet.createRow(2); dataRow2.createCell(0).setCellValue("Jane"); dataRow2.createCell(1).setCellValue(30); ``` 这个例子创建了一个名为 "Sheet1" 的 Excel 表格,并填充了两行数据。 3. 将 Excel 文件转换为字节数组 将 Excel 文件转换为字节数组,以便在下载文件时使用。以下是一个简单的例子: ``` ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); byte[] bytes = outputStream.toByteArray(); ``` 这个例子将 Excel 文件写入 ByteArrayOutputStream 中,并使用 toByteArray() 方法将其转换为字节数组。 4. 创建下载链接 创建一个下载链接,以便用户可以下载生成Excel 文件。以下是一个简单的例子: ``` @GetMapping("/download") public ResponseEntity<byte[]> download() { // Generate the Excel file Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // ... // Convert the Excel file to a byte array ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); byte[] bytes = outputStream.toByteArray(); // Create a HttpHeaders object to set the Content-Disposition header HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "data.xlsx"); // Return the byte array as a ResponseEntity return new ResponseEntity<>(bytes, headers, HttpStatus.OK); } ``` 这个例子创建了一个名为 "/download" 的 GET 请求处理程序,它生成 Excel 文件并将其转换为字节数组。然后,它创建了一个 HttpHeaders 对象,设置 Content-Disposition 头以指示浏览器下载文件。最后,它将字节数组包装在 ResponseEntity 中并返回。 现在,当用户访问 "/download" 链接时,会下载生成Excel 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值