ocupload、struts2实现excel文件上传,poi解析

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.itcast.bos</groupId>
  <artifactId>mavenbos</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>mavenbos</name>
   <properties>
      <spring.version>3.2.12.RELEASE</spring.version>
      <spring-data.version>1.4.1.RELEASE</spring-data.version>
      <hibernate.version>3.6.10.Final</hibernate.version>
      <sl4j.version>1.7.6</sl4j.version>
      <struts2.version>2.3.15.3</struts2.version>
      <c3p0.version>0.9.1.2</c3p0.version>
      <oracle.version>10.2.0.4.0</oracle.version>
      <servlet.version>2.5</servlet.version>
      <jsp.version>2.0</jsp.version>
      <junit.version>4.11</junit.version>
      <hibernate-tools.version>3.2.4.GA</hibernate-tools.version>
      <poi.version>3.9</poi.version>
      <cxf.version>2.2.3</cxf.version>
      <hibernate-search.version>3.4.2.Final</hibernate-search.version>
      <shiro.version>1.2.2</shiro.version> 
   </properties>
   <dependencies>
   <!-- spring  -->
   	<dependency>
   		<groupId>org.springframework</groupId>
   		<artifactId>spring-context</artifactId>
   		<version>${spring.version}</version>
   	</dependency>

   	<dependency>
   		<groupId>org.springframework</groupId>
   		<artifactId>spring-aspects</artifactId>
   		<version>${spring.version}</version>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework</groupId>
   		<artifactId>spring-orm</artifactId>
   		<version>${spring.version}</version>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework</groupId>
   		<artifactId>spring-web</artifactId>
   		<version>${spring.version}</version>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework</groupId>
   		<artifactId>spring-test</artifactId>
   		<version>${spring.version}</version>
   	</dependency>
   	<!-- hiberante -->
   	<dependency>
   		<groupId>org.hibernate</groupId>
   		<artifactId>hibernate-core</artifactId>
   		<version>${hibernate.version}</version>
   	</dependency>
   	  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-entitymanager</artifactId>
  		<version>${hibernate.version}</version>
  	</dependency>
   	<dependency>
   		<groupId>org.slf4j</groupId>
   		<artifactId>slf4j-log4j12</artifactId>
   		<version>${sl4j.version}</version>
   	</dependency>
   	<dependency>
   		<groupId>org.apache.struts</groupId>
   		<artifactId>struts2-core</artifactId>
   		<version>${struts2.version}</version>
   	</dependency>
   	<!-- struts整合spring -->
   	<dependency>
   		<groupId>org.apache.struts</groupId>
   		<artifactId>struts2-spring-plugin</artifactId>
   		<version>${struts2.version}</version>
   	</dependency>
   	<!-- struts2注解开发 -->
   	<dependency>
   		<groupId>org.apache.struts</groupId>
   		<artifactId>struts2-convention-plugin</artifactId>
   		<version>${struts2.version}</version>
   	</dependency>
   	<!-- struts2-json插件 -->
   	  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-json-plugin</artifactId>
  		<version>${struts2.version}</version>
  	</dependency>
   	 
   
   	<!-- c3p0 -->
   	<dependency>
   		<groupId>c3p0</groupId>
   		<artifactId>c3p0</artifactId>
   		<version>${c3p0.version}</version>
   	</dependency>
   	<!-- oracle驱动 -->
   	<dependency>
   		<groupId>com.oracle</groupId>
   		<artifactId>ojdbc14</artifactId>
   		<version>${oracle.version}</version>
   	</dependency>
   	<!-- servlet jsp -->
   		<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>servlet-api</artifactId>
  		<version>${servlet.version}</version>
  		<scope>provided</scope>
  	</dependency>
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>jsp-api</artifactId>
  		<version>${jsp.version}</version>
  		<scope>provided</scope>
  	</dependency>
  		<!-- junit -->
  	<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>${junit.version}</version>
	<scope>test</scope>
