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

JavaWeb使用POI操作Excel文件实例

程序员文章站 2024-02-27 18:00:21
1.为项目添加poi poi 点进去之后下载(上边的是编译好的类,下边的是源代码)  解压文件夹,把下面三个文件复制到webcomtent>w...

1.为项目添加poi

poi

JavaWeb使用POI操作Excel文件实例

点进去之后下载(上边的是编译好的类,下边的是源代码)

JavaWeb使用POI操作Excel文件实例

 解压文件夹,把下面三个文件复制到webcomtent>web-inf>lib文件夹下

JavaWeb使用POI操作Excel文件实例

再把这三个文件复制到tomcat的lib文件夹下,否则tomcat会因为找不到类而报错(这个地方郁闷了一上午)

读取“.xls”格式使用  import org.apache.poi.hssf.usermodel.*;包的内容,例如:hssfworkbook

读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:xssfworkbook

读取两种格式使用    import org.apache.poi.ss.usermodel.*    包的内容,例如:workbook

由于我是读取xslx文件所以使用以上几个jar文件。

注意:

JavaWeb使用POI操作Excel文件实例

上图中的两个文件夹中也有我们需要的jar文件,具体是哪几个忘记了(当然为了保险也可以把所有的都放进webcontent>wen-inf>lib下再buildpath进项目),没关系,一会运行的过程中会报错,根据错误信息再去找到相关的jar文件buildpath进去就好,注意还要再tomcat>lib下放置一份副本。

2.读取excel文件

官方教程:链接

类库:

直接看代码吧,不难懂。

JavaWeb使用POI操作Excel文件实例

//遍历一个excel文件<br>private void getexceldata(file file) {
  system.out.println("now in getexceldata" );
  system.out.println("get file name:"+file.getname().tostring());
  xssfworkbook workbook= null;
  try {
   workbook = new xssfworkbook(file);
   int sheetcount = workbook.getnumberofsheets(); //sheet的数量
   system.out.println("num of sheet is : "+sheetcount);
   //遍历每个sheet
   for(int i=0;i<sheetcount;i++)
   {
    xssfsheet sheet = workbook.getsheetat(i);
    //获取总行数
    int rowcount = sheet.getphysicalnumberofrows();
    system.out.println("num of row : "+ rowcount);
    system.out.println("i now in sheet : "+ i);
    //遍历每一行 
    for (int r = 0; r < rowcount; r++) {
     xssfrow row = sheet.getrow(r); 
     //获取总列数 
     int cellcount = row.getphysicalnumberofcells();
     //遍历每一列 
      for (int c = 0; c < cellcount; c++) {
       xssfcell cell = row.getcell(c);
       string cellvalue = null;
       switch (cell.getcelltypeenum()) {
      case string:
       //system.out.println("celltype is string");
       cellvalue = cell.getstringcellvalue(); 
       break;
      case numeric:
       //system.out.println("celltype is number");//整数,小数,日期
       cellvalue = string.valueof(cell.getnumericcellvalue());
       break;
      case boolean:
       //system.out.println("celltype is boolean");
       cellvalue = string.valueof(cell.getbooleancellvalue()); 
       break;
      case formula:
       //system.out.println("celltype is formula");//公式
       cellvalue = "错误,不能为公式"; 
       break;
      case blank:
       //system.out.println("celltype is blank");//空白
       cellvalue = cell.getstringcellvalue();
       break;
      case error:
       //system.out.println("celltype is error");
       cellvalue = "错误"; 
       break;
      default:
       //system.out.println("celltype : default");
       cellvalue = "错误";
       break;       
      }
       system.out.println(cellvalue.tostring());
      }
    }
   }
  } catch (ioexception e) {
   system.out.println("file error ioexception : "+e.getmessage());
  } 
  catch (exception e) {
   // todo: handle exception
  }
  finally {
   try {
    workbook.close();
      } catch (exception e) {
    // todo auto-generated catch block
    e.printstacktrace();
    system.out.println("workbook.close()&fileinputstream.close() error : "+e.getmessage());  
   }
   system.out.println("try catch : finally");   
  }
  system.out.println("hi feipeng8848 getexceldata is done");
 }

以上所述是小编给大家介绍的javaweb使用poi操作excel文件实例,希望对大家有所帮助