针对使用POI操作Excel文件,可以参考:《POI读取EXCEL教程》http://julycn.javaeye.com/blog/107341
下面为在struts1.x下使用POI生成Excel文件并下载。
大概过程如下:创建HSSFWorkbook——在刚创建的HSSFWorkbook下创建sheet——在刚创建的sheet下创建row——在刚创建的row下创建Cell——在刚创建的Cell中填入数据——把创建的HSSFWorkbook加入到OutputStream中。
在实际的开发中,经常会碰到以Excel形式导出数据库中的数据,完成此功能,可以在此程序的基础上把写入Cell中的数据改为写入数据库中的数据即可。
在此程序中使用了struts中的action做为控制器,所以相应的视图JSP文件内容为空即可。
为了方便阅读,全部代码如下,完整工程可到我的资源里下载:http://download.csdn.net/source/765876
- //DownloadAction.java
- import java.io.OutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- public class DownloadAction extends Action {
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- //create HSSFWorkbook
- HSSFWorkbook workbook = new HSSFWorkbook();
- //create HSSFSheet named user
- HSSFSheet sheet = workbook.createSheet("user");
- for(int i=0;i<5;i++){
- //create row
- HSSFRow row = sheet.createRow(i);
- for(int j=0;j<3;j++){
- //create cell
- HSSFCell cell = row.createCell((short)(j));
- //filled cell
- cell.setCellValue(i+"/"+j);
- }
- }
- //set response content type
- response.setContentType("application/vnd.ms-excel");
- //set the name of download file
- response.addHeader("Content-Disposition","attachment;filename=user.xls");
- //get response stream
- OutputStream out=response.getOutputStream();
- //add workbook to OutpustStream
- workbook.write(out);
- out.close();
- return mapping.findForward("success");
- }
- }
- <!--download.jsp(but it is blank.)-->
下面的为配置文件:web.xml and struts-config.xml
-
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>cofig</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://struts.apache.org/dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- </form-beans>
- <action-mappings>
- <action path="/download" type="DownloadAction">
- <!-- download.jsp is a blank file,but it exist -->
- <forward name="success" path="/download.jsp"></forward>
- </action>
- </action-mappings>
- </struts-config>