</dependency>
  	<dependency>
  		<groupId>org.springframework.data</groupId>
  		<artifactId>spring-data-jpa</artifactId>
  		<version>${spring-data.version}</version>
  	</dependency>
  	<!-- poi类库 -->
  		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>${poi.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>${poi.version}</version>
		</dependency>

		<dependency>
			<groupId>pinyin4j</groupId>
			<artifactId>pinyin4j</artifactId>
			<version>2.5.0</version>
		</dependency>
		<!-- cxf -->
				<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency> 
		<!-- asm -->
		<dependency>
			<groupId>asm</groupId>
			<artifactId>asm</artifactId>
			<version>3.3.1</version>
		</dependency>
		
		<!-- hibernate-search jar -->
			<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-search</artifactId>
			<version>${hibernate-search.version}</version>
		</dependency>
		
		<!-- 引入ik 分词器jar -->
		<dependency>
			<groupId>cn.itcast.bos.ik</groupId>
			<artifactId>myik</artifactId>
			<version>2.2.1</version>
		</dependency>
		
		<!--   shiro -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-all</artifactId>
			<version>${shiro.version}</version>
		</dependency>
		
   </dependencies>
   <build>
   	<plugins>
   	     <!-- 配置 hibernate3 maven plugin -->
   	    <plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>hibernate3-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<components>
						<component>
						   <!-- 该命令反向生成 XXX.hbm.xml  -->
							<name>hbm2hbmxml</name>
							<implementation>jdbcconfiguration</implementation>
							 <!-- 生成文件目录 -->
							<outputDirectory>target/generated-resources/hibernate</outputDirectory>
						</component>
						<component>
						   <!-- 该命令反向生成实体类 没有注解 -->
							<name>hbm2java</name>
							<implementation>jdbcconfiguration</implementation>
							<!-- 实体类生成目录 -->
							<outputDirectory>target/generated-sources/hibernate</outputDirectory>
						</component>
					</components>
					<componentProperties>
					   <!-- reveng.xml 定义表生成实体类规则   -->
						<revengfile>src/main/resources/reveng.xml</revengfile>
						<!-- 配置连接连接数据库信息 4个 -->
						<propertyfile>src/main/resources/hibernate.properties</propertyfile>
						<!-- 没有配置reveng.xml 规则  实体类会生成在 下列包下 -->
						<packagename>cn.itcast.bos.domain</packagename>
						<jdk5>true</jdk5>
						<!-- true 生成注解   false 表示不生成注解 -->
						<ejb3>true</ejb3>
					</componentProperties>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>cglib</groupId>
						<artifactId>cglib-nodep</artifactId>
						<version>2.2.2</version>
					</dependency>
					<dependency>
						<groupId>com.oracle</groupId>
						<artifactId>ojdbc14</artifactId>
						<version>${oracle.version}</version>
						<scope>runtime</scope>
					</dependency>
				</dependencies>
			</plugin>
   		<plugin>
   			<groupId>org.apache.tomcat.maven</groupId>
   			<artifactId>tomcat7-maven-plugin</artifactId>
   			<version>2.1</version>
   			<configuration>
   			<port>80</port>
   			</configuration>
   		</plugin>
   	</plugins>
   </build>
</project>


前端通过ocupload进行处理:

<pre name="code" class="javascript">//   添加 ocup
		$("#button-import").upload({
			   name: 'upload',
		        action: '${pageContext.request.contextPath}/bc/regionAction_importData',
		        enctype: 'multipart/form-data',
		        onSelect: function() {
		        	  this.autoSubmit = false;  //  选择文件 阻止表单的请求提交
                    //  var re = /^.+\.xlsx?$/;    //   excel 扩展名  xxxx.xls  .xlsx
                      var re = /^.+[\.xls|\.xlsx]$/;    //   excel 扩展名  xxxx.xls  .xlsx
                      if (re.test(this.filename())) {  
                    	  this.submit();  
                      }  
                      else {  
                          $.messager.alert("警告","必须上传excel","warning");
                      }  
		        },
		        //  等服务器解析excel文件之后   回送data  回调函数...实现
		        onComplete: function(response) {
		        	// {"flag":true}   //  疑问:   是不是一个js 对象?  不是js对象...而是一个字符串 长的是js对象.
		        			//  基础问题  字符串 转换 js 对象!  $()  eval("("+str+")")
		        	  var data = eval("("+response+")");
		        	if(data.flag){
		        		$.messager.alert("恭喜","批量导入成功","info");
		        	}else{
		        		$.messager.alert("错误","服务器...报错啦.导入失败..","error");
		        	}
		        }
		});

action部分代码:
// 1: struts2 接受上传文件 由 默认栈 拦截器 FileUploadInterceptor
	private File upload;

	public void setUpload(File upload) {
		this.upload = upload;
	}

/**
	 * action 利用poi 将excel 文件数据解析 封装到 List<Region> 集合
	 * 
	 * @return
	 * @throws Exception
	 * @throws FileNotFoundException
	 */
	@Action(value = "regionAction_importData")
	public String importData() throws Exception {
		Map<String, Object> results = new HashMap<String, Object>();
		try {
			// poi 解析文件 学习 poi 解析 excel 类库 数据 插入到数据库 region 数据 List<Region>
			// 1: 获取excel文件对象 HSSFWorkBook 对象
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(upload));
			// 2: 获取excel数据 获取 sheet --->row---->cell--->value---->Region.setXXX()--->list.add(region)
			HSSFSheet sheet = workbook.getSheetAt(0);
			List<Region> regions = new ArrayList<Region>();
			for (Row row : sheet) {
				// row 当前sheet 每一个行对象
				// 第一行不要解析 排除
				if (row.getRowNum() == 0) {
					continue;
				}
				// 读取每一行数据
				Region r = new Region();
				r.setId(row.getCell(0).getStringCellValue());// 区域编号 添加数据库表region中 注解采用assigned
				r.setProvince(row.getCell(1).getStringCellValue());
				r.setCity(row.getCell(2).getStringCellValue());
				r.setDistrict(row.getCell(3).getStringCellValue());
				r.setPostcode(row.getCell(4).getStringCellValue());
				// pingyin4j 实现 省市区 简记码生成
				String provice = row.getCell(1).getStringCellValue();// 省
				String city = row.getCell(2).getStringCellValue();// 北京市
				String district = row.getCell(3).getStringCellValue();// 区域
				provice = provice.substring(0, provice.length() - 1);// 北京
				city = city.substring(0, city.length() - 1);// 北京
				district = district.substring(0, district.length() - 1);// 东城
				String[] shortArr = PinYin4jUtils.getHeadByString(provice + city + district);
				String shortcode = getStringFromArray(shortArr);// 抽取方法
				r.setShortcode(shortcode);// 简码 默认
				String[] cityArr = PinYin4jUtils.getHeadByString(city);
				String cityCode = getStringFromArray(cityArr);// 抽取方法
				r.setCitycode(cityCode);// 市去掉即可
				// getStringCellValue() 只能获取 字符类型数据
				regions.add(r);
			}
			// 3: 调用业务层
			regionService.importData(regions);
			results.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			results.put("flag", false);
		}
		getValueStack().push(results);
		return SUCCESS;
	}

	private String getStringFromArray(String[] cityArr) {
		if (cityArr != null && cityArr.length != 0) {
			StringBuffer sb = new StringBuffer();
			for (String s : cityArr) {
				sb.append(s);
			}
			return sb.toString();
		}
		return null;
	}





 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值