---
title: "java代码导出excel"
author: "microsoft_hang"
date: "2009-06-24T18:53:00Z"
tags: []
slug: "java-excel"
draft: false
lastmod: "2009-06-24T18:53:00Z"
id: 414374
url: "https://microsoft-hang.iteye.com/blog/414374"
---

举个例子更加直观

 

在信息展示页面有一个button 此按钮用到将模块信息导出excel则首先用它连接一个action然后在action里查出所有数据将它放入范围在进行跳转到生成数据的jsp

如下：

&lt;%@ page contentType="text/html; charset=GBK" %&gt;、

&lt;%@ taglib prefix="c"  uri="[http://java.sun.com/jsp/jstl/core"%](http://java.sun.com/jsp/jstl/core)&gt;

&lt;%  
  //独立打开excel软件  
response.setHeader("Content-disposition","attachment; filename=laborDispute.xls");  
//嵌套在ie里打开excel

//response.setHeader("Content-disposition","inline; filename=MyExcel.xls");

//Word只需要把contentType="application/msexcel"改为contentType="application/msword"

%&gt;

&lt;html&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=GBK"&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;  
&lt;head&gt;  
&lt;title&gt;title&lt;/title&gt;

&lt;/head&gt;  
&lt;body&gt;

&lt;table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#70C3FD" style="border-collapse:collapse"&gt;  
&lt;tr class=""&gt;   
        &lt;td width="2%"&gt;&nbsp;&lt;/td&gt;  
       &lt;td width="10%" align="center"&gt;争议编号&lt;/td&gt;  
       &lt;td width="10%" align="center"&gt;保 险号&lt;/td&gt;  
       &lt;td width="10%" align="center"&gt;单 位号&lt;/td&gt;  
       &lt;td width="10%" align="center"&gt;争议类型&lt;/td&gt;               
       &lt;td width="10%" align="center"&gt;开始时间&lt;/td&gt;  
                      &lt;td width="15%" align="center"&gt;争议内容&lt;/td&gt;  
                      &lt;td width="10%" align="center"&gt;结    果&lt;/td&gt;  
                      &lt;td width="10%" align="center"&gt;争议附件&lt;/td&gt;  
                      &lt;td width="15%" align="center"&gt;备    注&lt;/td&gt;  
                    &lt;/tr&gt;  
     &lt;c:forEach items="${dispute}" var="info"&gt;                       
                     &lt;tr &gt;  
                       &lt;td width="2%" &gt;&lt;/td&gt;  
                       &lt;td height="19"  width="10%" align="center"&gt;${info.laborDisputeId}&lt;/td&gt;  
        &lt;td height="19"  width="10%" align="center"&gt;${info.employeeUserInfo.UEmployeeId}&lt;/td&gt;  
        &lt;td height="19"  width="10%"  align="center"&gt;${info.organCode}&lt;/td&gt;  
        &lt;td height="19"  width="10%" align="center"&gt;${info.disputeType}&lt;/td&gt;  
        &lt;td height="19" width="10%" align="center"&gt;${info.startDate}&lt;/td&gt;  
                       &lt;td  width="15%" align="center"&gt;${info.disputeContent}&lt;/td&gt;  
                       &lt;td  width="10%" align="center"&gt; ${info.result}&lt;/td&gt;  
                       &lt;td  width="10%" align="center"&gt; ${info.attachment}&lt;/td&gt;  
                       &lt;td  width="15%" align="center"&gt; ${info.memo}&lt;/td&gt;  
                     &lt;/tr&gt;  
                   &lt;/c:forEach&gt;  
     
                  &lt;/table&gt;

&lt;/body&gt;  
&lt;/html&gt;

 

只需如此将自己的信息放入jsp 则直接可实现另存为功能导出excel 注意页头部分的设置

&lt;!-- --&gt;