0

如何以 XML 格式读取 Java 中的 Excel 文件,如下所示?

如果我正在尝试

HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

然后我得到:

Exception in thread "main" java.io.IOException: Invalid header signature; read 0x726576206C6D783C, expected 0xE11AB1A1E011CFD0

如果我正在尝试

XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);

然后我得到:

Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

Excel文件:

<xml version>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-    microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
 <Styles>
 <Style ss:ID="Default" ss:Name="Normal">
 <Alignment ss:Vertical="Bottom"/>
 <Borders/>
 <Font/>
 <Interior/>
 <NumberFormat/>
 <Protection/>
 </Style>
 <Style ss:ID="BoldColumn">
 <Font x:Family="Swiss" ss:Bold="1"/>
 </Style>
 <Style     ss:ID="StringLiteral">
 <NumberFormat ss:Format="@"/>
 </Style>
 <Style ss:ID="Decimal">
 <NumberFormat ss:Format="0.0000"/>
 </Style>
 <Style ss:ID="Integer">
 <NumberFormat ss:Format="0"/>
 </Style>
 <Style ss:ID="DateLiteral">
 <NumberFormat ss:Format="mm/dd/yyyy;@"/>
 </Style>

非常感谢您的帮助。米克

4

1 回答 1

1

XSSF 需要一个包含组件的实际 ZIP 文件。该错误与 OPC 格式需要一个文件这一事实有关[Content_Types].xml

一种解决方案是获取现有的 XLSX 文件,解压缩,替换文件xl/worksheets/sheet1.xml,然后重新压缩。

于 2013-10-27T00:13:37.940 回答