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文件