关于springboot项目前端导出从数据库查询到的数据到Excel时出现的问题
问题是这样的,我现在想通过下载在用户在点击导出时将查询的结果导出来。
然后这是我的js
后端代码是这样的
controller
工具类ExcelUtils
我现在这样写可以导出来一个excel文件,但是打开的时候显示

还有一种方式就是我可以不用下载,直接在后端生成,但是这样目录我又就写死了,感觉对用户的友好性不好。。。大佬们帮帮我吧,,,毕设写的我快疯了
然后这是我的js
layer.confirm('确定导出到Excel?', {
btn : [ '确定', '取消' ]
}, function() {
$.ajax({
type : "POST",
url : "/require/export2Excel",
responseType : Blob,
data : {
requireName : $('#requireName').val(),
requireId : $('#requireId').val(),
functionName : $('#functionName').val(),
functionModule : $('#functionModule').val(),
functionType : $('#functionType').val(),
assignState : $('#assignState').val(),
submitState : $('#submitState').val(),
},success: (wb) => {//wb为我后端的workbook,我写成respong时excel文件直接没数据,写成这个的时候有数据,这儿可能有问题
var blob = new Blob([wb]); //创建一个blob对象
var a = document.createElement('a'); //创建一个<a></a>标签
a.href = URL.createObjectURL(blob); // response is a blob
a.download = "功能需求表.xlsx";
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
layer.msg("导出成功!");
}
})
});
后端代码是这样的
controller
@PostMapping("export2Excel")
@ResponseBody
public void export2Excel(@RequestParam Map<String, Object> params, HttpServletResponse response){
Query query = new Query(params,"");
List<FunctionRequireDO> list = requireService.list(query);
ExcelUtils.writeExcel(response,list,FunctionRequireDO.class);
}
工具类ExcelUtils
public static <T> void writeExcel(HttpServletResponse response, List<T> dataList, Class<T> cls){
Field[] fields = cls.getDeclaredFields();
List<Field> fieldList = Arrays.stream(fields)
.filter(field -> {
ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
if (annotation != null && annotation.col() > 0) {
field.setAccessible(true);
return true;
}
return false;
}).sorted(Comparator.comparing(field -> {
int col = 0;
ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
if (annotation != null) {
col = annotation.col();
}
return col;
})).collect(Collectors.toList());
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
AtomicInteger ai = new AtomicInteger();
{
Row row = sheet.createRow(ai.getAndIncrement());
AtomicInteger aj = new AtomicInteger();
//写入头部
fieldList.forEach(field -> {
ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
String columnName = "";
if (annotation != null) {
columnName = annotation.value();
}
Cell cell = row.createCell(aj.getAndIncrement());
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
Font font = wb.createFont();
font.setBoldweight(Font.BOLDWEIGHT_NORMAL);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
cell.setCellValue(columnName);
});
}
if (CollectionUtils.isNotEmpty(dataList)) {
dataList.forEach(t -> {
Row row1 = sheet.createRow(ai.getAndIncrement());
AtomicInteger aj = new AtomicInteger();
fieldList.forEach(field -> {
Class<?> type = field.getType();
Object value = "";
try {
value = field.get(t);
} catch (Exception e) {
e.printStackTrace();
}
Cell cell = row1.createCell(aj.getAndIncrement());
if (value != null) {
if (type == Date.class) {
cell.setCellValue(value.toString());
} else {
cell.setCellValue(value.toString());
}
cell.setCellValue(value.toString());
}
});
});
}
//冻结窗格
wb.getSheet("Sheet1").createFreezePane(0, 1, 0, 1);
//浏览器下载excel
buildExcelDocument("功能需求表.xlsx",wb,response);
//生成excel文件
// buildExcelFile("C:/Users/Administrator/Desktop/"+fileName+".xlsx",wb);
}
/**
* 浏览器下载excel
* @param fileName
* @param wb
* @param response
*/
private static void buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response){
try {
//application/octet-stream
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("content-type", "application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));
response.flushBuffer();
wb.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
我现在这样写可以导出来一个excel文件,但是打开的时候显示

还有一种方式就是我可以不用下载,直接在后端生成,但是这样目录我又就写死了,感觉对用户的友好性不好。。。大佬们帮帮我吧,,,毕设写的我快疯了
...全文
相关推荐
springboot dormitory management system
每个模块均配备完整的CRUD操作接口,支持分页查询、模糊检索、条件筛选与Excel批量导入导出功能。
SpringBoot+Vue人事管理系统[可运行源码]
权限控制体系采用RBAC(基于角色的访问控制)模型,预置超级管理员、部门主管、普通员工三类角色,每个角色绑定细粒度接口级权限,权限数据通过数据库动态加载,支持运行时热更新。
StringBoot+Thymeleaf 系统查询工具
**查询结果展示**:提供表格形式的查询结果展示,并支持导出为Excel、CSV等格式,方便数据分析和报表生成。7.
Java毕业设计-基于springboot开发的毕业设计成绩管理系统设计与实现-毕业论文(附毕设源代码).rar
- 成绩查询:学生和教师可以查询个人或班级的详细成绩。- 成绩统计:系统能够根据不同的条件对成绩进行统计和分析。- 数据导出:提供成绩数据导出为Excel或其他格式文件的功能。
JasperReportTableDemo
表格的每一行和列都可以映射到数据源中的字段,通过绑定数据源(例如Java集合、数据库查询结果等),报表可以动态填充数据。


Java SE
