【问题标题】:How can I read Excel file in Java?如何在 Java 中读取 Excel 文件?
【发布时间】:2013-10-25 13:04:01
【问题描述】:

如何读取 Java 格式的 XML 格式的 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>

非常感谢您的帮助。 米克

【问题讨论】:

标签: java xml excel xls xlsx


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多