解析Excel文件 Apache POI框架使用
本文整理了,使用Apache POI 框架解析、读取Excel文件,过程中,程序代码出现的一些问题,并解决
1、.xls 和 .xlsx
我们知道Excel文档,在Windows下,分为Excel2003 和 Excel2007.两者有一些区别,最直观的,就是后缀名不一样,分别是 .xls 和 .xlsx
使用Apache POI 解析时,需要区别对待。用不同的API去解析。
但是,却也提供了一个,统一去解析的API,那就是 org.apache.poi.ss.usermodel.WorkbookFactory;
开发,运行的时候,需要导入这样的几个 .jar 包

不然,就会出问题:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
这个错误是由于POI包是默认不支持excel2007操作的,要加入一个xmlbeans的包xbean.jar
使用POI库,在实例化XSSFWorkbook对象时,报 Java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions 的错误,经检查,是因为官方包里默认是不包含xmlbean.jar包的,需要自己添加xmlbeans.jar这个包
public void parseXml(String filename) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File(filename));
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(getCellValue(cell) + "---");
save(getCellValue(cell) + "---");
}
System.out.println();
}
} catch (EncryptedDocumentException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
e.printStackTrace();
}
}
2、解析文件时出现:lang.RuntimeException: Unexpected record type (org.apache.poi.hssf.record.DefaultRowHeightRecord)
解决:excel打开另存一下就可以了
3、一个可以遍历,解析文件夹下,全部Excel文件内容的代码
解析的内容,保存到了一个 .txt 文件中
已调试通过,如下:
public class ParseExcel {
public static void main(String[] args) throws IOException {
String path = "C:\\Users\\文件夹目录路径\\Desktop\\a01hos\\img";
File f = new File(path);
File[] files = f.listFiles();
System.out.println(files.length);
File[] filesxls = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith(".xls") || name.endsWith(".xlsx")) {
return true;
}
return false;
}
});
System.out.println("Excel文件有: " + filesxls.length);
for (File f2 : filesxls) {
String fileDirectPathName = f2.getCanonicalPath();
System.out.println(fileDirectPathName);
// System.out.println("文件名: " + f2.getName());
new ParseExcel().parseXml(fileDirectPathName);
}
}
public void parseXml(String filename) {
Workbook wb = null;
try {
wb = WorkbookFactory.create(new File(filename));
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(getCellValue(cell) + "---");
save(getCellValue(cell) + "---");
}
System.out.println();
}
} catch (EncryptedDocumentException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getCellValue(Cell cell) {
int type = cell.getCellType();
String show = null;
switch (type) {
case Cell.CELL_TYPE_BLANK:// 空值
show = null;
break;
case Cell.CELL_TYPE_BOOLEAN:// Boolean
show = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:// 故障
show = String.valueOf(cell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:// 公式
show = cell.getCellFormula();
break;
case Cell.CELL_TYPE_NUMERIC:// 数字
show = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:// 字符串
show = cell.getStringCellValue();
break;
default:
show = null;
}
return show;
}
/**
* 保存字符串到文本中
*
* @param str
*/
public boolean save(String str) {
boolean flag = false; // 声明操作标记
String fileName = "file/test.txt"; // 定义文件名
File f = new File(fileName);
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fw = null; // 用来写入字符文件的便捷类
PrintWriter out = null; // 向文本输出流打印对象的格式化表示形式类
try {
fw = new FileWriter(f, true); // 创建一个FileWriter
out = new PrintWriter(fw); // 创建一个PrintWriter,以追加方式将内容插入到最后一行
out.println(str); // 将字符串打印到文本中
out.flush(); // 刷新缓存
flag = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭PrintWriter
if (out != null) {
out.close();
out = null;
}
// 关闭FileWriter
if (fw != null) {
fw.close();
fw = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
}
扫个红包吧!

Donate捐赠
如果我的文章帮助了你,可以赞赏我 6.66 元给我支持,让我继续写出更好的内容)

(微信) (支付宝)
微信/支付宝 扫一扫
解析Excel文件 Apache POI框架使用的更多相关文章
- 【Java】使用Apache POI生成和解析Excel文件
概述 Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,分别有jxl和poi,2种方式. HSSF is the POI Project's pure ...
- Excelbatis-一个将excel文件读入成实体列表、将实体列表解析成excel文件的ORM框架,简洁易于配置、可扩展性好
欢迎使用Excelbatis! github地址:https://github.com/log4leo/Excelbatis Excelbatis的优点 和spring天然结合,易于接入 xsd支持, ...
- poi解析Excel文件版本问题
poi解析Excel文件时有两种格式: HSSFWorkbook格式用来解析Excel2003(xls)的文件 XSSFWorkbook格式用来解析Excel2007(xlsx)的文件 如果用HSSF ...
- 解析Excel文件并把数据存入数据库
前段时间做一个小项目,为了同时存储多条数据,其中有一个功能是解析Excel并把其中的数据存入对应数据库中.花了两天时间,不过一天多是因为用了"upload"关键字作为URL从而导致 ...
- vue下载和上传excle数据文件,解析excel文件数据并存在数据库中
下载: VUE: window.open("xxxx/downloadOldTaskDataFile.do_", "_blank"); JAVA: /** * ...
- GrapeCity Documents for Excel 与 Apache POI 功能对比
GrapeCity Documents for Excel 与 Apache POI 功能对比 GrapeCity Documents for Excel 是什么? GrapeCity Documen ...
- Java:JXL解析Excel文件
项目中,有需求要使用JXL解析Excel文件. 解析Excel文件 我们先要将文件转化为数据流inputStream. 当inputStream很大的时候 会造成Java虚拟器内存不够 抛出内存溢出 ...
- c++ 读取并解析excel文件方法
用Cocos开发模型特效工具编辑器,跨Mac和windows,当中有个需求是读取并解析excel文件,但网上的查找的例子几乎都只能是在windows下面使用,再或者是命令行脚本之类的.于是,自己写了一 ...
- Java通过jxl解析Excel文件入库,及日期格式处理方式 (附源代码)
JAVA可以利用jxl简单快速的读取文件的内容,但是由于版本限制,只能读取97-03 xls格式的Excel. 本文是项目中用到的一个实例,先通过上传xls文件(包含日期),再通过jxl进行读取上传 ...
随机推荐
- light oj 1047 - Neighbor House(贪心)
The people of Mohammadpur have decided to paint each of their houses red, green, or blue. They've al ...
- 爬取猫眼电影top100的代码
废话不说,代码附上: #encoding:utf-8 import requests import re import json from multiprocessing import Pool #多 ...
- hdu1028 Ignatius and the Princess III(递归、DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- linux 数据库管理
1.安装数据库: yum install mariadb.serversystemctl staus mariadbsystemctl start mariadbsystemctl enable ma ...
- python3 continue和break 区别
for i in range(10): if i==5: continue #跳出当次循环 if i==8: break #跳出整个for循环 print(i)
- PHP、thinkPHP5.0开发网站文件管理功能(三)编辑文件
public function edit(){ $file = iconv('UTF-8','GB2312',urldecode(input('file'))); if(empty($file)|| ...
- android 闹钟设置问题
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmMana ...
- 理解 Linux backlog/somaxconn 内核参数
https://jaminzhang.github.io/linux/understand-Linux-backlog-and-somaxconn-kernel-arguments/ 各参数的含义:h ...
- (转)Python字典实现三级菜单
Python字典实现三级菜单 原文:https://www.cnblogs.com/pyramid1001/p/5803294.html 1 ############################# ...
- eclipse中使用git下载项目
准备工作: 目的:从远程仓库github上down所需的项目 eclipse使用git插件下载github上项目 eclipse版本:eclipse4.5 64位 jdk版本:jdk-1.7 64位 ...