java读取excel2007文件,java 读取excel 2007 .xlsx文件 poi实现

工作需要读取excel里面的行内容,使用java实现较为简单。

在最开始,尝试使用 jxl-2.6.12 来实现读取excel

的行内容。但是按照网上的方法,程序根本无法正确处理文件流。经过谷姐的一番努力,发现jxl只能支持excel 2000而已(或许我用的方法有误)。jxl

操作excel 2007 无望,无奈放弃之。

之后转到apache 的poi 库,看到它的文档里面说到,都可以支持office 2010了,对于2007 应该不在话下。果断转投poi 的怀抱。

我下载的是poi 3.10版本。

解压包后,将下面的jar包加入工程。

20180923120056401049.png

测试poi代码

fcecaa27ea5212ceb9bf034c36bfbf34.gifpackagerw_excel;import static org.junit.Assert.*;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importorg.apache.poi.hssf.extractor.ExcelExtractor;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.ss.usermodel.WorkbookFactory;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importorg.junit.AfterClass;importorg.junit.BeforeClass;importorg.junit.Test;public classtest_poi {

@BeforeClasspublic static void setUpBeforeClass() throwsException {

}

@AfterClasspublic static void tearDownAfterClass() throwsException {

}

@Testpublic void test() throwsIOException {//fail("Not yet implemented");

String file_dir = "test.xlsx";

Workbook book= null;

book=getExcelWorkbook(file_dir);

Sheet sheet= getSheetByNum(book,0);int lastRowNum =sheet.getLastRowNum();

System.out.println("last number is "+lastRowNum);for(int i = 0 ; i <= lastRowNum ; i++){

Row row= null;

row=sheet.getRow(i);if( row != null){

System.out.println("reading line is " +i);int lastCellNum =row.getLastCellNum();

System.out.println("lastCellNum is " +lastCellNum );

Cell cell= null;for( int j = 0 ; j <= lastCellNum ; j++){

cell=row.getCell(j);if( cell != null){

String cellValue=cell.getStringCellValue();

System.out.println("cell value is \n" +cellValue);

}

}

}

}

}public static Sheet getSheetByNum(Workbook book,intnumber){

Sheet sheet= null;try{

sheet=book.getSheetAt(number);//if(sheet == null){//sheet = book.createSheet("Sheet"+number);//}

} catch(Exception e) {throw newRuntimeException(e.getMessage());

}returnsheet;

}public static Workbook getExcelWorkbook(String filePath) throwsIOException{

Workbook book= null;

File file= null;

FileInputStream fis= null;try{

file= newFile(filePath);if(!file.exists()){throw new RuntimeException("文件不存在");

}else{

fis= newFileInputStream(file);

book=WorkbookFactory.create(fis);

}

}catch(Exception e) {throw newRuntimeException(e.getMessage());

}finally{if(fis != null){

fis.close();

}

}returnbook;

}//

}

fcecaa27ea5212ceb9bf034c36bfbf34.gif

excel 文件就在工程的目录下,直接填写了文件名。开始时,写的是绝对路径,可能是windows下 “\”的问题,出现文件不存在的情况。原因不明。

程序非常简单,很多的代码就是拷贝参考文章的。这里感谢“北京大鹏”这位博主,博文写得很详细。

原文:http://www.cnblogs.com/chenfool/p/3632642.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值