It goes something like this. I've left out a bunch of stuff to do with
setting column headings, widths, and styles.
public interface ReportColumn extends Serializable {
public Object getCellValue(IModel rowModel);
}
public class ExcelReportGenerator implements ReportGenerator {
public void generate(
OutputStream outputStream,
ReportColumn[] columns,
IDataProvider dataProvider) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
Iterator iter = dataProvider.iterator(0, dataProvider.size());
while (iter.hasNext()) {
Object o = iter.next();
IModel rowModel = dataProvider.model(o);
row = sheet.createRow(rownum++);
for (short i = 0; i < columns.length; i++) {
HSSFCell cell = row.createCell(columnNumber,
HSSFCell.CELL_TYPE_STRING);
Object value = column.getCellValue(rowModel);
cell.setCellValue(new HSSFRichTextString(value.toString()));
}
}
workbook.write(outputStream);
}
}
We then use a Link with code like this in its onClick method:
public void onClick() {
final ReportGenerator generator = getReportGenerator();
IResourceStream resourceStream =
new AbstractResourceStreamWriter() {
public void write(OutputStream output) {
try {
generator.generate(output, columns, dataProvider);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String getContentType() {
return generator.getContentType();
}
};
getRequestCycle().setRequestTarget(
new ResourceStreamRequestTarget(resourceStream)
.setFileName(getFileName()));
}
jk
On Wed, Apr 16, 2008 at 09:25:41PM -0400, Ricky wrote:
> Can someone post some code on this ? (only rough sketch ... nothing
> elaborate really.
>
> Thanks in advance.
> Rick
>
> On Wed, Apr 16, 2008 at 8:54 PM, John Krasnay <[EMAIL PROTECTED]> wrote:
>
> > Hi RG,
> >
> > You probably want to look at how DataTable in wicket-extensions works.
> > The idea is you set up an array of column objects, each of which knows
> > how to display the data in a particular column, and a data provider,
> > which provides an iterator over the objects represent your rows. The
> > DataTable puts these two together and renders an HTML table.
> >
> > You can tackle your PDF problem in a similar way. Create a different
> > column abstraction that writes to the iText API, then create a component
> > analagous to DataTable but that writes the PDF out to a stream.
> > Internally, this DataTable-equivalent would create the iText document,
> > iterate over the rows returned by the data provider, and for each row
> > iterate over the columns, asking each column to render its particular
> > cell.
> >
> > We use this approach for generating spreadsheets (using POI instead of
> > iText, of course) and it works like a charm.
> >
> > jk
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]