針對(duì)使用POI操作Excel文件,可以參考:《POI讀取EXCEL教程》http://julycn./blog/107341 下面為在struts1.x下使用POI讀取上傳的Excel文件中的內(nèi)容。見意:剛接觸POI的讀者可以看看,否則就可以免了。 大概過程如下:在瀏覽器端上傳文件——>使用struts api讀取上傳的文件——>使上傳的文件轉(zhuǎn)換為InputStream——>通過InputSteam得到HSSFWorkbook——>通過HSSFWorkbook得到HSSFSheet ——>通過HSSFSheet 得到HSSFRow——>再通過HSSFRow 得到HSSFCell——>最后得到單元格中的值 為了方便閱讀全部代碼如下,完整代碼見我的資源:http://download.csdn.net/source/765509 - import org.apache.struts.action.ActionForm;
- import org.apache.struts.upload.FormFile;
- public class UploadForm extends ActionForm {
- private FormFile file;
- public FormFile getFile() {
- return file;
- }
- public void setFile(FormFile file) {
- this.file = file;
- }
- }
- import java.io.InputStream;
- import java.util.ArrayList;
- 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;
- import org.apache.struts.upload.FormFile;
- public class UploadAction extends Action {
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
-
- UploadForm uploadForm=(UploadForm)form;
- FormFile file=uploadForm.getFile();
-
- InputStream is=file.getInputStream();
-
- HSSFWorkbook wb=new HSSFWorkbook(is);
-
- HSSFSheet sheet=wb.getSheet("employeelist");
- for(int i=0;i<sheet.getLastRowNum();i++)
- {
-
- HSSFRow row = sheet.getRow(i);
- for(int j=0;j<row.getLastCellNum();j++){
-
- HSSFCell cell=row.getCell((short)j);
-
- System.out.println(i+"/"+j+" : "+cell.getStringCellValue());
- }
- }
- return mapping.findForward("success");
- }
-
- }
- <!--Upload.jsp-->
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>Upload</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
-
- <body>
- <br>
- <br>
- <br>
- <br>
- <center>
- <form action="upload.do" method="post" enctype="multipart/form-data">
- Please upload your file : <input type="file" name="file">
- <input type="submit" name="Submit" value="Submit">
- </form>
- </center>
- <br>
- <br>
- <br>
- <br>
- </body>
- </html>
下面為配置文件:web.xml和struts-config.xml - <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java./xml/ns/j2ee"
- xmlns:xsi="http://www./2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
- <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>
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://struts./dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- <form-bean name="uploadForm" type="UploadForm"></form-bean>
- </form-beans>
- <action-mappings>
- <action path="/upload" name="uploadForm" type="UploadAction">
- <forward name="success" path="/upload.jsp"></forward>
- </action>
- </action-mappings>
- </struts-config>
針對(duì)使用POI操作Excel文件,可以參考:《POI讀取EXCEL教程》http://julycn./blog/107341 下面為在struts1.x下使用POI生成Excel文件并下載。 大概過程如下:創(chuàng)建HSSFWorkbook——在剛創(chuàng)建的HSSFWorkbook下創(chuàng)建sheet——在剛創(chuàng)建的sheet下創(chuàng)建row——在剛創(chuàng)建的row下創(chuàng)建Cell——在剛創(chuàng)建的Cell中填入數(shù)據(jù)——把創(chuàng)建的HSSFWorkbook加入到OutputStream中。 在實(shí)際的開發(fā)中,經(jīng)常會(huì)碰到以Excel形式導(dǎo)出數(shù)據(jù)庫中的數(shù)據(jù),完成此功能,可以在此程序的基礎(chǔ)上把寫入Cell中的數(shù)據(jù)改為寫入數(shù)據(jù)庫中的數(shù)據(jù)即可。 在此程序中使用了struts中的action做為控制器,所以相應(yīng)的視圖JSP文件內(nèi)容為空即可。 為了方便閱讀,全部代碼如下,完整工程可到我的資源里下載:http://download.csdn.net/source/765876 - 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 {
-
- HSSFWorkbook workbook = new HSSFWorkbook();
-
- HSSFSheet sheet = workbook.createSheet("user");
- for(int i=0;i<5;i++){
-
- HSSFRow row = sheet.createRow(i);
- for(int j=0;j<3;j++){
-
- HSSFCell cell = row.createCell((short)(j));
-
- cell.setCellValue(i+"/"+j);
- }
- }
-
- response.setContentType("application/vnd.ms-excel");
-
- response.addHeader("Content-Disposition","attachment;filename=user.xls");
-
- OutputStream out=response.getOutputStream();
-
- 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./xml/ns/j2ee"
- xmlns:xsi="http://www./2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
- <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>
<?xml version="1.0" encoding="ISO-8859-1" ?>- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://struts./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>
|