前几天被叫去看一个老系统,做好大概几个月了。
说是excel导入出了问题,有一批数据就是不能导入。而且也是excel格式的,看不出问题。
然后我过去本机跑了一下,报的不是业务异常,是jxl读文件的异常,如下:
jxl.read.biff.BiffException: Unable to recognize OLE stream
at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
at jxl.read.biff.File.<init>(File.java:127)
at jxl.Workbook.getWorkbook(Workbook.java:221)
at jxl.Workbook.getWorkbook(Workbook.java:198)
for (int i = 0; i < IDENTIFIER.length; i++)
{
if (data[i] != IDENTIFIER[i])
{
throw new BiffException(BiffException.unrecognizedOLEFile);
}
}
除了让用户另存为xls格式外,比较主动的做法还有:识别html格式的数据,并导入。
JXL得容错性超烂, 有时候格式不对也会出错。
下载的时候要设置http head力的类型是excel.
刚碰到,让用户save as...
昨天试了下jxl,刚好也碰到这个问题,当时是新建了另外一个excel了事,没有去追根问底,多谢楼主
困扰已久,期待各位高手解决!
[quote="h521999"]困扰已久,期待各位高手解决![/quote]
很多网站导出excel都用html或是xhtml。
根本不是BIFF的格式,jxl和poi当然用不了。
vba之类的功能也用不了。
没办法。
[quote="h521999"]困扰已久,期待各位高手解决![/quote]
有条件的还是推荐使用exreport之类的基于dsoframer的
纯excel的导出功能。
汗,我明白了,我以前也老出错,jxl生成的xls ,有一个网页控件怎么也不能识别,但是生成的xls却可以用excel打开!
用poi纯excel的导出
一般在下载的时候都会默认头为text/html,这样会导致你下载的文件格式发生变化,所以,你可以如下设置
public String getContentType(String suffix) {
String contentType = null;
if (suffix.equals(".avi")) {
contentType = "video/avi";
} else if (suffix.equals(".bmp")) {
contentType = "application/x-bmp";
} else if (suffix.equals(".doc")) {
contentType = "application/msword";
} else if (suffix.equals(".gif")) {
contentType = "image/gif";
} else if (suffix.equals(".htm") || suffix.equals(".html")) {
contentType = "text/html";
} else if (suffix.equals(".jpg") || suffix.equals(".jpeg")) {
contentType = "image/jpeg";
} else if (suffix.equals(".mht") || suffix.equals(".mhtml")) {
contentType = "message/rfc822";
} else if (suffix.equals(".mp3")) {
contentType = "audio/mp3";
} else if (suffix.equals(".ppt")) {
contentType = "application/vnd.ms-powerpoint";
} else if (suffix.equals(".pptx")) {
contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
} else if (suffix.equals(".rm")) {
contentType = "application/vnd.rn-realmedia";
} else if (suffix.equals(".rmvb")) {
contentType = "application/vnd.rn-realmedia-vbr";
} else if (suffix.equals(".xls")) {
contentType = "application/x-xls";
} else if (suffix.equals(".xml")) {
contentType = "text/xml";
} else if (suffix.equals(".rar")) {
contentType = "application/octet-stream";
} else if (suffix.equals(".zip")) {
contentType = "application/x-zip-compressed";
} else if (suffix.equals(".swf")) {
contentType = "application/x-shockwave-flash";
} else if (suffix.equals(".wav")) {
contentType = "audio/wav";
} else if (suffix.equals(".txt")) {
contentType = "text/plain";
}
return contentType;
}
[size=x-large][color=red]
下载方法[/color][/size]
public void downLoad(String filePath, String contentType) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
res.sendError(404, "对不起,下载文件没有找到,无法下载!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
res.reset();
[size=x-large][color=red]// 非常重要,设置下载文件的格式[/color][/size]
res.setContentType(contentType + ";charset=utf-8");
res.setHeader("Content-Disposition", "attachment; filename="