工作需要读取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实现的更多相关文章

  1. 关于解决java读取excel文件遇空行抛空指针的问题 !

    关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...

  2. Java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls).

  3. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  4. java读取excel文件的代码

    如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...

  5. (后台)java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls). 光修改文件后缀不行,需要文件另存(或者导出)为 .xls Excel 1997-2004 ...

  6. 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...

  7. Java读取excel表格

    Java读取excel表格 一般都是用poi技术去读取excel表格的,但是这个技术又是什么呢 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建, ...

  8. Java读取excel(兼容03和07格式)

    读取excel,首先需要下载POI的jar,可以去官网下,也可以在这里下载 一.简单说明 excel2003和excel2007区别比较大,最直观的感受就是扩展名不一样,哈哈 不过,使用POI的API ...

  9. java 读取excel内容转为JSONArray

    需要引入的JAR     <!--*.xls--> <dependency> <groupId>net.sourceforge.jexcelapi</grou ...

随机推荐

  1. 谈谈contentWindow 和contentDocument以及 iframe

    1,contentWindow:是用来获取子窗口的window对象的,它兼容各大浏览器,用法如下 document.getElementById("iframeId").conte ...

  2. Java之IO输入输出

    首先介绍File类: 我们直接上代码: package com.learn.chap10.sec02; import java.io.File; import java.io.IOException; ...

  3. Net Framework 4.0 和.Net Framework 4.0 Client Profile

    Net Framework 4.0 和.Net Framework 4.0 Client Profile区别: .Net Framework 4.0毫无疑问就像是.Net Framework 2.0一 ...

  4. MYSQL中str_to_date函数的用法

    str_to_date(str,format) 函数的用法 str_to_date函数将str转化为日期型的数据,format表示转化后的格式. format参数格式: 常用: %Y  年 %m  月 ...

  5. 迷你MVVM框架 avalonjs 0.99发布

    在本版本主要是性能优化,添加一些有用的功能(如回调什么的),离成品阶段不远了. 修正 updateViewModel bug 修正监控数组的set方法 bug 添加data-each-rendered ...

  6. 【原】Coursera—Andrew Ng斯坦福机器学习(0)——课程地址和软件下载

    斯坦福大学机器学习 课程信息 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效的网络搜索,让我们对人类基因的解读能力大大提 ...

  7. docker下 zookeeper集群

    首先创建所需的文件夹 mkdir -p /data/{data1,data2,data3} mkdir -p /data/{datalog1,datalog2,datalog3} docker-com ...

  8. 【HDU6024】Building Shops

    题意 有n个教室排成一排,每个教室都有一个坐标,现在,小Q想建一些糖果商店,在这n个教室里面.总的花费有两部分,在教室i建一个糖果屋需要花费ci,对于没有任何糖果屋的P,需要的花费为这个教室到它左边有 ...

  9. android:gravity设置居中的问题

    如果设置一个Button的android:gravity="center" android:text="按钮",则是设置了“按钮”两个字在Button中居中显示 ...

  10. ubunt 14.04 Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul

    CMake Error: Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul ...