java 读取excel 2007 .xlsx文件 poi实现
2014-03-29 16:10
363 查看
工作需要读取excel里面的行内容,使用java实现较为简单。
在最开始,尝试使用 jxl-2.6.12 来实现读取excel 的行内容。但是按照网上的方法,程序根本无法正确处理文件流。经过谷姐的一番努力,发现jxl只能支持excel 2000而已(或许我用的方法有误)。jxl 操作excel 2007 无望,无奈放弃之。
之后转到apache 的poi 库,看到它的文档里面说到,都可以支持office 2010了,对于2007 应该不在话下。果断转投poi 的怀抱。
poi官方网址:http://poi.apache.org/
我下载的是poi 3.10版本。
解压包后,将下面的jar包加入工程。
测试poi代码
excel 文件就在工程的目录下,直接填写了文件名。开始时,写的是绝对路径,可能是windows下 “\”的问题,出现文件不存在的情况。原因不明。
程序非常简单,很多的代码就是拷贝参考文章的。这里感谢“北京大鹏”这位博主,博文写得很详细。
jxl 参考文章:/article/4066876.html
poi参考文章:/article/2369102.html
在最开始,尝试使用 jxl-2.6.12 来实现读取excel 的行内容。但是按照网上的方法,程序根本无法正确处理文件流。经过谷姐的一番努力,发现jxl只能支持excel 2000而已(或许我用的方法有误)。jxl 操作excel 2007 无望,无奈放弃之。
之后转到apache 的poi 库,看到它的文档里面说到,都可以支持office 2010了,对于2007 应该不在话下。果断转投poi 的怀抱。
poi官方网址:http://poi.apache.org/
我下载的是poi 3.10版本。
解压包后,将下面的jar包加入工程。
测试poi代码
package rw_excel; import static org.junit.Assert.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class test_poi { @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Test public void test() throws IOException { // 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,int number){ Sheet sheet = null; try { sheet = book.getSheetAt(number); // if(sheet == null){ // sheet = book.createSheet("Sheet"+number); // } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return sheet; } public static Workbook getExcelWorkbook(String filePath) throws IOException{ Workbook book = null; File file = null; FileInputStream fis = null; try { file = new File(filePath); if(!file.exists()){ throw new RuntimeException("文件不存在"); }else{ fis = new FileInputStream(file); book = WorkbookFactory.create(fis); } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } finally { if(fis != null){ fis.close(); } } return book; } // }
excel 文件就在工程的目录下,直接填写了文件名。开始时,写的是绝对路径,可能是windows下 “\”的问题,出现文件不存在的情况。原因不明。
程序非常简单,很多的代码就是拷贝参考文章的。这里感谢“北京大鹏”这位博主,博文写得很详细。
jxl 参考文章:/article/4066876.html
poi参考文章:/article/2369102.html
相关文章推荐
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- 知识总结-Java 操作 Excel (读取Excel2003 2007,Poi实现)
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- java 操作 Excel (读取Excel2003 2007,Poi实现)
- PIO读取excel文档,java实现
- java实现从数据库读取数据写入excel,写入多个sheet,6万数据耗时100秒附近
- JAVA读取Excel的实现(利用jxl.jar读取excel)
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- Java的poi技术读取Excel[2003-2007,2010]
- Java POI读取Excel 2003/2007/2010例子
- Java POI 读取Office excel (2003,2007)及相关jar包
- java android 读取Excel 2007 xlsx(最新整理,亲测有效)
- java读取excel实现 poi
- XSSF POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现
- Java实现不同excel格式(*.xls、*.xlsx)文件的读取
- Java读取Excel(包括2003和2007)
- poi实现excel读取,实现增加超链接,对于office 2007完美支持
- JAVA读取Excel兼容2003、2007