Next thing I saw is that you should remove "sos.close();" - you don't want to close your JSP's output stream.

BTW - since Excel is a binary format and JSP is text output, you really should do it as a servlet.

Here is a quick cut and paste of one that we use:

public class ExcelServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Expires","Thu, 01 Dec 1994 16:00:00 GMT");
response.setDateHeader("Last-Modified", System.currentTimeMillis() );

        try {
response.setHeader("Content-disposition", "attachment;filename=" + file);
            response.setContentType("application/vnd.ms-excel");

List headers =(List)request.getAttribute("headers");
List records =(List)request.getAttribute("records");

HSSFWorkbook wb =  new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();

HSSFFont headerFont = wb.createFont();
headerFont.setFontName(HSSFFont.FONT_ARIAL);
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);


HSSFCellStyle HeaderStyle = wb.createCellStyle();
HeaderStyle.setFont(headerFont);

int rowNum = 0;

if (headers != null) {
        
        HSSFRow headerRow = sheet.createRow(rowNum++);
        Object[] arr = headers.toArray();
        
        for (int i=1; i<arr.length; i++) {
                
                String s = (String)arr[i];
                HSSFCell cell = headerRow.createCell((short)i);
                cell.setCellValue(s);
        }
}

if (records != null) {
        
        Object[] arr = records.toArray();
        
        for (int i=0; i<arr.length; i++) {

                String record = (String)arr[i];
                HSSFRow recordRow = sheet.createRow(rowNum++);
                
                StringTokenizer st = new StringTokenizer(record,"|");
                boolean skip = true;
                short cellNum = 0;
                
                while (st.hasMoreTokens()) {
                        
                        String tok=st.nextToken();
                        if (skip) {
                                skip = false;
                                continue;
                        }
        
                        if(tok.equals("&nbsp;"))
                                tok="";
                        
                        HSSFCell cell = recordRow.createCell(cellNum++);
                        cell.setCellValue(tok);
                }
        }
}

OutputStream sos = response.getOutputStream();
wb.write(sos);
        }
    }

}


On May 28, 2008, at 10:46 AM, Dean Schulze wrote:




David Fisher <dfisher <at> jmlafferty.com> writes:


Dean,

Excel is a very exact and binary format.

You must put out no whitespace from your JSP (I recommend using a
servlet, but..)

Change the top part to this:

<%@ page language="java"
%><%@ page
import
="java.util.*,java.io.OutputStream,org.apache.poi.hssf.usermodel.*"
%><%

And make sure you don't have a trailing blank after the final %>

Regards,
Dave


David,

Thanks for your response, but after doing what you recommend I still get no output. View Page Source shows nothing, but when I look at it with Firefox Web
tools it shows

<html>
<head/>
<body>
<pre/>
</body>
</html>


The HSSF docs don't mention using HSSF in a JSP so maybe HSSF is incompatible
with JSPs.

I'm going to take a look at using the Excel XML format and see if that works.

CSV works, but it produces unformatted output.





---------------------------------------------------------------------
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]

Reply via email to