- 浏览: 334220 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
u013810758:
[flash=200,200][url][img][list] ...
poi 解析excel 03与07不兼容问题 -
u013810758:
我也是这么做的 但是还是有问题啊 感觉07版的或03版的内容 ...
poi 解析excel 03与07不兼容问题 -
sezi915:
我觉得如果用异常来判断Excel版本的话 用org.apach ...
poi 解析excel 03与07不兼容问题 -
他大姨妈:
思路很好,楼主脑子比较好使
poi 解析excel 03与07不兼容问题 -
zhenlong_qu:
谢谢博主的这篇文章,不得不说,博主的思路真不错,按照这个思路, ...
poi 解析excel 03与07不兼容问题
刚开始使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
该错误意思是说,文件中的数据是用Office2007+XML保存的,而现在却调用OLE2 Office文档处理,应该使用POI不同的部分来处理这些数据,比如使用XSSF来代替HSSF。
于是按提示使用XSSF代替HSSF,用new XSSFWorkbook(excelFile)来读取Workbook,对Excel2007没有问题了,可是在读取Excel2003以前(包括2003)的版本时却发生了如下新异常:
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: '*.xls'
该错误是说,操作无效,不能打开指定的xls文件。
下载POI的源码后进行单步调试,发现刚开始的时候还是对的,但到ZipFile类后就找不到文件了,到网上查了下,原来是XSSF不能读取Excel2003以前(包括2003)的版本,这样的话,就需要在读取前判断文件是2003前的版本还是2007的版本,然后对应调用HSSF或XSSF来读取。
这是初步的想法,但这种做法比较麻烦,看了下API,发现XSSF和HSSF虽然在不同的包里,但却引用了同一接口Workbook,于是想到了这样的读取方法:
Workbook book = null;
try {
book = new XSSFWorkbook(excelFile);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(excelFile));
}
在各版本的Excel中测试,没有发生异常,问题解决。
评论
[flash=200,200][url][img][list] |
[*]
|
[/list][/img][/url][/flash] |
// Excel批量导入
public String importExcle() {
int error = 0;
TEmployee employee = (TEmployee) request.getSession().getAttribute(
"employee");
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
// 声明数据流
InputStream is = null;
if (employee != null) {
if (excel != null) {
// 如果出错记住导入到的行和列
int hang = 0;
int lie = 0;
try {
// 解析excel 2007 版本文件
is = new FileInputStream(excel);
XSSFWorkbook work = new XSSFWorkbook(is);
XSSFSheet sheet = work.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
if (sheet != null) {
Map<TImportantCustomerAndTNextFollowDetail, List<TCustomerFollowDetail>> map = new LinkedHashMap<TImportantCustomerAndTNextFollowDetail, List<TCustomerFollowDetail>>();
System.out.println("解析excel 2007版本文件");
// 导入啊 2007版
/*--------------------------------------------------------------------------------------------------------------------------*/
// 循环表的行,从第二行开始
long st07 = System.currentTimeMillis();
for (int i = 1; i < rows; i++) {
TImportantCustomerAndTNextFollowDetail tt = new TImportantCustomerAndTNextFollowDetail();
hang = i + 1;
XSSFRow row = sheet.getRow(i);
if (row != null) {// 判断是否为空
/*--------重要客户信息表------------------------------*/
TImportantCustomer ic = new TImportantCustomer();
if (row.getCell((short) (0)) != null) {
lie = 1;
String v = Util.getXssfCellValue((row.getCell((short) 0)));
ic.setCompanyName(v);
}
if (row.getCell((short) (1)) != null) {
lie = 2;
String v = Util.getXssfCellValue((row.getCell((short) 1)));
ic.setLinkMan(v);
}
if (row.getCell((short) (2)) != null) {
lie = 3;
String v = Util.getXssfCellValue((row.getCell((short) 2)));
ic.setTell(v);
}
if (row.getCell((short) (3)) != null) {
lie = 4;
String v = Util.getXssfCellValue((row.getCell((short) 3)));
ic.setTelephone(v);
}
if (row.getCell((short) (4)) != null) {
lie = 5;
String v = Util.getXssfCellValue((row.getCell((short) 4)));
ic.setEmail(v);
}
if (row.getCell((short) (5)) != null) {
lie = 6;
String v = Util.getXssfCellValue((row.getCell((short) 5)));
ic.setQq(v);
}
if (row.getCell((short) (6)) != null) {
lie = 7;
String v = Util.getXssfCellValue((row.getCell((short) 6)));
ic.setCompanyDetail(v);
}
//并发数
if (row.getCell((short) (7)) != null) {
lie = 8;
String v = Util.getXssfCellValue((row.getCell((short) 7)));
if (StringUtils.hasText(v)) {
ic.setConcurrent(Integer.parseInt(v));
}
}
//行业
if (row.getCell((short) (8)) != null) {
lie = 9;
String v = Util.getXssfCellValue((row.getCell((short)));
if (StringUtils.hasText(v)) {
ic.setIndustry(v);
}
}
//职位
if (row.getCell((short) (9)) != null) {
lie = 10;
String v = Util.getXssfCellValue((row.getCell((short) 9)));
if (StringUtils.hasText(v)) {
ic.setPosition(v);
}
}
//主要负责人
if (row.getCell((short) (10)) != null) {
lie = 11;
String v = Util.getXssfCellValue((row.getCell((short) 10)));
if (StringUtils.hasText(v)) {
ic.setHead(v);
}
}
//合作方式
if (row.getCell((short) (11)) != null) {
lie = 12;
String v = Util.getXssfCellValue((row.getCell((short) 11)));
if (StringUtils.hasText(v)) {
ic.setCooperation(v);
}
}
//公司规模
if (row.getCell((short) (12)) != null) {
lie = 13;
String v = Util.getXssfCellValue((row.getCell((short) 12)));
if (StringUtils.hasText(v)) {
ic.setCompanySize(v);
}
}
//客户级别
if (row.getCell((short) (13)) != null) {
lie = 14;
String v = Util.getXssfCellValue((row.getCell((short) 13)));
if (StringUtils.hasText(v)) {
ic.setCustomerLevel(Integer.parseInt(v));
}
}
//不购买原因
if (row.getCell((short) (14)) != null) {
lie = 15;
String v = Util.getXssfCellValue((row.getCell((short) 14)));
if (StringUtils.hasText(v)) {
ic.setWhyNotBuy(v);
}
}
//需求简介
if (row.getCell((short) (15)) != null) {
lie = 16;
String v = Util.getXssfCellValue((row.getCell((short) 15)));
if (StringUtils.hasText(v)) {
ic.setRequireIntroduction(v);
}
}
/*--------下次跟进表------------------------------*/
TNextFollowDetail nf = new TNextFollowDetail();
if (row.getCell((short) (16)) != null) {
lie = 17;
String v = Util.getXssfCellValue((row.getCell((short) 16)));
nf.setMainRequire(v);
}
if (row.getCell((short) (17)) != null) {
lie = 18;
String v = Util.getXssfCellValue((row.getCell((short) 17)));
nf.setNextFollowDetail(v);
}
if (row.getCell((short) (18)) != null) {
lie = 19;
String v = Util.getXssfCellValue((row.getCell((short) 18)));
if (StringUtils.hasText(v)) {
nf.setNextFollowDate(sf.parse(v));
}
}
if (row.getCell((short) (19)) != null) {
lie = 20;
String v = Util.getXssfCellValue((row.getCell((short) 19)));
nf.setNextFollowMan(v);
}
// 重要客户信息保存
ic.setCreateDate(new Date());
tt.setIc(ic);
// 下次跟进保存
tt.setNf(nf);
/*--------跟进历史记录表------------------------------*/
// 计算循环次数
int k = (row.getLastCellNum() - 19) / 3;
List<TCustomerFollowDetail> nfd = new ArrayList<TCustomerFollowDetail>();
TCustomerFollowDetail icf = null;
for (int j = 0; j < k; j++) {
icf = new TCustomerFollowDetail();
if (row.getCell((short) (3 * j + 20)) != null) {
lie = 3 * j + 21;
String v = Util.getXssfCellValue((row.getCell((short) (3 * j + 20))));
icf.setFollowMan(v);
}
if (row.getCell((short) (3 * j + 21)) != null) {
lie = 3 * j + 22;
String v = Util.getXssfCellValue((row.getCell((short) (3 * j + 21))));
if (StringUtils.hasText(v)) {
icf.setFollowDate(sf.parse(v));
}
}
if (row.getCell((short) (3 * j + 22)) != null) {
lie = 3 * j + 23;
String v = Util.getXssfCellValue((row.getCell((short) (3 * j + 22))));
icf.setFollowContent(v);
}
if (StringUtils.hasText(icf.getFollowMan())|| StringUtils.hasText(icf.getFollowContent())|| icf.getFollowDate() != null) {
// 保存历史跟进
nfd.add(icf);
}
}
map.put(tt, nfd);
}
}
try {
//保存 错误则返回错误行
error = importantCustomerBiz.addExcelInfo(map);
} catch (Exception e) {
}
long ed07 = System.currentTimeMillis();
if (error <= 0) {
request.setAttribute("message",
"excel 2007 导入成功(=^_^=),累计耗时"+ (ed07 - st07) + "毫秒");
} else {
request.setAttribute("message", "从 <<" + error+ "行>> 之后导入失败,累计耗时" + (ed07 - st07) + "毫秒");
}
}
} catch (Exception e) {
/*--------------------------------------------------------------------------------------------------------------------------*/
// 导入啊 2003版
/*--------------------------------------------------------------------------------------------------------------------------*/
try {
// 解析excel 2003 版本文件
is = new FileInputStream(excel);
HSSFWorkbook work = new HSSFWorkbook(is);
HSSFSheet sheet = work.getSheetAt(0);
if (sheet != null) {
Map<TImportantCustomerAndTNextFollowDetail, List<TCustomerFollowDetail>> map = new LinkedHashMap<TImportantCustomerAndTNextFollowDetail, List<TCustomerFollowDetail>>();
System.out.println("解析excel 2003 版本文件");
int rows = sheet.getPhysicalNumberOfRows();
// 循环表的行,从第二行开始
long st03 = System.currentTimeMillis();
for (int i = 1; i < rows; i++) {
TImportantCustomerAndTNextFollowDetail tt = new TImportantCustomerAndTNextFollowDetail();
hang = i + 1;
HSSFRow row = sheet.getRow(i);
if (row != null) {// 判断是否为空
/*--------重要客户信息表------------------------------*/
TImportantCustomer ic = new TImportantCustomer();
if (row.getCell((short) (0)) != null) {
lie = 1;
String v = Util.getHssfCellValue((row.getCell((short) 0)));
ic.setCompanyName(v);
}
if (row.getCell((short) (1)) != null) {
lie = 2;
String v = Util.getHssfCellValue((row.getCell((short) 1)));
ic.setLinkMan(v);
}
if (row.getCell((short) (2)) != null) {
lie = 3;
String v = Util.getHssfCellValue((row.getCell((short) 2)));
ic.setTell(v);
}
if (row.getCell((short) (3)) != null) {
lie = 4;
String v = Util.getHssfCellValue((row.getCell((short) 3)));
ic.setTelephone(v);
}
if (row.getCell((short) (4)) != null) {
lie = 5;
String v = Util.getHssfCellValue((row.getCell((short) 4)));
ic.setEmail(v);
}
if (row.getCell((short) (5)) != null) {
lie = 6;
String v = Util.getHssfCellValue((row.getCell((short) 5)));
ic.setQq(v);
}
if (row.getCell((short) (6)) != null) {
lie = 7;
String v = Util.getHssfCellValue((row.getCell((short) 6)));
ic.setCompanyDetail(v);
}
//并发数
if (row.getCell((short) (7)) != null) {
lie = 8;
String v = Util.getHssfCellValue((row.getCell((short) 7)));
if (StringUtils.hasText(v)) {
ic.setConcurrent(Integer.parseInt(v));
}
}
//行业
if (row.getCell((short) (8)) != null) {
lie = 9;
String v = Util.getHssfCellValue((row.getCell((short)));
if (StringUtils.hasText(v)) {
ic.setIndustry(v);
}
}
//职位
if (row.getCell((short) (9)) != null) {
lie = 10;
String v = Util.getHssfCellValue((row.getCell((short) 9)));
if (StringUtils.hasText(v)) {
ic.setPosition(v);
}
}
//主要负责人
if (row.getCell((short) (10)) != null) {
lie = 11;
String v = Util.getHssfCellValue((row.getCell((short) 10)));
if (StringUtils.hasText(v)) {
ic.setHead(v);
}
}
//合作方式
if (row.getCell((short) (11)) != null) {
lie = 12;
String v = Util.getHssfCellValue((row.getCell((short) 11)));
if (StringUtils.hasText(v)) {
ic.setCooperation(v);
}
}
//公司规模
if (row.getCell((short) (12)) != null) {
lie = 13;
String v = Util.getHssfCellValue((row.getCell((short) 12)));
if (StringUtils.hasText(v)) {
ic.setCompanySize(v);
}
}
//客户级别
if (row.getCell((short) (13)) != null) {
lie = 14;
String v = Util.getHssfCellValue((row.getCell((short) 13)));
if (StringUtils.hasText(v)) {
ic.setCustomerLevel(Integer.parseInt(v));
}
}
//不购买原因
if (row.getCell((short) (14)) != null) {
lie = 15;
String v = Util.getHssfCellValue((row.getCell((short) 14)));
if (StringUtils.hasText(v)) {
ic.setWhyNotBuy(v);
}
}
//需求简介
if (row.getCell((short) (15)) != null) {
lie = 16;
String v = Util.getHssfCellValue((row.getCell((short) 15)));
if (StringUtils.hasText(v)) {
ic.setRequireIntroduction(v);
}
}
/*--------下次跟进表------------------------------*/
TNextFollowDetail nf = new TNextFollowDetail();
if (row.getCell((short) (16)) != null) {
lie = 17;
String v = Util.getHssfCellValue((row.getCell((short) 16)));
nf.setMainRequire(v);
}
if (row.getCell((short) (17)) != null) {
lie = 18;
String v = Util.getHssfCellValue((row.getCell((short) 17)));
nf.setNextFollowDetail(v);
}
if (row.getCell((short) (18)) != null) {
lie = 19;
String v = Util.getHssfCellValue((row.getCell((short) 18)));
if (StringUtils.hasText(v)) {
nf.setNextFollowDate(sf.parse(v));
}
}
if (row.getCell((short) (19)) != null) {
lie = 20;
String v = Util.getHssfCellValue((row.getCell((short) 19)));
nf.setNextFollowMan(v);
}
// 重要客户信息保存
ic.setCreateDate(new Date());
tt.setIc(ic);
// 下次跟进保存
tt.setNf(nf);
/*--------跟进历史记录表------------------------------*/
// 计算循环次数
int k = (row.getLastCellNum() - 19) / 3;
List<TCustomerFollowDetail> nfd = new ArrayList<TCustomerFollowDetail>();
TCustomerFollowDetail icf = null;
for (int j = 0; j < k; j++) {
icf = new TCustomerFollowDetail();
if (row.getCell((short) (3 * j + 20)) != null) {
lie = 3 * j + 21;
String v = Util.getHssfCellValue((row.getCell((short) (3 * j + 20))));
icf.setFollowMan(v);
}
if (row.getCell((short) (3 * j + 21)) != null) {
lie = 3 * j + 22;
String v = Util.getHssfCellValue((row.getCell((short) (3 * j + 21))));
if (StringUtils.hasText(v)) {
icf.setFollowDate(sf.parse(v));
}
}
if (row.getCell((short) (3 * j + 22)) != null) {
lie = 3 * j + 23;
String v = Util.getHssfCellValue((row.getCell((short) (3 * j + 22))));
icf.setFollowContent(v);
}
if (StringUtils.hasText(icf.getFollowMan())|| StringUtils.hasText(icf.getFollowContent())|| icf.getFollowDate() != null) {
// 保存历史跟进
nfd.add(icf);
}
}
map.put(tt, nfd);
}
}
try {
error = importantCustomerBiz.addExcelInfo(map);
} catch (Exception e2) {
e2.printStackTrace();
}
long ed03 = System.currentTimeMillis();
if (error <= 0) {
request.setAttribute("message","excel 2007 导入成功(=^_^=),累计耗时"+ (ed03 - st03) + "毫秒");
} else {
request.setAttribute("message", "从 <<"+ error + "行>> 之后导入失败,累计耗时"+ (ed03 - st03) + "毫秒");
}
}
} catch (Exception e1) {
request.setAttribute("message", "从第" + hang + "行,第"+ lie + "列读取有误!,请检查");
e1.printStackTrace();
}
}
}
} else {
request.setAttribute("message", "登录超时!");
}
// 查询时对象中含有值对查询有误
if (icustomer != null) {
icustomer = null;
}
return icustomerList();
}
我在new HSSFWorkbook时new2007的会报这个错 直接用Exception有点太笼统了吧 因为我还知道一个org.apache.poi.hssf.OldExcelFormatException的错 这是弄Ecxel5.0/95出的错 这个用XSSF也处理不了
//创建相关的文件流对象 in = new FileInputStream(excelFile); //声明相关的工作薄对象 Workbook wb =null; //声明相关的excel抽取对象 ExcelExtractor extractor=null; String fileName = excelFile.getName(); String hz = fileName.substring(fileName.lastIndexOf("."),fileName.length()); if(hz.equals(".xls"))//针对2003版本 { //创建excel2003的文件文本抽取对象 wb=new HSSFWorkbook(new POIFSFileSystem(in)); extractor =new org.apache.poi.hssf.extractor.ExcelExtractor((HSSFWorkbook)wb); }else{ //针对2007版本 wb = new XSSFWorkbook(in); //创建excel2007的文件文本抽取对象 extractor =new XSSFExcelExtractor((XSSFWorkbook)wb); }
这样应该更合适一点
呵呵,谢谢夸奖
if (excelFileFileName.toLowerCase().endsWith("xls")) {
return new HSSFWorkbook(is); //这行报错! }
if (excelFileFileName.toLowerCase().endsWith("xlsx")) {
return new XSSFWorkbook(is);
}
return null;
发表评论
-
http断点续传
2014-07-24 13:31 905Http文件下载的普通模式就不多说了,断点下载与普通模式不一 ... -
修改Tomcat的运行内存 & JSP中查看当前的内存使用状况
2014-03-24 16:00 992一、设置JVM内存设置 1. 设置JVM内存的参数有 ... -
Eclipse编码格式修改
2014-03-21 09:13 770如果要使插件开发应 ... -
标题叠加在图片上面的几种显示方式
2014-02-24 10:55 1472很多新改版的网站都会在图片显示上下一些文章,比如加 ... -
java druid淘宝数据库连接池数据库监听
2013-12-26 15:40 13449java程序很大一部分 要操作数据库,为了提高性能操作数据库 ... -
memcached- tomcat配置详解
2013-12-04 18:08 1404msmconfigurationmemcached-se ... -
Oracle中将小数转换成字符丢零.截取小数.除数为零解决法
2013-11-26 14:33 82251.小数转换成字符往往会丢失前面的零.解决方法_例:to_c ... -
java 垃圾回收配置
2013-10-22 14:16 4431一、JVM内存模型及垃 ... -
nginx配置详解
2013-10-18 14:45 1038user nginx ; #用户 ... -
oracle 列转行
2013-10-11 21:15 16272业务中做报表,需要将一列列数据汇总成一行,然后汇总,如下: ... -
osgi 注册servlet 和资源
2013-10-08 10:02 1655在Activator类中 public void s ... -
正则表达式 匹配url
2013-07-09 14:27 1526正则表达式 (http|ftp|https):\ ... -
ZooKeeper 集群配置
2013-07-05 14:40 1167ZooKeeper 是一个分布式开源框架,提供了协调 ... -
jdk logger 配置实用宝典
2013-06-18 15:45 1788日志输出是所有系统必备的,很多开发人员可能因为常常使用log ... -
CSS实现兼容性的渐变背景(gradient)效果
2013-06-05 14:32 976CSS实现兼容性的渐变背 ... -
jinterop获取windows事件
2013-05-27 13:42 1793Java代码 package com ... -
jcifs windows主机之间进行网络文件共享
2013-05-20 18:05 1139CIFS (Common Internet File ... -
java调用windows的wmi
2013-05-20 14:34 2037100%纯java调用windows的wmi获取监控数据 ... -
java调用windows的wmi
2013-05-20 14:32 3964100%纯java调用windows的wmi获取监控数据 ... -
tomcat 域名
2013-03-14 14:44 18841. 下载安装版本的Tomcat(或者压缩版的,只不过要自己配 ...
相关推荐
使用poi解析excel文件,并将数据写入到数据库 项目说明 这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL...
POI解析Excel简单实例
poi解析excel2003、2007 poi解析excel2003、2007 poi解析excel2003、2007
通过Java Poi 解析EXCEL数据,内附实现代码,绝对可以用。要分不为别的,只为能给个评论。
POI 解析excel,转成html页面(支持excel03和07)包括style
通过poi解析Excel示例
采用poi3.8解析excel文件,内含实例代码和jar包
使用poi解析excel将数据存入Oraclo所用的joi包
POI 解析excel,转成html页面(支持excel03和07)包括style
自己写的一个poi解析excel 的工具类。里面有些实用的方法,满足基本的解析要求了。
自己做的poi解析excel,包括样式等等都已经解析,可以直接拿去就用的。欢迎大家测试使用,有BUG可以及时联系我。
在使用poi解析Excel时,会遇缺少很多的包,这里都包括了
最新版poi解析excel2007源码,简单实用,需要的可下.
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
apache poi解析Excel时报错,我将Excel打开后保存,就不会报错,我将重写的类从新打包这样问题虽然网上有解决方案,但是需要自己打包重写对应类,我已将这些步骤做好了。
一个解单的java工程,利用poi解析excel.下载代码直接可以运行。
1.不分版本,2003/2007完美解析 2.调用方便 3.输出直观 4.解析excel工程demo
POI解析Excel2007
附件包含一个web项目,是一个简单的解析excel和word的例子,项目中包含jar poi解析excel、word2007,2010等版本
poi excel poi excel poi excel poi excel poi excel poi excel poi excel poi excel