`
zcw_java
  • 浏览: 296842 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Poi读取Excel2007

 
阅读更多
jsp页面
<form class="form_0" id="form1" enctype="multipart/form-data" method="post" action="importToContent.aa">
<input class=" wd_50 btn" type="button" name="back" value="返回" onclick="javascript:window.history.go(-1)" />  
<input class=" wd_50 btn" type="button" name="subtn" value="提交" /><label><span class="labelTitle">上传文件地址:</span>
<input type="file" name="excelFile" id="Cname" value="" size="45"/>
	  </form>

后台接收
    //excel文件批量导入短信
    @RequestMapping("/importToContent.aa")
    public ModelAndView importToContent(HttpServletRequest request,@RequestParam("excelFile") MultipartFile file) throws Exception{
        scm.readExcelWriteData(file.getInputStream());
        return new ModelAndView("/frames/so_Groupsms_into");
    }

调用处理
    public int readExcelWriteData(InputStream inp) throws Exception{
        Workbook wb = WorkbookFactory.create(inp);
        int numberRow;
        //获取第一张表
        Sheet st = wb.getSheetAt(0);
        //获取行数   st.getLastRowNum();
        //存储读取的短信
        List smsContentList = new ArrayList();
        String smsCon = null;
        //遍历第一张表的所有行
        for(int i=0;i<=st.getLastRowNum();i++){
        	//第一行一般为title不读取
        	if(i!=0){
        		Row row = st.getRow(i);	//获取第一行数据
        		//遍历第一行所有的列(单元格)
        		for(int j=0;j<row.getLastCellNum();j++){
        			Cell cell = row.getCell(j);  //获取第一个单元格
        			//获取单元格类型
        			switch (cell.getCellType()) {
	                    case Cell.CELL_TYPE_STRING:		//字符串
	                    	System.out.println(cell.getRichStringCellValue().getString());
	                        smsCon = cell.getRichStringCellValue().getString();
	                        break;
	                    case Cell.CELL_TYPE_NUMERIC:	//数字
	                        if (DateUtil.isCellDateFormatted(cell)) {	//判断是否包含日期
	                            System.out.println(cell.getDateCellValue());
	                        } else {
	                            System.out.println(cell.getNumericCellValue());
	                        	smsCon = String.valueOf(cell.getNumericCellValue());
	                        }
	                        break;
	                    case Cell.CELL_TYPE_BOOLEAN:	//布尔
	                        //System.out.println(cell.getBooleanCellValue());
	                        break;
	                    case Cell.CELL_TYPE_FORMULA:	//公式
	                        //System.out.println(cell.getCellFormula());
	                        break;
	                    default:
	                        System.out.println();
	                }
        		}
        	}
        	if(smsCon!=null&&!smsCon.equals("")){
        		smsContentList.add(smsCon);
        	}
        }			
        System.out.println("----------------"+smsContentList.size()+"--------------------");	
        return 0;
    }


pom文件
<dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-stax-api_1.0_spec</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.7</version>
        </dependency>
        <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml-schemas</artifactId>
          <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.7</version>
        </dependency>

不能下载,改写setting文件,添加maven2资源库
   <mirror>
      <id>mav.mt</id>
      <url>http://repo1.maven.org/maven2</url>
      <mirrorOf>central</mirrorOf>
      <!-- Spain, Sevilla -->
   </mirror>
  • 描述: 需要的jar包
  • 大小: 16.3 KB
分享到:
评论
1 楼 qww_friend 2013-12-03  
很不错的一个案例,试了下,没有报错,就是我想要的

相关推荐

Global site tag (gtag.js) - Google Analytics