热门标签 | 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中的文件。


推荐阅读
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了在多平台下进行条件编译的必要性,以及具体的实现方法。通过示例代码展示了如何使用条件编译来实现不同平台的功能。最后总结了只要接口相同,不同平台下的编译运行结果也会相同。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 手把手教你使用GraphPad Prism和Excel绘制回归分析结果的森林图
    本文介绍了使用GraphPad Prism和Excel绘制回归分析结果的森林图的方法。通过展示森林图,可以更加直观地将回归分析结果可视化。GraphPad Prism是一款专门为医学专业人士设计的绘图软件,同时也兼顾统计分析的功能,操作便捷,可以帮助科研人员轻松绘制出高质量的专业图形。文章以一篇发表在JACC杂志上的研究为例,利用其中的多因素回归分析结果来绘制森林图。通过本文的指导,读者可以学会如何使用GraphPad Prism和Excel绘制回归分析结果的森林图。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
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社区 版权所有