热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用Java加载旧的MicrosoftOfficeXML文件-HowtoloadoldMicrosoftOfficeXMLfile(Excel)usingJava

ImnotabletoloadanExcelfileintheolderOfficeXMLformat(thinkOffice2002or2003version

I'm not able to load an Excel file in the older Office XML format (think Office 2002 or 2003 version) into Java. I tried JXL and Apache's POI (version 3.7). POI doesn't work since it appears to want the newer Office .xlsx format.

我无法将旧的Office XML格式(请考虑Office 2002或2003版本)的Excel文件加载到Java中。我尝试了JXL和Apache的POI(版本3.7)。POI不能工作,因为它似乎想要更新的Office .xlsx格式。

Here's an example of the older Office XML format.

这里有一个旧的Office XML格式的示例。

One can generate a similar XML file from MS Excel 2010 by saving the workbook as the format "XML Spreadsheet 2003"?

您可以通过将工作簿保存为格式“XML Spreadsheet 2003”,从MS Excel 2010中生成类似的XML文件。

Are there any open-source Java libraries that will load the XMLSS format? Otherwise I have no choice but to write a custom parser: read the XML file then interpret the cell tags to build out the cell matrix. In this XML format, any rows with empty cell values are skipped, the next cell with data positioned with an index attribute that acts like an offset in the columns, I assume to save space in the XML file.

是否有任何开源的Java库将加载XMLSS格式?否则,我别无选择,只能编写一个自定义解析器:读取XML文件,然后解释单元格标记,构建单元格矩阵。在这种XML格式中,任何空单元格值的行都被跳过,下一个单元格的数据定位为一个索引属性,它的作用类似于列中的偏移量,我假设要在XML文件中保存空间。

6 个解决方案

#1


3  

The format is called SpreadsheetML (do not confuse with .xlsx which is also xml-based), a library called Xelem can handle it:

这种格式称为SpreadsheetML(不要和基于xml的.xlsx混淆),一个名为Xelem的库可以处理它:

import nl.fountain.xelem.excel.Workbook;
import nl.fountain.xelem.lex.ExcelReader;
//...
ExcelReader reader = new ExcelReader();
Workbook xlWorkbook = reader.getWorkbook("c:\\my\\spreadsheet.xml");
System.out.println(xlWorkbook.getSheetNames());

#2


2  

Copying Mark Beardsley's answer from POI team http://apache-poi.1045710.n5.nabble.com/How-to-convert-xml-to-xls-td2306602.html :

从POI团队http://apache-poi.1045710.n5.nabble.com/How-to-convert-xml-to-xls-td2306602.html复制Mark Beardsley的答案:

You have got an Office 2003 xml file there, not an OpenXML file; it is an early attempt by Microsoft to create an xml based file format for Excel and it is in that sense a 'valid' Office file format.

这里有一个Office 2003 xml文件,不是OpenXML文件;这是微软为Excel创建基于xml的文件格式的早期尝试,从这个意义上说,这是一种“有效的”Office文件格式。

Sadly, POI cannot interpret this file at all and that is why you saw the exception when you tried to wrap it up in the InputStream and pass it to WorkbookFactory(s) constructor. You do however have a number of options;

遗憾的是,POI根本无法解释这个文件,这就是为什么当您试图在InputStream中包装它并将它传递给WorkbookFactory(s)构造函数时,您会看到异常。但是你有很多选择;

  • You could use Excel itself and manually open and save each file you wish to convert, as you already have done.
  • 您可以使用Excel本身,手动打开并保存希望转换的每个文件,就像您已经做过的那样。
  • If you have access to Visual Studio and can write Visual Basic or C# code then you could use a control that will allow you to control Excel programmatically. This way you could automate a file conversion process using Excel itself. Then once the file has been converted wither to the binary or OpenXML formats, POI can be used to process it.
  • 如果您可以访问Visual Studio,并且可以编写Visual Basic或c#代码,那么您可以使用一个控件,它将允许您以编程方式控制Excel。通过这种方式,您可以使用Excel本身自动化文件转换过程。然后,一旦文件被转换成二进制或OpenXML格式,就可以使用POI来处理它。
  • If you are running on a stand alone PC on which a copy of Excel is installed and using the Windows operating system, then you could use OLE to do something very similar from Java code. As above, POI can be used to process the file following the conversion.
  • 如果您在安装了Excel副本的独立PC上运行,并使用Windows操作系统,那么您可以使用OLE来完成与Java代码非常相似的工作。如上所述,POI可用于在转换之后处理文件。
  • If you have access to OpenOffice, it has a rather good API that is accessible from Java code. You could use it to convert between the file types for you - it is simply a matter of discovering the correct filter to use in this case. OpenOffice is good for all except the most complex files and you should be able to use POI to process the file following conversion. However, if you choose this route, it may be best to do all of the work using OpenOffice's UNO api.
  • 如果您可以访问OpenOffice,它有一个相当好的API,可以从Java代码访问。您可以使用它在文件类型之间进行转换——这只是在这种情况下发现正确的过滤器的问题。OpenOffice适用于除最复杂的文件之外的所有文件,您应该能够在转换后使用POI来处理文件。但是,如果您选择此路径,那么最好使用OpenOffice的UNO api完成所有工作。
  • Depending upon what you want to do with the file's contents, you could create your own parser using core java code and either the SAX or Xerces parsers (consider using xmlBeans (http://xmlbeans.apache.org/) ). If you simply open the original xml file using a simple text editor, you can see that the structure is not complex and, if all you wish to get at is the raw data it contains, this could be your best option.
  • 根据您想对文件内容做什么,您可以使用核心java代码和SAX或Xerces解析器(考虑使用xmlBeans (http://xmlbeans.apache.org/)创建自己的解析器。如果您只是使用一个简单的文本编辑器打开原始的xml文件,您可以看到该结构并不复杂,如果您只想获得它所包含的原始数据,那么这可能是您最好的选择。

#3


1  

After a lot of pain I've found a solution to this. JODConverter uses the OpenOffice.org/LibreOffice API and can convert SpreadsheetML to whatever formats OpenOffice.org suppports.

在经历了很多痛苦之后,我找到了解决办法。JODConverter使用OpenOffice.org/LibreOffice API,可以将SpreadsheetML转换为OpenOffice.org的任何格式。

#4


0  

You might get some result using the OpenOffice API. If not directly you could probably convert to a 'supported' format. Otherwise the schema for the Office 2003 'SpreadsheetML' isn't very complicated. I have succesfully created an xslt scenario to convert a resultset (database query) to a (simple yet effective) Excel 2003 document (XML format). The other way around should not be very hard to achieve.

您可能会使用OpenOffice API得到一些结果。如果不能直接转换成“受支持的”格式。否则,Office 2003“SpreadsheetML”的架构就不是很复杂了。我成功地创建了一个xslt场景,将resultset(数据库查询)转换为Excel 2003文档(XML格式)。另一种方法不应该很难实现。

Cheers, Wim

欢呼,Wim

#5


0  

The answer today was to ask the vendor to change their Excel file format to an Excel binary rather than the old Office XML. Doing so allowed me to use Apache POI 3.7 to read the file with no issues. I appreciate the answers, as I had no idea there was no direct support in the Java-based open source libraries for this old Office XML format. Now I know next time to check earlier to see what format the Excel files are in before committing to a timeline.

今天的答案是要求供应商将他们的Excel文件格式改为Excel二进制文件,而不是旧的Office XML。这样做让我可以使用Apache POI 3.7来读取文件,没有问题。我很欣赏这些答案,因为我不知道在基于java的开放源码库中没有直接支持这种旧的Office XML格式。现在我知道下次在提交时间轴之前要检查Excel文件的格式。

#6


0  

I had the same problem some time ago, ended up writing a SAX parser to read the XML file. I wrote a blog post about it here.

不久前我遇到了同样的问题,最后我编写了一个SAX解析器来读取XML文件。我在这里写了一篇博客。

You can find the sample project to parse the file in Github.

您可以找到示例项目来解析Github中的文件。


推荐阅读
  • 本文由编程笔记小编整理,主要介绍了使用Junit和黄瓜进行自动化测试中步骤缺失的问题。文章首先介绍了使用cucumber和Junit创建Runner类的代码,然后详细说明了黄瓜功能中的步骤和Steps类的实现。本文对于需要使用Junit和黄瓜进行自动化测试的开发者具有一定的参考价值。摘要长度:187字。 ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • Ubuntu 9.04中安装谷歌Chromium浏览器及使用体验[图文]
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • 本文介绍了禅道作为一款国产开源免费的测试管理工具的特点和功能,并提供了禅道的搭建和调试方法。禅道是一款B/S结构的项目管理工具,可以实现组织管理、后台管理、产品管理、项目管理和测试管理等功能。同时,本文还介绍了其他软件测试相关工具,如功能自动化工具和性能自动化工具,以及白盒测试工具的使用。通过本文的阅读,读者可以了解禅道的基本使用方法和优势,从而更好地进行测试管理工作。 ... [详细]
  • mac php错误日志配置方法及错误级别修改
    本文介绍了在mac环境下配置php错误日志的方法,包括修改php.ini文件和httpd.conf文件的操作步骤。同时还介绍了如何修改错误级别,以及相应的错误级别参考链接。 ... [详细]
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
author-avatar
达达2502854565
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有