Java利用POI导入导出Excel中的数据

     首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据。下面POI包的下载地址http://poi.apache.org/download.html,有兴趣的朋友也可以去看看其中的API。

     下面分享一下在对POI进行基本操作时觉得需要注意的两点:

      1.POI中针对xlsx/xls是需要create different Workbook instance HSSFWorkbook是针对xls XSSFWorkbook是针对xlsx的。

   1:  String fileaddress="E:/博客统计.xlsx"; //读取Excel地址
   2:  XSSFWorkbook wb=null;
   3:  File f = new File(fileaddress);// 读取excel文件
   4:  FileInputStream is = new FileInputStream(f);// 创建文件流
   5:  if(fileaddress.toLowerCase().endsWith("xlsx")){
   6:       wb = new XSSFWorkbook(is);  //如果xlsx版本的就创建XSSFWorkbook对象
   7:  } else if(fileName.toLowerCase().endsWith("xls")){  
   8:       wb = new HSSFWorkbook(is);  //如果xls版本的就创建HSSFWorkbook对象
   9:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

      2.在向Excel中导入数据时需要明白sheet中是否有row这个对象,如果有的话就getRow(),gerCell(),setCellValue(),没有的话就需要createRow(),createCell(),setCellValue().

   1:    XSSFSheet sheet = wb.getSheetAt(1); // 第一个工作表  
   2:    int lastrow = sheet.getLastRowNum() + 1;   
   3:    if(rowindex_<lastrow){    
   4:      XSSFRow row = sheet.getRow(rowindex_);// 获得行对象             
   5:      row.getCell(0).setCellValue(bolgname2);      
   6:      row.getCell(1).setCellValue(linktime2);    
   7:      row.getCell(2).setCellValue(linktitle2);      
   8:      row.getCell(3).setCellValue(linkresult2);     
   9:      row.getCell(4).setCellValue(linkred2);      
  10:      row.getCell(5).setCellValue(linkpinglun2);      
  11:      row.getCell(6).setCellValue(tuijianString2);      
  12:      row.getCell(7).setCellValue(fanduiString2);      
  13:      }            
  14:      else{      
  15:       XSSFRow row = sheet.createRow(rowindex_);    
  16:       row.createCell(0).setCellValue(bolgname2);    
  17:       row.createCell(1).setCellValue(linktime2);   
  18:       row.createCell(2).setCellValue(linktitle2);   
  19:       row.createCell(3).setCellValue(linkresult2);   
  20:       row.createCell(4).setCellValue(linkred2);    
  21:       row.createCell(5).setCellValue(linkpinglun2);    
  22:       row.createCell(6).setCellValue(tuijianString2);    
  23:       row.createCell(7).setCellValue(fanduiString2);    
  24:         }
     3.关闭流

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

   1:  FileOutputStream fileOut = new FileOutputStream(fileaddress);
   2:          wb.write(fileOut);
   3:          is.close();
   4:          fileOut.close();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

       最后今天对博客统计修复了一个bug,发现正则表达式在控制字符串格式时很给力但是水很深需要好好学习,就把他当成下一个学习目标,然后下一次解决问题时,脑子里迸出来的就是他。

上一篇:框架的 总结(nop)------添加功能


下一篇:[转]ZooKeeper的学习与应用