The following code doesn't work: POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream ( "c:\\workbook1.xls")); wb = new HSSFWorkbook(fs); FileOutputStream fileOut = new FileOutputStream("c:\\workbook2.xls"); byte[] bytes = wb.getBytes(); fileOut.write(bytes); fileOut.close(); Whereas this does: POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream ( "c:\\workbook1.xls")); wb = new HSSFWorkbook(fs); FileOutputStream fileOut = new FileOutputStream("c:\\workbook2.xls"); wb.write(fileOut); fileOut.close(); My input file just contains a simple text "Hi" in the 1st cell. I have just replaced the line - wb.write(fileOut); with the lines - byte[] bytes = wb.getBytes(); fileOut.write(bytes); I think there is some problem with the wb.getBytes() method.
The write method works because it does a couple more things than just getBytes and write them to the stream: public void write(OutputStream stream) throws IOException { byte[] bytes = getBytes(); POIFSFileSystem fs = new POIFSFileSystem(); fs.createDocument(new ByteArrayInputStream(bytes), "Workbook"); if (preserveNodes) { List excepts = new ArrayList(1); excepts.add("Workbook"); copyNodes(this.poifs,fs,excepts); } fs.writeFilesystem(stream); //poifs.writeFilesystem(stream); }