日期:2014-05-16  浏览次数:20750 次

apache poi 读取Excel文件内容(2003,2007)

很久没记录了,前一段做了个只读取Excel文件内容的需求,今天整理出来,备份一下。

?

?项目名称 Test

?

直接上代码

?

index.jsp

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
    
    <title>读取的Excel文件内容(2003,2007)</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">
	<!-- 引入jQuery -->
	<script type="text/javascript" src="/js/jquery.js"></script>
	<script type="text/javascript">
	//提交Excel
	function importExcel(){
	    var fileExcel = $("#fileExcel").val();
	    // 导入文件必选项
	    if(fileExcel==""||fileExcel==null){
	    	alert("请选择要导入Excel文件!");
	    	return false;
	    }
	  	// 判断文件类型
	  	if(fileExcel!=""&&fileExcel!=null){
	  		var hz = fileExcel.substr(fileExcel.lastIndexOf(".")+1);
	  		if(hz!="xls"&&hz!="xlsx"){
	  			alert("请按提示导入指定类型文件!");
	  			return false;
	  		}
	  	}
	  	//获得客户端上传的实际路径
	  	$("#filePath").val(fileExcel);
	    return true;
	}
	</script>
  </head>
  
  <body>
	<form action="<%=request.getContextPath()%>/Excel.do?method=read" id="excelForm" name="excelForm" method="post" enctype="multipart/form-data" onsubmit="return importExcel();">
	  	<table align="center" border="0">
		  	<tr>
		  		<td>
				  	请选择读取的Excel文件:
				    <input type="file" id="fileExcel" name="file" title="请选择文件">&nbsp;&nbsp;&nbsp;
				    <font style="color: red;">注:.xls或.xlsx</font></br>
				    <input type="hidden" id="filePath" name="filePath" value="">
				    <input type="submit" value="提 交" title="提 交">
				    <input type="reset" value="重 置" title="重 置">
			    </td>
		    </tr>
	    </table>
    </form>
  </body>
</html>

?

?

struts-config.xml

?

?

<?xml version="1.0" encoding="UTF-8"?>
<!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="excelForm" type="com.test.struts.form.ExcelForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  	<action path="/Excel" name="excelForm" type="com.test.struts.action.ExcelReadAction" scope="request" parameter="method"></action>
  </action-mappings>
  <message-resources parameter="com.test.struts.ApplicationResources" />
</struts-config>

?

?

web.xml

?

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>Set Character Encoding</filter-name>
		<filter-class>com.test.struts.filter.SetCharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>ignore</param-name>
			<param-value>true</param-value>
		</in