Hi all,
I solved my problem in another reply marked with [SOLVED]. For clarity
I'm replying to this message additionally.
Thank you David for your reply. As you mentioned, yours is not a struts2
solution.
I wanted to make use of David's reply, not to his shame, but to point
out to those listening that I think David agrees with me in my [SOLVED]
reply that we need a Workbook getBytes() method. It is very natural to
assume that there is a getBytes() method on the Workbook interface
(org.apache.poi.ss.usermodel). At the time of this writing, there is
none (using http://poi.apache.org/apidocs/index.html for reference), so
the javacode part of David's solution wouldn't work.
Like I said, I'm not trying to publicly shame anyone here, but David's
example is case in point. I think there are two votes for a Workbook
getBytes().
Thanks,
Abe
David Fisher wrote:
I don't know struts2, but we download XLS all the time from Tomcat 5.5.
The important thing is not to do it through a JSP, or other Text based
method.
Instead use a servlet container:
In WEB-INF/web.xml:
<servlet>
<servlet-name>xlsoutput</servlet-name>
<servlet-class>com.output.generator</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>output</servlet-name>
<url-pattern>/your-url-pattern-for-xls</url-pattern>
</servlet-mapping>
In WEB-INF/classes/com/output/ compile a java class name generator.java
public class generator extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
byte[] bytes = null;
boolean is_inline = false;
String file = "hello.xls";
Workbook workbook = new HSSFWorkbook();
Sheet s = workbook.createSheet("sheet");
Row r = s.createRow(0);
Cell c = r.createCell(0);
c.setCellValue("hello world");
bytes = workbook.getBytes();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
(is_inline?"inline":"attachment")+";filename=" + file);
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
}
}
HTH,
Dave
On Jul 24, 2009, at 1:37 PM, 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: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]