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 ...
随机推荐
- 玩转angularJs——通过自定义ng-model,不仅仅只是input可以实现双向数据绑定
体验更优排版请移步原文:http://blog.kwin.wang/programming/angularJs-user-defined-ngmodel.html angularJs双向绑定特性在开发 ...
- 分析比较多表查询中的IN与JOIN
IN 是子查询的关键字,JOIN 是连接的关键字,项目开发中经常会使用到多表查询,而子查询与连接正是实现多表查询的重要途径.那两者是怎么运行的?IN与JOIN哪个更好?下面就来分析与比较. 现在有te ...
- S3C6410移植u-boot
1.首先下载u-boot(ftp://ftp.denx.de/pub/u-boot) wget ftp://ftp.denx.de/pub/u-boot/u-boot-latest.tar.bz2 2 ...
- codeforces:Roads in the Kingdom分析和实现
题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...
- 【HDU5861】Road
题意 有n个村庄排成一排,有n-1条路将他们连在一起.每条路开放一天都会花费一定数量的钱.你可以选择打开或者关上任意条路在任意一天,但是每条路只能打开和关闭一次.我们知道m天的运输计划.每天都有一辆马 ...
- linux下rsync命令详细整理
点评:rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一详细说明,需要了解的朋友可以参考下-在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命 ...
- IFC标准是为了满足建筑行业的信息交互与共享而产生的统一数据标准,是建 筑行业事实上的数据交换与共享标准。本文概要介绍了IFC标准的产生及发展 历程,IFC的整体框架结构,简要说明了IFC标准的实现方法和过程,描述了 当前的应用以及我们应该更加积极地利用IFC标准为建筑软件行业服务。
- Newtonsoft 序列化和反序列化特殊处理
1>序列化,时间格式化处理 JsonConvert.SerializeObject(Iar, new JsonSerializerSettings() { DateFormatString = ...
- 使对象可以像数组一样foreach循环,要求属性必须是私有的(写个类实现Iterator接口)
<?php class Test implements Iterator { ,,,,); public function __construct() { } // 重置,将数组内部指针指向第一 ...
- SQL 左联接去除左边重复的数据
代码如下: use DB go select table1.*,b.OPTime from [dbo].[table1] left join( select * from (select table2 ...