欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

POI读取Excel 03/07

程序员文章站 2022-05-18 23:18:28
java application可以成功读取excel 03及excel 07,但是在jsp servlet中却莫名其妙不能读取excel 07(.xlsx)中的内容,只能读取excel 03(.x...
java application可以成功读取excel 03及excel 07,但是在jsp servlet中却莫名其妙不能读取excel 07(.xlsx)中的内容,只能读取excel 03(.xls)中的内容。问题没有解决。
 
/**
 * 需要如下jar包.
 * poi-3.7-20101029.jar, poi-ooxml-3.7-20101029.jar,
 * xmlbeans-2.3.0.jar, poi-ooxml-schemas-3.7-20101029.jar, dom4j-1.6.1.jar
 */
 
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.invalidformatexception;
import org.apache.poi.ss.usermodel.*;
 
public class poiexcelutils {  
    public poiexcelutils() {
    }
    /**
     * 读取excel,支持excel 97~03 / excel 07
     * @param filename : 文件名
     */
    public void read(string filename) {
       workbook wb = null;
       file f = new file(filename);
       fileinputstream is;
       try {
           is = new fileinputstream(f);
           wb = workbookfactory.create(is);
           readwb(wb);
           is.close();
       } catch (filenotfoundexception e) {
           system.out.println(e.getmessage());
       } catch (ioexception e) {
           system.out.println(e.getmessage());
       } catch (invalidformatexception e) {
           system.out.println(e.getmessage());
       }    
    }
    /**
     * 读取excel,支持excel 97~03 / excel 07
     * @param is : 文件流
     */
    public void read(inputstream is) {
       workbook wb;
       try {
           wb = workbookfactory.create(is);
           readwb(wb);
           is.close();
       } catch (invalidformatexception e) {
           system.out.println(e.getmessage());
       } catch (ioexception e) {
           system.out.println(e.getmessage());
       }
     
    }
    /** 
     * 读取workbook 
     * @param wb 
     * @throws exception 
     */ 
    private void readwb(workbook wb){  
        try {  
        // 读取sheet0
            //for (int k = 0; k < wb.getnumberofsheets(); k++) {     
                //sheet  
                //sheet sheet = wb.getsheetat(k); 
             sheet sheet = wb.getsheetat(0);
             readrows(sheet); // 按行读取
             //-- test
             /*
             system.out.println("physicalnumberofrows:"+sheet.getphysicalnumberofrows());
             system.out.println("firstrownum:"+sheet.getfirstrownum());
             system.out.println("lastrownum:"+sheet.getlastrownum());
             */          
            //}  
        } catch (exception e) {     
            system.out.println(e.getmessage());  
        }  
    }
    /**
     * 读取每一行
     * @param rows : 有效行数 /非空行数
     */
    private void readrows(sheet sheet) {
        int rows = sheet.getphysicalnumberofrows();
        system.out.println(rows);
        int rowindex = 0; //每行索引
        int notnullrowindex = 0; //非空行索引
        while (notnullrowindex < rows) {
           row row = sheet.getrow(rowindex);
           rowindex++;
           if (row != null) {
              readcells(row);
              notnullrowindex++;
           }
        }
    }  
    /**
     * 读取每一行的单元格
     * @param row : 所在行数据
     */
    private void readcells(row row) {
    int cells  = row.getphysicalnumberofcells();
       int cellindex = 0; //单元格索引
       int notnullcellindex = 0; //非空单元格索引
       while(notnullcellindex < cells) {
           cell cell = row.getcell(cellindex);
           cellindex++;
           if (cell != null) {
              string value = null;    
              switch (cell.getcelltype()) {                                  
                 case cell.cell_type_formula:  
                     value = "formula value=" + cell.getcellformula();  
                     break;
                 case cell.cell_type_numeric:  
                     if(hssfdateutil.iscelldateformatted(cell)){  
                         value = "date value=" + cell.getdatecellvalue();  
                     }else{  
                         value = "numeric value=" + cell.getnumericcellvalue();  
                     } 
                     break;                 
                  case cell.cell_type_string:  
                     value = "string value=" + cell.getstringcellvalue();  
                     break;               
                  case cell.cell_type_boolean:  
                      value = "boolean value="
                          + cell.getbooleancellvalue();                           
                     break;                
                  default:  
                }  
                notnullcellindex++;  
                system.out.println(value);
           }
       }    
    }
}
作者:angus_17