jsp导出excell,word

来源:互联网 发布:ubuntu 安装jdk1.8 编辑:程序博客网 时间:2024/05/22 03:46
  1. jsp导出excell
  2. <%@ page contentType="application/vnd.ms-excel; charset=gbk" %>   
  3. <%@ page language="java" pageEncoding="GBK"%>   
  4. <%   
  5.     String filename = new String(("表格名").getBytes("GBK"),"ISO-8859-1");    
  6.     response.addHeader("Content-Disposition""filename=" + filename + ".xls");   
  7. %>   
  8. <html>   
  9. <head>   
  10.     <meta name="Generator" content="Microsoft Excel 11">   
  11.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
  12. </head>   
  13. <body >   
  14. <center><b>表格名</b></center><br>   
  15.     <table border="1" align="center" cellpadding="0" cellspacing="1">   
  16.         <tr >    
  17.             <td>   
  18.             <!--在这里用html写表格内容的代码,可以用jsp代码-->     
  19.             </td>   
  20.         </tr>   
  21.     </table>   
  22. </body>   
  23. </html>  

   

JSP生成WORD文档

 

在jsp页面上生成word文档非常简单,只需把contentType=”text/html”改为contentType="application/msword; charset=gb2312"即可,代码如下:
<%@ page contentType="application/msword; charset=gb2312" %>
通过设置可以使原来页面的内容在word中表现出来。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

JSP生成WORD文档的另类方法
这种方法不需要用到第三方的类库,只要先把WORD模版文档另存为网页再提取源代码,把源代码保存为JSP文件,然后在该JSP文件的头部加上
<%@ page contentType="application/msword;charset=GBK" %>,
这样访问该JSP时就会弹出“打开”和“保存”的对话框,如果客户端有WORD程序就可以直接在网页中打开生成的WORD文档了。

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

               JSP页面引入来实现Word保存就方便多了,但是也有不足的地方,首先如果需要引入
<meta http-equiv="Content-Type" content="application/msword; charset=gb2312" />

               如果需要下载的话就引入

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

               其实如果大家用框架做就方便多了,比如Struts2。在Action里直接写如下代码:

          if(out!=null){
                  String fileName="";
                  fileName+="评价报告.doc";
             try {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1"));
                  } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                  }

     out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。Action设置jsp页面头文件。这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。

          不足的地方在于由于表内容是动态生成,有的需要先查看在下载Word,就需要另外建立一个新JSP页面进行Word下载,当然首先要在struts.xml里配置好页面转向。

          新建立的页面传值同查看页面要保持一样。