针对使用POI操作Excel文件,可以参考:《POI读取EXCEL教程》http://julycn.javaeye.com/blog/107341
下面为在struts1.x下使用POI读取上传的Excel文件中的内容。见意:刚接触POI的读者可以看看,否则就可以免了。
大概过程如下:在浏览器端上传文件——>使用struts api读取上传的文件——>使上传的文件转换为InputStream——>通过InputSteam得到HSSFWorkbook——>通过HSSFWorkbook得到HSSFSheet ——>通过HSSFSheet 得到HSSFRow——>再通过HSSFRow 得到HSSFCell——>最后得到单元格中的值
为了方便阅读全部代码如下,完整代码见我的资源:http://download.csdn.net/source/765509
- //UploadForm.java
- 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;
- }
- }
- //UploadAction.java
- 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();
- //get the InputStream
- InputStream is=file.getInputStream();
- //get the Workbook
- HSSFWorkbook wb=new HSSFWorkbook(is);
- //get the sheet
- HSSFSheet sheet=wb.getSheet("employeelist");
- for(int i=0;i<sheet.getLastRowNum();i++)
- {
- //get row
- HSSFRow row = sheet.getRow(i);
- for(int j=0;j<row.getLastCellNum();j++){
- //get cell
- HSSFCell cell=row.getCell((short)j);
- /* here get String type ,if there are other types in your sheet ,
- you can use other Cell's API,such as cell.getBooleanCellValue();
- cell.getDateCellValue();cell.getNumericCellValue();
- */
- 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.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>
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!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-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>