关于Java读取Excel2007的文章在Google、百度上搜索一下,没有太好的例子,实现的也不算太好。查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2007(XLSX and PPTX),自己来实现Java读取Excel2007了。
一、老版本案例,08年的poi.jar;下面案例只适用于与解析格式较小的文件,对于较大的文件会出现内存溢出情况。
1,下载 POI 3.5 beta 4 解压,把其中的jar包导入项目文件。以我的读取为例,导入了以下jar包。
没有配置 log4j,测试时报告警报信息,应该为加载顺序导致的初始化问题造成(暂时没有找原因)。
2,建立读取 Excel2007 方法
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(path); //已过时,被废弃,使用会出错
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
String cell;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
cell = row.getCell(j).toString();
System.out.print(cell + "\t");
}
System.out.println("");
}
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(strPath);
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
String cell;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
cell = row.getCell(j).toString();
System.out.print(cell + "\t");
}
System.out.println("");
}
此过程直接传入文件所在路径,其他一些过程已经被自动实现。
3,测试,文件为8列3563行数据,以文本方式读取内容,
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();
String fileName = "F:\\我的文档\\学生缴费代码.xlsx";
// 检测代码
try {
PoiReadExcel er = new PoiReadExcel();
// 读取excel2007
er.testPoiExcel2007(fileName);
} catch (Exception ex) {
Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex);
}
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
}
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();
String fileName = "F:\\我的文档\\学生缴费代码.xlsx";
// 检测代码
try {
PoiReadExcel er = new PoiReadExcel();
// 读取excel2007
er.testPoiExcel2007(fileName);
} catch (Exception ex) {
Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex);
}
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
}
读取所用时间为:4.172秒。
二、java解析大数据分析
一. Apache POI 简介( http://poi.apache.org/)
使用Java程序读写Microsoft Office,提供了下面这几种类型:
HSSF-提供读写Microsoft Excel XLS格式档案的功能。
XSSF-提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF-提供读写Microsoft Word DOC格式档案的功能。
HSLF-供读写Microsoft PowerPoint格式档案的功能。
HDGF-提供读Microsoft Visio格式档案的功能。
HPBF-提供读Microsoft Publisher格式档案的功能。
二、POI操作Excel
1. 官方快速帮助:http://poi.apache.org/spreadsheet/quick-guide.html
2. 导入包:poi-3.6.jar
参考:
1. http://www.blogjava.net/vwpolo/archive/2009/09/16/295243.html
2. http://hacker-zxf.javaeye.com/blog/746546
3. http://zmx.javaeye.com/blog/622536
4. http://canfly2010.javaeye.com/blog/701726
[java] view plaincopyprint?
package excel.poi.input;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException;
public class ReadExcel {
/**
* 读取office 2003 xls
* @param filePath
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public void loadXls(String filePath){
try {
InputStream input = new FileInputStream("D://test.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// Iterate over each row in the sheet
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("Row #" + row.getRowNum());
// Iterate over each cell in the row and print out the cell"s
// content
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println("Cell #" + cell.getCellNum());
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println("unsuported sell type");
break;
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 读取xlsx文本
* @param filePath
*/
public void loadXlsxText(String filePath){
File inputFile = new File("D://test.xlsx");
try {
POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
System.out.println(extractor.getText());
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
} catch (XmlException e) {
e.printStackTrace();
}
}
/**
* 读取office 2007 xlsx
* @param filePath
*/
public void loadXlsx(String filePath){
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = null;
try {
xwb = new XSSFWorkbook("D://test.xlsx");
} catch (IOException e) {