On Wed, Jan 19, 2011 at 5:07 PM, fachhoch <fachh...@gmail.com> wrote:
> > I found some code to export datatable to excel > > here is the code > > > add(new Link<Void>("export"){ > @Override > public void onClick() { > > getRequestCycle().setRequestTarget(new > ComponentRequestTarget(abstractDataTablePanel.getDataTable()){ > @Override > public void > respond(RequestCycle requestCycle) { > DataTable > dataTable=(DataTable)getComponent(); > DataGridView > dataGridView = (DataGridView) > dataTable.get("body:rows"); > final int > oldRowsPerPage = dataGridView.getRowsPerPage(); > > dataGridView.setRowsPerPage (Integer.MAX_VALUE); > > super.respond(requestCycle); > > dataGridView.setRowsPerPage(oldRowsPerPage); > } > }); > WebResponse wr = (WebResponse) > getResponse(); > wr.setContentType("application/vnd.ms-excel; > charset=UTF-8"); > wr.setCharacterEncoding("UTF-8"); > wr.setHeader("content-disposition", > "attachment;filename=excel.xls"); > } > }); > > > > on clicking the link export works. After the file is downloaded If I > click on any pagination link for next page I get this error > > > WicketMessage: org.apache.wicket.WicketRuntimeException: component > > dataTableContainer:form:dataTable:dataTable:topToolbars:1:toolbar:span:navigator:navigation:5:pageLink > not found > > > Please suggest me . > This is a horrible hack of a way to get this to work. You're exporting HTML as Excel, potentially with additional HTML around it, etc.... I suspect that the error comes because you changed the server-side state of the data view, and there are now no pagination links - because everything is on a single page. Although you set the rows per page back to the old value, doing it after the respond probably means that your change was lost. To do export to Excel correctly, I'd suggest creating a link that does it and uses the data provider directly. It can use the data provider to get an iterator for all rows in the data set and iterate through them. It might be able to use the column components (labels) to render the cell content, although I personally wouldn't try that. I'd have an interface that extends IColumn that has a method for rendering to Excel. By doing this, you can iterate through your rows and your columns (IColumn) and render directly to Excel. Use one of the java/excel libraries to create the file. -- Jeremy Thomerson http://wickettraining.com *Need a CMS for Wicket? Use Brix! http://brixcms.org*