Displaying chart in the browser through Java


#1

A J2EE web application cannot display a stream in the Excel in the browser?

You have said "Please use OpenInBrowser or OpenInExcel option to send the file to client. J2EE browser may doesn't support to display the Excel file but they can download it."

In my web service, I am using the following code:

System.IO.MemoryStream objStream = new System.IO.MemoryStream();

objExcelChart.Save(objStream, FileFormatType.Excel2003);

Then I convert the objStream to a byte array in .NET and send the byte array back to the J2EE client. (whether I serialize and deserialize it does not matter).

I am able to receive the byte array correctly in the J2EE web application from the Web service. I can save it to a file and then open it from disk. The chart can be viewed just fine. But if I try to display the bytearray in a browser in Excel, it displays garbage.

Do I need to set something other than the Response.ContentType to display the excel in a browser in Java?


#2

My code to send the binary data to browser:

response.ContentType = "application/vnd.ms-excel";

if(saveType == SaveType.OpenInExcel)
response.AddHeader( "content-disposition","attachment; filename=" + resultSpreadsheet);
else
response.AddHeader( "content-disposition","inline; filename=" + resultSpreadsheet);


#3

My .Net web service saves the Excel as a stream and sends it to the J2EE client. How can I save it as an Excel and send it to the client? From a web service, I can only send it as a bytearray to the client. There is no overload for the Save method that lets me save as a stream with a SaveType of OpenInExcel.


#4

In your .Net web service, could you call the code similiar to our demos?

excel.Save("book1.xls", SaveType.OpenInBrowser, FileFormatType.Default, Response);


#5

Yes, I can. But don’t I need to save it as a stream and then convert it to a bytearray before sending it to the client? Or is there any other way that I can send it to the client application?


#6

But don't I need to save it as a stream and then convert it to a bytearray before sending it to the client?

No. You don't need.

Or is there any other way that I can send it to the client application?

Yes. You can save the data to stream and send it to client with my above sample code.

excel.Save(stream, FileFormatType.Default);

byte[] data1 = stream.ToArray();

this.Response.ContentType = "application/xls";
Response.AddHeader( "content-disposition","inline; filename=book1.xls");
Response.BinaryWrite(data1);
Response.End();