0

Is it possible to open an excel xml ( an xml which can open in microsoft office excel ) and it is a perfectly good xml that complies with xml spreadsheet reference

I tried to use :

String outputFile = "output.xml";
FileInputStream file = new FileInputStream( new File( outputFile ) );
Workbook wb = WorkbookFactory.create( new POIFSFileSystem( file ) );
Sheet sheet = wb.getSheetAt(0);

It shows me the below exception :

java.io.IOException: Invalid header signature; read 0x6576206C6D783F3C, expected 0xE11AB1A1E011CFD0
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
SomeDude
  • 13,876
  • 5
  • 21
  • 44
  • Can you try with Apache POI 3.15 beta 2? That tends to give more helpful error messages with unsupported files. Also, [why are you using an InputStream when you have a file? It uses more memory and is slower!](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream) – Gagravarr Jul 12 '16 at 22:33
  • @Gagravarr Unfortunately I cannot use 3.15 and using file or inputstream doesn't matter. It gives the same error. So it seems 3.9 or 3.6 cannot handle excel xml ? – SomeDude Jul 13 '16 at 15:15

1 Answers1

0

It seems in newer Excel versions encryption is often used. For me the following code works

InputStream fin = new FileInputStream(xlsFile);
BufferedInputStream in = new BufferedInputStream(fin);
if (POIFSFileSystem.hasPOIFSHeader(in)) {
    // if there is any encryption
    POIFSFileSystem fs = new POIFSFileSystem(in);
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = Decryptor.getInstance(info);
    d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
    workbook = WorkbookFactory.create(d.getDataStream(fs));
}
else
    // without encryption
    workbook = WorkbookFactory.create(in);
OkieOth
  • 3,604
  • 1
  • 19
  • 29
  • It doesn't work for me. I still get same error. I am shifting to a parser now. – SomeDude Jul 12 '16 at 19:44
  • @svasa if you can publish the excel file I can do tomorrow a simple test with it – OkieOth Jul 12 '16 at 19:48
  • Sorry, I cannot publish my file. But thanks for the input. – SomeDude Jul 12 '16 at 19:52
  • 1
    WorkbookFactory should handle encrypted files too - [the optional second argument taken is the password to use for decryption](http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html#create(java.io.File,%20java.lang.String)) – Gagravarr Jul 12 '16 at 22:34