1. Excel文件使用Http服务实现下载
1.1. 在web.xml文件中注册新建的Servlet
<!-- ExcelDownloadServlet -->
<servlet>
<servlet-name>excelDownloadServlet</servlet-name>
<servlet-class>
com.jerry.support.ExcelDownloadServlet
</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>excelDownloadServlet</servlet-name>
<url-pattern>/excelDownloadServlet</url-pattern>
</servlet-mapping>
1.2. 创建ExcelDownloadServlet.java类,在容器中查找相应service服务,使用service服务去完成预定的任务
public class ExcelDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
IExcelService iEexcelService = ServiceLocator.getInstance().getService("iEexcelService");
response.reset(); // 重置response
response.setContentType("application/msexcel;charset=\"UTF-8\"");
response.setHeader("Content-disposition","attachment; filename=IndexModelExcel.xls");
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
String path = iEexcelService.downloadTemplate(request);
PathUtils utils = new PathUtils();
File file = new File(utils.getWebRoot() + path);
in = new BufferedInputStream(new FileInputStream(file));
out = new
BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
while (-1 != in.read(buff)) {
out.write(buff);
buff = new byte[1024]; // 需要扩充字符数组
}
try {
if (null != in)
in.close();
if (null != out)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
response.flushBuffer();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
this.doGet(request, response);
}
}
request.getRequestURL():http://200.200.202.189:8081/project /excelDownloadServlet
request.getContextPath():/project
url: http://200.200.202.189:8081/visu/IndexExcel.xls
1.3.Web工程路径Utils
public class PathUtils {
/**
* 获取工程类路径(绝对路径)
*
* @return
*/
public String getWebClassesPath() {
String
path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
return path;
}
/**
* 获取WEB-INF文件夹路径(绝对路径)
*
* @return
* @throws
IllegalAccessException
*/
public String getWebInfPath() throws IllegalAccessException{
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF")+8);
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
/**
* 获取WebRoot文件夹路径(绝对路径)
*
* @return
* @throws
IllegalAccessException
*/
public String getWebRoot() throws IllegalAccessException{
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF/classes"));
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
public static void main(String[] args) throws Exception {
PathUtils
utils = new PathUtils();
System.out.println("1、" + utils.getWebClassesPath());
System.out.println("2、" + utils.getWebInfPath());
System.out.println("3、" + utils.getWebRoot());
}
}
输出:1、/E:/Workspaces/MyEclipse/project/WebRoot/WEB-INF/classes/
2、/E:/Workspaces/MyEclipse/project/WebRoot/WEB-INF/
3、/E:/Workspaces/MyEclipse/project/WebRoot/
1.4. 浏览器远程调用servlet在服务器端生成Excel文件,发生乱码及无法打开Excel文件的问题是因为在生成Excel文件时没有指定文件的编码格式,采用UTF-8的格式
File file = new File(realPath);
file.createNewFile();
FileOutputStream
fos = new FileOutputStream(file);
OutputStreamWriter osw = new
OutputStreamWriter(fos, "UTF-8");
osw.write(excelXml);
osw.flush();
fos.close();
分享到:
相关推荐
前言项目中使用到比较多的关于Excel的前端上传与下载,整理出来,以便后续使用或分析他人。 1.前端vue:模板下载与导入Excel 导入Excel封装了子组件,点击导入按钮可调用子组件,打开文件上传的对话框,上传成功后...
SpringBoot实现Excel文件解析
1、java解析读取excel文件中的数据,并写入数据库。 2、java读取数据库数据,并导出为excel文件。 3、README.md中有详细的操作步骤示例。 使用说明: 1. 先使用postman导入:other/excel相关.postman_collection....
JavaScript 使用ActiveX实现在Excel文件中插入图片
springboot实现Excel文件导入与导出功能, 本次任务主要实现两个功能,一是如何将Excel文件导入后台服务器,解析文件中的内容,并且存入数据库中。二是如何将后台数据库中的数据导出为Excel表格。经过调研,实现此...
Excel文件上传预览示例
excel上传预览 这里会用到 npm i element-ui npm i xlsx 在vue的template中写上,排版和css看个人需求 <div> 选择文件 <input type=file d=file_input @change=importf(this) accept=.csv, application/vnd....
- Pandas包, 后台实现EXCEL文件生成和写操作。 - csv包,后台实现csv文件生成和写操作。 - reportlab包, 后台实现PDF文件生成和写操作。 - Flask的send_file包, 实现从后端向发送请求的客户端传送文件。 提供了...
java实现文件下载,并弹出保存对话框(Excel)
在Spring Boot中实现Excel的导入导出... filename=employees.xlsx,这样浏览器就会将响应解释为要下载的Excel文件。 将Excel写入响应流: 使用response.getOutputStream()获取输出流,然后将工作簿写入到输出流中.....
JMeter处理Excel文件
Qt5,excel文件,创建文件,读写文件,增加删除excel内容等,主要接口有新建一个Excel、增加1个Worksheet、向Excel单元格中写入数据、保存Excel、设置sheet名称、导入到数据库等,各接口方便使用 详细描述可见:...
用labview调用excel后关闭,在labvie中操作excel不必打开excel文件,使用者不必接触excel即可实现labvie 与excel的通信
本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// /// 导出Excel文件 /// /// /// <param name=dataSet></param> /// 数据集 /// 导出后是否打开文件 /// ...
压缩文件中包括一个包含宏代码的Excel文件Sheet Macros.xls以及其他四个测试文件工作表 1~4。 宏的功能为(1)选中Excel表格中的某些行或列,运行某个Macro,自动根据选中的cells创建新的worksheets,worksheets的...
excel文件上传(代码)! excel文件上传(代码)! excel文件上传(代码)! excel文件上传(代码)! excel文件上传(代码)! excel文件上传(代码)!
windows 操控excel文件 c++实现
本文实例讲述了Python实现读取txt文件并转换为excel的方法。分享给大家供大家参考,具体如下: 这里的txt文件内容格式...import xlrd #打开excel文件 import os txtFileName = 'questions.txt' excelFileName = 'quest
java实现excel表格文件的复制,测试可用,将excel表格的表头和内容拷贝到新表中
使用js-xlsx可以很方便地实现前端导出Excel文件的功能,你只需要提供要导出的数据,并通过调用js-xlsx的API即可实现导出功能。它的API很容易理解,而且代码实现起来也很简单。 总的来说,使用js-xlsx可以使前端开发...