• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to read an ArrayList and write to an XLS file using POIFSFileSystem  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to read from a arraylist and write to a excel. I
reading some records from the DB and trying to write to and excel
file I get -----

java.io.IOException: Unable to read entire header; 88 bytes read;
expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>
(HeaderBlockReader.java:78)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>
(POIFSFileSystem.java:83)

My code ----

ResultSet rs2 = stmt2.executeQuery(repSQLStatement);

String filePath = "C:\\test1.xls";
ArrayList al = new ArrayList();
int cols = rs2.getMetaData().getColumnCount();

while (rs2.next()) {
ArrayList record = new ArrayList();
for (int i= 1; i <= cols; i++) {
Object value = rs2.getObject(i);
record.add(value);
}
al.add(record);
}

StringBufferInputStream inputStream = new
StringBufferInputStream(String.valueOf(al));
OutputStream outputStream = new FileOutputStream(filePath);

if (inputStream != null) {
//Here it throws java.io.IOException: Unable to read entire header
POIFSFileSystem poiFS = new POIFSFileSystem(inputStream);
poiFS.writeFilesystem(outputStream);
}
 
Ajay Reddy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way I am using poi-bin-2.5.1-final-20040804.zip version of POI.

Thanks for your help in advance.
AJ.
 
Ajay Reddy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should read my documentation properly. The POIFileSystem class I believe is used to write files from one type to another(xls to xls). If you like to create a xls file by reading data from arraylist then HSSFWorkbook class must be used.

For further information on this class go to http://jakarta.apache.org/poi/hssf/quick-guide.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!