I think you should also upgrade your Apache POI version and switch to .xlsx
since the older format has a limit of about 65K records. Outside of that I
suggest you use an IResourceStream instead of writing to your web response.

Personally I use csv comma delimited since I don't do any magic beside
export those records to be viewed in Excel, edited and then imported back
via the webapp.

In any event this is how I do it via Wicket 6.x:

@Override
protected void onClick() {
    IResourceStream csvStream = new
CsvResourceStream(service.exportAll(getSession().getLocale()));
    String fileNameKey = ...;
    getRequestCycle().scheduleRequestHandlerAfterCurrent(
        new
ResourceStreamRequestHandler(csvStream).setFileName(ResourceKeyHelper.resour
ceValue(fileNameKey) + ".csv")
    );
}

public class CsvResourceStream extends AbstractResourceStream {
    private static final long serialVersionUID = 1L;
    
    public CsvResourceStream(ByteArrayOutputStream outputStream) {
        super(outputStream.toByteArray());
        IOUtils.closeQuietly(outputStream);
    }
    
    @Override
    public String getContentType() {
        return new StringBuilder()
            .append("text/comma-separated-values, ")
            .append("text/csv, ")
            .append("application/csv, ")
            .append("application/excel")
            .toString();
    }
}

You can switch it to your HSSFWorkbook and grab its byte content.

-----Original Message-----
From: BrianWilliams [mailto:brianwilliams33...@yahoo.com] 
Sent: Wednesday, July 31, 2013 1:14 PM
To: users@wicket.apache.org
Subject: Generating spreadsheet to send to Client fails in 1.6

Hello,

I had code in Wicket 1.x that worked perfectly to dynamically generate a
spreadsheet and send it to the user.  I had to rewrite it for the new
Response classes in 1.6:

    public void generateExcel(String filename, List<List&lt;? extends
Object>> dataList) {
        HSSFWorkbook myWorkBook = new HSSFWorkbook();
        HSSFSheet mySheet = myWorkBook.createSheet();
        ... (generate sheet from dataList using HSSFSheet methods)
        WebResponse response = (WebResponse)getResponse();
        webResponse.setContentType("application/vnd.ms-excel");
        webResponse.setAttachmentHeader(filename + ".xls");
        OutputStream out = webResponse.getOutputStream();
        myWorkBook.write(out);
        out.close();
    }

The sheet is still being populated as I need, and the client is asked to
open or save the <filename>.xls file (as expected).

The problem with 1.6 is: the current page is sent rather than the sheet I've
built.   My .xls viewer complains that the file is not in the correct format
and, when I display it anyway, contains the original Wicket page, head, css,
javascript and all.

The same problems happens when generating xml from the same data list:

    WebResponse response = (WebResponse)getResponse();
    response.setContentType("application/xml");
    response.setAttachmentHeader(filename + ".xml");
    XMLStreamWriter out =
XMLOutputFactory.newInstance().createXMLStreamWriter(response.getOutputStrea
m());

    out.writeStartDocument();
    ... (generate XML document from dataList)
    out.writeEndDocument();
    out.close();

This also sends the original HTML page to the client. 

Both methods worked perfectly before I migrated to Wicket 1.6, but now the
Response (returned by getResponse()) seems to behave differently.  Any
ideas?  Thank you very much.



--
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/Generating-spreadsheet-to-send-to
-Client-fails-in-1-6-tp4660579.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to