利用POI导Excel到数据库,2007可以用

来源:互联网 发布:three.js源码下载 编辑:程序博客网 时间:2024/05/17 06:09

<!-- 页面代码 -->

 <div>
  <form id="ExcelAction" name="ExcelAction" action="QyxyAction_addExcel"
   method="post" enctype="multipart/form-data" style="display: none;"
   onsubmit="return checkGs('Excelfile');">
   <table id="ExcelTab" border="0" cellpadding="0" cellspacing="0"
    width="100%" class="tableBorderOther2"
    style="margin-left: 1px; margin: auto;">
    <tr>
     <td class="tdBlank1" height="30" style="width: 17%">导入数据</td>
     <td class="tdBlank2" style="width: 60%"><input id="Excelfile"
      type="file" name="Excelfile" style="width: 85%" /></td>
     <td class="tdBlank1" height="30"
      style="width: 23%; text-align: center"><input type="image"
      id="btnQuery"
      src="<%=request.getContextPath()%>/images/exportExcel.gif" />&nbsp;</td>
            </tr>
          </table>
        </form>
        </div>

<!--后台部分代码-->

         InputStream fis = new FileInputStream(Excelfile);                        //Excelfile 为file
          XSSFWorkbook book = new XSSFWorkbook(fis);
           XSSFSheet sh = book.getSheetAt(0);
          int rowCount = sh.getLastRowNum()+1 - sh.getFirstRowNum();
          for(int i=0;i<rowCount;i++){
           String[] value_name = new String[12];
            XSSFRow row = sh.getRow(i);                                                  //获得行数
            int cols = row.getLastCellNum();                                              //获得列数
            for(int j=0;j<cols;j++){
             value_name[j] = String.valueOf(row.getCell(j));
             if(value_name[j].indexOf(".")>0){
              value_name[j] =  value_name[j].substring(0, value_name[j].lastIndexOf("."));                       //赋值
             }
            }