You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Abe Mishler <ab...@mishlerlabs.com> on 2009/07/24 23:58:09 UTC

Re: [SOLVED] Malformed xls with struts2 and POI 3.5-beta6

Hi all,

I have solved the issue. Since I have aired my problems to the 
community, I will provide "the solution" in case someone in the future 
comes across this in the archives. And "Thank you" to David Fisher for 
spending valuable time on my problem.

So digging deeper into the docs I discovered that 
HSSFWorkbook.getBytes() does the following:

"get the bytes of just the HSSF portions of the XLS file".

I wanted the bytes for the WHOLE file, which I mused, is why the written 
output was incorrect as previously posted.

I altered my execute() method as follows and it works like a champ.

public String execute() throws Exception {
	Workbook workbook = new HSSFWorkbook();
		
	Sheet s = workbook.createSheet("sheet");
	Row r = s.createRow(0);
	Cell c = r.createCell(0);
	c.setCellValue("hello world");
		
	ByteArrayOutputStream baos =
		new ByteArrayOutputStream();
	workbook.write(baos);
	baos.close();
		
	ByteArrayInputStream bais =
		new ByteArrayInputStream(baos.toByteArray());
	inputStream = bais;
	return SUCCESS;
}

Now my inputStream has ALL the bytes of the workbook, and not SOME of 
the bytes.

SUGGESTION: As I am not a contributing developer to POI I am unaware of 
the ramifications or possibility of this suggestion. Please take it with 
a grain of salt. If I had my "druthers" I would be able to call a method 
on the Workbook class that would return bytes[] for the whole file.

E.g.

InputStream inputStream = null;
Workbook wb = new HSSFWorkbook();

/**
  * hydrate wb with data
  */

// now I have all the bytes I need for whole file
ByteArrayInputStream bais = new ByteArrayInputStream(wb.getBytes());

// and can easily put them into an InputStream for reading elsewhere
inputStream = bais;


But then again, maybe I sound like Bill Murray in "What About Bob?":

[I'm baby stepping. I'm doing the work. I'm not a slacker! Give me! Give 
me! I need! I need! Give me! Give me!]

Because this is already a fabulous library.

Thanks again,
Abe

Abe Mishler wrote:
> Hi all,
> 
> I need some pointers/help with creating an .xls file for download from a 
> servlet container.
> 
> Specifically, I have a struts2 project running inside Tomcat 5.5. And 
> I'm using POI 3.5-beta6.
> 
> I am able to use POI to create a workbook and fill it with data. The 
> problem is after I download the file, the binary header doesn't match 
> the expected: D0 CF 11 E0 A1 B1 1A E1 signature.
> 
> Instead, I get: 09 08 10 00 00 06 05 00 D3 10 CC 07 41 ...
> 
> I'm definitely getting a binary file. When I open the file using MS 
> Excel 2003 SP2, it goes into file recovery mode telling me the file is 
> damaged beyond repair, etc. After I click "Don't Send" a couple times on 
> the error dialogs, my data is there, although without the formatting 
> (Date values look like numbers).
> 
> I have one immediate concern: All of the examples use wb.write(fileOut), 
> however, AFAIK, that's not the way for writing data out of a servlet 
> container for download. I'm familiar with creating an InputStream for a 
> file download.
> 
> I've included a basic execute() method from my action which demonstrates 
> how I export the data to the user for download. Is this causing the 
> malformed file? How do I get the workbook bytes[] into an InputStream 
> properly?
> 
> Thanks in advance,
> Abe
> 
> ==
> 
> I have an action class with an execute() method as follows:
> 
> public String execute() throws Exception {
>     Workbook workbook = new HSSFWorkbook();
>     Sheet s = workbook.createSheet("sheet");
>     Row r = s.createRow(0);
>     Cell c = r.createCell(0);
>     c.setCellValue("hello world");
>     ByteArrayInputStream bais =
>         new ByteArrayInputStream(
>         ((HSSFWorkbook) workbook).getBytes());
>     inputStream = bais;
>     return SUCCESS;
> }
> 
> and the necessary getter for inputStream.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org