Java读取Excel中的单元格数据_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java读取Excel中的单元格数据

Java读取Excel中的单元格数据

 2018/5/11 12:56:42  妮子321  程序员俱乐部  我要评论(0)
  • 摘要:目前网上能找到的读取Excel表格中数据的两种比较好的方案:PageOffice好用开发效率高;POI免费。供大家参考,针对具体情况选择具体方案。1.PageOffice读取excelimportcom.zhuozhengsoft.pageoffice.*;importcom.zhuozhengsoft.pageoffice.excelreader.*;//读取Excel单元格数据WorkbookworkBook=newWorkbook(request,response)
  • 标签:excel Java 数据

目前网上能找到的读取Excel表格中数据的两种比较好的方案:PageOffice好用开发效率高;POI免费。供大家参考,针对具体情况选择具体方案。

  1.?PageOffice读取excel

  

class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8; font-size: 12px !important;">复制代码
    import com.zhuozhengsoft.pageoffice.*;
    import com.zhuozhengsoft.pageoffice.excelreader.*;

    //读取Excel单元格数据
    Workbook workBook = new Workbook(request, response);
    Sheet sheet = workBook.openSheet("Sheet1");
    Table table = sheet.openTable("A1:F5");while (!table.getEOF()) {
        //获取提交的数值
        if (!table.getDataFields().getIsEmpty()) {
for(int i=0; i<6; i++){ String content
= table.getDataFields().get(i).getText(); System.out.println(content);//输出单元格数据
}
} //循环进入下一行 table.nextRow(); } table.close(); workBook.close();
复制代码

  这些代码只是在服务器端接收客户端提交的excel单元格数据,PageOffice真正的读取单元格数据工作是在客户端执行的,由于服务器端并没有对excel文件做任何读取操作,只接受一下数据就行,无需耗费大量资源去处理文件,也无需处理多个客户并发请求的问题,因为每个客户端都各自读取自己的数据,服务器端只接收数据保存到数据库。

  2.?poi读取excel

  

复制代码
package com.test.poi;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
 
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
 
public class ReadExcel {
 
public static void main(String[] args) throws Exception {
    HSSFWorkbook wb = null;
    POIFSFileSystem fs = null;
    try {
      fs = new POIFSFileSystem(new FileInputStream("e:\\workbook.xls"));
      wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
      e.printStackTrace();
    }
 
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(0);
    HSSFCell cell = row.getCell(0);
    String msg = cell.getStringCellValue();
    System.out.println(msg);
}
public static void method2() throws Exception {
 
    InputStream is = new FileInputStream("e:\\workbook.xls");
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
 
    ExcelExtractor extractor = new ExcelExtractor(wb);
    extractor.setIncludeSheetNames(false);
    extractor.setFormulasNotResults(false);
    extractor.setIncludeCellComments(true);
 
    String text = extractor.getText();
    System.out.println(text);
}
 
public static void method3() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("e:\\workbook.xls"));
    HSSFSheet sheet = wb.getSheetAt(0);
 
    for (Iterator<Row> iter = (Iterator<Row>) sheet.rowIterator(); iter.hasNext();) {
      Row row = iter.next();
      for (Iterator<Cell> iter2 = (Iterator<Cell>) row.cellIterator(); iter2.hasNext();) {
        Cell cell = iter2.next();
        String content = cell.getStringCellValue();// 除非是sring类型,否则这样迭代读取会有错误
        System.out.println(content);
      }
    }
}
}
复制代码

  注意:HSSFWorkbook,XSSFWorkbook的区别:前者是解析出来excel 2007 以前版本的,后缀名为xls的,后者是解析excel 2007 版的,后缀名为xlsx。需要针对不同的excel版本编写不同的程序,判断该用哪个workbook来对其进行解析处理,而且通常需要开发人员自己把这些方法都做相应封装,使其更面向对象,工作量有点大,还有就是要求开发高质量代码,因为是在服务器上执行的,必须解决多个客户同时请求的并发问题,和万一程序执行异常的处理问题,上例只是main方法的简单示例而已,仅供参考!

发表评论
用户名: 匿名