Reprint: Without poi method, directly use jsp page to convert to excel or word

Steps to generate Word or Excel file with Jsp: 

1. First use Word or Excel to make a template file, and then save it as a web page html format. Then change the extension to .jsp. 

 

2. Add the code at the top of the renamed jsp page code: 

If it is to generate Excel, add: 

<%@page language="java" contentType="application/msexcel;charset=gb2312"%> 

If you generate Word, add: 

<%@page language="java" contentType="application/msword;charset=gb2312"%> 

 

3. Continue to add after the above code (specify the default file name to display when the document is generated and downloaded): 

 

To generate Excel, add: 

 

<% String fileName="我的文件名.xls"; String sn = new String(fileName.getBytes("gb2312"),"ISO8859-1"); response.setHeader("Content-Disposition","attachment; filename="+sn); %> 

 

To generate Word, add: 

 

<% String fileName="我的文件名.doc"; String sn = new String(fileName.getBytes("gb2312"),"ISO8859-1"); response.setHeader("Content-Disposition","attachment; filename="+sn); %> 

 

Finally, according to the need, just like editing the jsp page, output the data content you want to output in the body content of this file. In this way, when you visit this jsp, the file download dialog box will appear, and the downloaded file is the Word or Excel file it generates.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893233&siteId=291194637