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官方网址: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 参考文章:http://heisetoufa.iteye.com/blog/1932073
poi参考文章:http://blog.csdn.net/qq522935502/article/details/8374118
java 读取excel 2007 .xlsx文件 poi实现的更多相关文章
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- Java 读取excel 文件 Unable to recognize OLE stream 错误
原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls).
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java读取excel文件的代码
如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...
- (后台)java 读取excel 文件 Unable to recognize OLE stream 错误
原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls). 光修改文件后缀不行,需要文件另存(或者导出)为 .xls Excel 1997-2004 ...
- 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)
前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
- Java读取excel表格
Java读取excel表格 一般都是用poi技术去读取excel表格的,但是这个技术又是什么呢 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建, ...
- Java读取excel(兼容03和07格式)
读取excel,首先需要下载POI的jar,可以去官网下,也可以在这里下载 一.简单说明 excel2003和excel2007区别比较大,最直观的感受就是扩展名不一样,哈哈 不过,使用POI的API ...
- java 读取excel内容转为JSONArray
需要引入的JAR <!--*.xls--> <dependency> <groupId>net.sourceforge.jexcelapi</grou ...
随机推荐
- 自定义inputformat和outputformat
1. 自定义inputFormat 1.1 需求 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案 1.2 分析 小文件的优 ...
- MySQL中TRUNCATE和ROUND函数的用法
一.TRUNCATE(expr, int_expr)用法 TRUNCATE函数将expr按照int_expr长度在小数点后按照位数直接进行截取. 实例: ); 输出结果:200.1256 二.ROUN ...
- uboot启动完成,kernel启动时lcd屏…
先说说开发环境吧: 1 内核:linux2.6.xx 2 uboot:买开发板带的 注释:在最后我又添加了问题得到完美解决的办法. 问题:uboot启动完成,kernel启动时lcd屏幕出现杂色(比如 ...
- 【HDU3949】XOR
[题目大意] 给定一个数组,求这些数组通过异或能得到的数中的第k小是多少. 传送门:http://vjudge.net/problem/HDU-3949 [题解] 首先高斯消元求出线性基,然后将k按照 ...
- memcache 随笔
第一次用可能有很多不足的地方 以后慢慢改进. memcache 是一个简单的键/值对 是通过键和值储存信息到memcache中 ,通过特定的键请求来返回信息. 信息会无限期的保留在内存中 : ...
- Mips下交叉编译dropbear
1. 编译zlib-1.2.8 在编译dropbear的时候,会遇到“configure: error: *** zlib missing - install first or check confi ...
- python解释器的下载和安装
1.python解释器的下载 python这样的语言,需要一个解释器.而且解释器还有多种语言的实现,我们介绍的是最常用的C语言的实现,称之为Cpython.Python通过在各种操作系统上都有各自的解 ...
- App测试从入门到精通之性能测试
好了,上节我们介绍了关于APP测试的功能测试方面一些细节.这一篇我们来介绍一下,关于APP测试过程中的性能测试参考要点,我们需要思考的如下: 响应时间 1.APP安装卸载的响应时间 2.APP各种功能 ...
- Python基础入门-While循环
讲完了for循环我们继续来看第二个循环,那就是while循环,while循环和for循环虽然都是循环,但是有着本质的不同.我们先来看下她们之间的区别和联系: While循环和for循环区别: 1.fo ...
- Python的split()函数
手册中关于split()用法如下: str.split(sep=None, maxsplit=-1) Return a list of the words in the string, usi ...