Java 为什么poi excel导出在我的spring项目中不起作用?
因为我是韩国人,我为我的英语很差道歉 我正在尝试实现一个spring项目,在该项目中,可以使用“poi库”将db值下载到Excel 我的代码不起作用。 日志中没有错误,所以我甚至不知道为什么。 有人知道我的代码不起作用吗 从数据库获取数据变得越来越清晰 因为它是一个公司项目,所以我更改了变量名并上传了它 控制器Java 为什么poi excel导出在我的spring项目中不起作用?,java,excel,spring,apache-poi,Java,Excel,Spring,Apache Poi,因为我是韩国人,我为我的英语很差道歉 我正在尝试实现一个spring项目,在该项目中,可以使用“poi库”将db值下载到Excel 我的代码不起作用。 日志中没有错误,所以我甚至不知道为什么。 有人知道我的代码不起作用吗 从数据库获取数据变得越来越清晰 因为它是一个公司项目,所以我更改了变量名并上传了它 控制器 @RequestMapping(value="/export/excel.download") @ResponseBody public void ExcelDownload(HttpS
@RequestMapping(value="/export/excel.download")
@ResponseBody
public void ExcelDownload(HttpServletRequest request, HttpServletResponse response) throws Exception{
List list=tempService.getDataList();
Calendar cal = Calendar.getInstance( );
String fileName = "excel_"+cal.getTimeInMillis();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet worksheet = workbook.createSheet(fileName);
XSSFRow row = worksheet.createRow(0);
row.createCell(0).setCellValue("first");
row.createCell(1).setCellValue("second");
row.createCell(2).setCellValue("third");
row.createCell(3).setCellValue("fourth");
row.createCell(4).setCellValue("fifth");
row.createCell(5).setCellValue("sixth");
row.createCell(6).setCellValue("seventh");
row.createCell(7).setCellValue("eighth");
for (int i = 0, rowIndex=1; i < list.size(); i++) {
Map item = (Map) list.get(i);
row = worksheet.createRow(rowIndex+i);
row.createCell(0).setCellValue(item.get(0).toString());
row.createCell(1).setCellValue(item.get(1).toString());
row.createCell(2).setCellValue(item.get(2).toString());
row.createCell(3).setCellValue(item.get(3).toString()+" Days");
if(item.get(4)!= null)
row.createCell(4).setCellValue(item.get(4).toString());
String checkUseYn[]=item.get(5).toString().split(",");
row.createCell(5).setCellValue(checkUseYn[0]);
row.createCell(6).setCellValue(checkUseYn[1]);
row.createCell(7).setCellValue(checkUseYn[2]);
}
UtilFunctions.downloadExcel(request, response, workbook, fileName);
}
你能定义什么是不工作吗?你有空文件吗?dowload没有启动吗?我使用poi版本3.15,即使是空文档也无法下载。仅从数据库获取数据。请描述访问
/export/excel时发生的情况。使用浏览器下载。有一个访问url的按钮。按下按钮通过ajax访问控制器。List List=tempService.getDataList();这个代码有效。重复的句子似乎也很有效。但是下载不起作用。你能定义什么是不起作用吗?你有空文件吗?dowload没有启动吗?我使用poi版本3.15,即使是空文档也无法下载。仅从数据库获取数据。请描述访问/export/excel时发生的情况。使用浏览器下载。有一个访问url的按钮。按下按钮通过ajax访问控制器。List List=tempService.getDataList();这个代码有效。重复的句子似乎也很有效。但是下载不起作用。
public static void downloadExcel(HttpServletRequest request, HttpServletResponse response, XSSFWorkbook workbook, String fileName) throws Exception{
if(workbook != null) {
String filename = fileName+".xlsx";
response.setHeader("Content-Type", "application/octet-stream;charset=euc-kr");
String userAgent = request.getHeader("User-Agent");
if(userAgent != null && userAgent.indexOf("MSIE 5.5") > -1)
response.setHeader("Content-Disposition", "Filename=" + URLEncoder.encode(filename, "UTF-8") + ";");
else if(userAgent != null && userAgent.indexOf("MSIE") > -1)
response.setHeader("Content-Disposition", "ATTachment; Filename=" + URLEncoder.encode(filename, "UTF-8") + ";");
else
response.setHeader("Content-Disposition", "ATTachment; Filename=" + new String(filename.getBytes("euc-kr"), "latin1") + ";");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Pragma", "no-cache;");
response.setHeader("Expires", "-1;");
OutputStream out = null;
try
{
out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}