Newly released!! DataGrid component for REACT: http://reactdatagrid.com
Show/Hide Code
Flexicious Flex DataGrid Print and Export (Excel/Word/Html/Text):



SUMMARY:

Flexicious supports the following features out of the box:

  • Flex DataGrid Printing
  • Extensible architecture to customize the look and feel of the Print including, Report Header, Report Footer, Page Header and Page Footer.
  • Flex DataGrid Excel Export, Word Export, Html Export, and Text Export
  • Extensible architecture to Plugin your own Export formats, as well as customize the existing Export formats.
  • Integration with Alive PDF for PDF generation.
Customization of Print and Export behavior:
The PrintOptions class provides you with the ability to:
  • Customize the header, footer, page header and page footer. These are all renderer factories, you can give it a any component that implements IPrintArea
  • Show or hide any of the above sections if not needed
  • Provide a customized popup for the print options via the printOptionsRenderer property
  • Provide a customized popup for the preview window. In this example, we have added Alive PDF integration to the preview window.
  • A dictionary to specify style properties at the page level, like background color, padding, etc
The ExportOptions class provides you with the ability to
  • Provide a customized popup for the initial Export options page.
  • Insert a custom Exporter to the list of built in exporters (Excel, Word, Html, Text). We demonstrate a custom Word Export.
In this example, we are customizing the Print and Export options by extending them in our own classes. In MyPrintOptions, we are overriding the default style properties of the overall print window, as well as providing custom implementations for the Report Header, Footer, Page Header and Page Footer. This gives us the opportunity to hook into the Grids' Print Rendering mechanism to provide our own look and feel to the generated print out.

In MyExportOptions, we are adding our own version of the DocExporter to the list of available exporters. This exporter, MyDocExporter, basically extends the base DocExporter, and adds some custom formatting to the generated HTML. Since MS Word is capable of reading HTML files, it is able to consume the plain HTML generated by our DocExporter and MyDocExporter with ease, displaying the grid in MS Word. Similar formatting can be applied to other Export Formats, including Excel, Html and Text.

Please note: Since Flex runs inside the browsers security sandbox, there is no way for it to persist a file on disk (except Flash Player 10). So the Export functionality relies on the server's file download mechanism. The server could be anything, IIS, or any J2EE based server, with the capability to buffer a file response. The code to do this in .Net follows (Java and PHP would do the same thing). You may use the Flexicious Echo URL, http://www.flexicious.com/Home/Echo (default), but it is recommended you implement this inside your own firewall, to speed up response times.

           
            Response.AddHeader("content-disposition", string.Format("attachment; filename=Download.{0}",Request["extension"]));
            Response.ContentType = Request["contentType"];
            Response.Write(Request["body"]);
            Response.End();

Print/Export with filterPageSortMode='server', where user requestes 'All Pages' or 'Selected Pages':
Print/Export with filterPageSortMode='server':
(Please refer to example #4, this demonstrates how to handle the special scenario described below.)
Under most circumstances, there is not much difference between server and client based filterServerPageModes. The difference lays in the inherent fact that in filterPageSortMode='server', the grid on the client may not have all the data required to print. Since the printing as well as export is encapsulated by the grid, it needs to get the data to perform the print or export operation from the server. This is only required when the user chooses to print "All Pages" or "Selected Pages" of data, since the grid only loads the current page of data into client memory. If the user chooses "Current Page" or "Selected Records", no server round trip is required because all the data required to perform the print or export operation is already loaded on the client. To handle the "All Pages" and "Selected Pages" scenario, the grid exposes an event, printExportDataRequested, which can be used like below:

            private function onPrintExportDataRequest(event:PrintExportDataRequestEvent):void
            {
                //here we build our custom filter object and send it to the server
                remoteObject.getEmployees(new MyPrintExportFilter(event.filter as PrintExportFilter));
            }


In the sample implementation, we have created a special Filter, MyPrintExportFilter, that simply extends the MyFilter, and add an object of type MyPrintExportOptions to it. In here, we store if the user chose to print/export "All Pages" of data, or if they chose to print a range of pages. In the server implementation, the method to handle the filterPageSortChange as well as the printExportDataRequest events could be the same, with the slight caveat that that the in case of the printExportDataRequest, the server may need to handle the data passed via the MyPrintExportOptions class, to either return all pages of data, or specific pages. Once the data is sent back, all your code needs to do is set the printExportData property of the grid to the returned collection, and the grid's print/export mechanism continues as usual.

Integration with AlivePDF for PDF generation:
In our customized version of the print preview popup, we have added a button to export the printout to pdf. This is a simple operation, where we set printOptions.printToPdf to true,and call the print method. This causes the controller to send the printed page snapshots to an array instead of the printer. These images are then added to the pdf object from AlivePDF, and then pushed to the server for buffering. Please note, AlivePDF requires a certain interface for buffering which we provide. You may use the Flexicious PDF Echo URL, http://www.flexicious.com/Home/EchoPdf (default), but it is recommended you implement this inside your own firewall, to speed up response times.The code to do this in .Net follows (Java and PHP would do the same thing).

           
            string method = Request.QueryString["method"];
            string name = Request.QueryString["name"];

            byte[] data = Request.BinaryRead(Request.TotalBytes);

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Length", data.Length.ToString());
            Response.AddHeader("Content-disposition", method + "; filename=" + name);
            Response.BinaryWrite(data);
            Response.End();

Links to do this in Java:
http://alivepdf.bytearray.org/?p=8
Please note : The core library has no dependency on Alive PDF. We just demonstrate in the sample, how it may be integrated if you require PDF generation. We have found the AlivePDF library to be excellent to work with. It is distributed under the MIT license, and is packaged separately from the Flexicious Library.