Javaweb项目实现文件导出功能

在我的项目中,我将模糊查询与文件导出结合使用,实现下载前端列表展示的数据到本地

导入maven坐标

<!--导出为表需要-->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.16</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.16</version>
</dependency>

前端部分代码及展示

<button  class="btn btn-default" onclick="exportExcel()">导出表格</button>
<input type="text" id="se1"
         placeholder="搜索" >
<input type="submit" onclick="jax(1)" value="查询" class="btn btn-default">



function jax(pagesize){
    var search = $("#se1").val();
    console.log(search);
    $.ajax({
        url:"/getLand",
        dataType:"JSON",
        data:{
            pagesize:pagesize,
            search:search
        },
        type:"POST",
        success:function (data) {
            $("#d1").children().remove();
            $("#tb").children().remove();
            $("#l1").children().remove();
            $("#l2").children().remove();
            $("#l3").children().remove();
            for (var i=0;i<data.content.length;i++){
                $("#tb").append(
                    "<tr>" +
                    "<td hidden='hidden'>"+data.content[i].lid+"</td>"+
                    "<td>"+data.content[i].purpose+"</td>"+
                    "<td>"+data.content[i].location+"</td>"+
                    "<td>"+data.content[i].mode+"</td>"+
                    "<td>"+data.content[i].area+"</td>"+
                    "<td>"+data.content[i].price+"</td>"+
                    "<td>"+data.content[i].actualarea+"</td>"+
                    "<td>"+data.content[i].issuedate+"</td>"+
                    "<td>" +
                    "<button onclick='updateLand("+data.content[i].lid+")'>修改</button>"+
                    "<button onclick='delLand("+data.content[i].lid+")'>删除</button>"+
                    "</td>"+
                    "</tr>");
            }
            $("#d1").append(
                "<span>"+"总共"+data.pageTotal+"页,共"+data.recordTotal+"条数据"+"</span>"
            )
            $("#l1").append(
                "<a onclick='left("+data.currentPage+")'>上一页</a>"

            )
            $("#l2").append(
                "<a onclick='right("+data.currentPage+","+data.pageTotal+")'>下一页</a>"
            )
            $("#l3").append(
                "<a onclick='sw("+data.pageTotal+")'>尾页</a>"
            )
        }
    })

}
function exportExcel() {
    var search = $("#se1").val();
    location.href = "/landExportExcel?search="+search;
}

 控制层代码

@RequestMapping("/landExportExcel")
public void landExportExcel(String search,HttpServletResponse response){
    search=search==null?"":search;
    landService.landExportExcel(search,response);
}

实现层代码

在这里可以自定义下载后excel的行标题以及文件名称等

@Override
public void landExportExcel(String search,HttpServletResponse response)  {
    LandMapper landMapper=sqlSessionTemplate.getMapper(LandMapper.class);
    // 创建Excel工作簿
    Workbook workbook = new XSSFWorkbook();
    // 创建工作表
    Sheet sheet = workbook.createSheet("Data");

    // 创建标题行
    Row headerRow = sheet.createRow(0);
    headerRow.createCell(0).setCellValue("ID");
    headerRow.createCell(1).setCellValue("土地证载明的土地用途");
    headerRow.createCell(2).setCellValue("具体位置");
    headerRow.createCell(3).setCellValue("供地方式及批复");
    headerRow.createCell(4).setCellValue("供地面积");
    headerRow.createCell(5).setCellValue("账面价值");
    headerRow.createCell(6).setCellValue("实际占地面积");
    headerRow.createCell(7).setCellValue("土地证发证时间");
    // 从数据库获取数据
    List<Land> resultSet = landMapper.landExportExcel(search);

    // 写入数据到Excel
    int rownum = 1;
    for (Land land : resultSet){
        Row row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(land.getLid());
        row.createCell(1).setCellValue(land.getPurpose());
        row.createCell(2).setCellValue(land.getLocation());
        row.createCell(3).setCellValue(land.getMode());
        row.createCell(4).setCellValue(land.getArea());
        row.createCell(5).setCellValue(land.getPrice());
        row.createCell(6).setCellValue(land.getActualarea());
        row.createCell(7).setCellValue(land.getIssuedate());
    }

    // 设置响应头信息
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    //设置下载文件名称
     response.setHeader("Content-Disposition", "attachment; filename=\"data.xlsx\"");

    // 写入响应输出流
    try (OutputStream os = response.getOutputStream()) {
        workbook.write(os);
        workbook.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

控制层和service层需要根据自己的项目自行补充,至此即可实现在前端页面点击导出按钮下载excel文件

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用 Apache POI 库来实现将数据导出为 Excel 文件功能。下面是一个简单的示例代码,演示了如何使用 POI 将数据导出为 Excel。 首先,您需要添加 Apache POI 的依赖到您的项目中。可以在 Maven 中添加以下依赖: ```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> ``` 接下来,您可以使用以下代码将数据导出为 Excel: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class ExcelExporter { public void exportDataToExcel(List<List<String>> data, String filePath) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); int rowNum = 0; for (List<String> rowData : data) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (String cellData : rowData) { Cell cell = row.createCell(colNum++); cell.setCellValue(cellData); } } try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码中,`exportDataToExcel` 方法接受一个包含数据的二维字符串列表和导出文件的路径作为参数。它会创建一个新的 Excel 文件,并将数据写入到文件中。 您可以根据自己的需求修改代码,例如设置单元格样式、合并单元格等。 请注意,这只是一个简单的示例,实际应用中可能需要更复杂的处理和错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码来码去(未来可期)

感谢您的打赏,我们一起进步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值