0

How can I read Excel file in Java in XML format like below?

If I'm trying

HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

then I getting :

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

If I'm trying

XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);

then I getting :

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

Excel file:

<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>

Many thanks for any help. Mik

flyx
  • 35,506
  • 7
  • 89
  • 126
javalonde
  • 549
  • 2
  • 6
  • 7

1 Answers1

1

XSSF expects an actual ZIP file with the components. The error relates to the fact that the OPC format requires a file [Content_Types].xml.

One solution is to take an existing XLSX file, unzip it, replace the file xl/worksheets/sheet1.xml and then re-zip.

SheetJS
  • 22,470
  • 12
  • 65
  • 75