Linux系统和windows系统的路径有区别
- 项目中获取模板excel的路径
// 获取模板文件路径
String tempPath = request.getSession().getServletContext().getRealPath("/")
+ "\\file\\download\\error_import_AssetInfo.xlsx";
- 使用\拼接在windows系统中可以找到,但是在linux中则不行。
- linux识别的路径为:
/usr/local/java/tomcat/tomcat8.5/webapps/ias/\file\download\error_import_AssetInfo.xlsx (No such file or directory)
解决办法:
使用File.separator进行拼接。如下:
// 获取模板文件路径
String tempPath = request.getSession().getServletContext().getRealPath("/")
+File.separator+ "file"+File.separator+"download"+File.separator+"assetInfo.xlsx";
excel表格导出B/S和C/S的区别
B/S架构的项目,excel表格导出需要转成response的流。
下面是B/S模式中采用的输出方式,而不是输出到本地指定的磁盘目录。该代码表示将details.xls的Excel文件通过应答实体(response)输出给请求的客户端浏览器,客户端可保存或直接打开。
//输出Excel文件
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=details.xls");
response.setContentType("application/msexcel");
wkb.write(output);
output.close();
C/S架构的项目,excel表格导出可以指定导出的本地系统的路径。
File file=new File("D://temp.xls");
WritableWorkbook wwb = Workbook.createWorkbook(file);
参考的博客:https://www.cnblogs.com/hanfeihanfei/p/7079210.html