java - Cannot write HSSFWorkbook to ByteArray and then read it to HSSFWorkbook -


5i need convert hssfworkbook (apache's poi) bytearray , later convert bytearray hssfworkbook. following testcase illustrates problem:

   @test    public void testxlsexportimport(){       try {          inputstream = new fileinputstream(filepath);          hssfworkbook wb = new hssfworkbook(is);          byte[] exported = wb.getbytes();          hssfworkbook wb2 = new hssfworkbook(new bytearrayinputstream(exported));           //in line above exception thrown       } catch (exception e) {          asserttrue(false);       }    } 

the testcase fails exception: java.io.ioexception: invalid header signature; read 0x0005060000100809, expected 0xe11ab1a1e011cfd0

(i using apaches poi 3.5-beta3)

i hope can me... how can work?!

you're not writing workbook correctly! need use write(outputstream) call.

as shown in various examples on website, code should instead be:

     inputstream = new fileinputstream(filepath);      hssfworkbook wb = new hssfworkbook(is);      bytearrayoutputstream out = new bytearrayoutputstream();      wb.write(out);      hssfworkbook wb2 = new hssfworkbook(new bytearrayinputstream(out.tobytearray()));  

also, don't open workbook fileinputstream, if have file use directly. opening file uses less memory stream.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -