Telerik blogs

Integrate Telerik UI for WinForms and Document Processing Libraries with a full set of file formats supported by RadGridView and its export functionality.

The smooth integration between the Progress Telerik UI for WinForms suite and the Document Processing Libraries brings a full set of file formats that are supported by RadGridView and its export functionality.

RadGridView can be exported to XLSX (XLS*), CSV, PDF, TXT and HTML. Considering the number of records and how important the style settings are to the generated file, we can optimize the export time by running the export asynchronously. Let’s get familiar with all the options you have when you generate a file with the data stored in the grid control.

csv, xls, txt, pdf

Export with RadSpreadProcessing

GridViewSpreadExport utilizes our RadSpreadProcessing library to export the content of RadGridView to XLSX (XLS*), CSV, PDF and TXT formats.

The spread export functionality requires the TelerikExport.dll assembly. To access the types in TelerikExport, you must include the assembly in your project and reference the Telerik.WinControls.Export namespace.

GridViewSpreadExport spreadExporter = new GridViewSpreadExport(this.radGridView1);
SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
string fileName = @"..\..\exportedFile.xlsx";
spreadExporter.RunExport(fileName, exportRenderer);
Process.Start(fileName);

GridViewSpreadExport

*Image export is not available in the XLS format provider.

You will notice that the default settings for the export wouldn’t bring the same look and feel to the exported file. If you need to achieve as close as possible the style available in the grid inside the exported file, like columns’ width or font, you can enable the ExportVisualSettings property or handle the CellFormatting event and apply all the style changes you need.

cell check to preserve cell formatting in gridview export

More information about the public API that the GridViewSpreadExport offers is available in the Properties and Events sections in the online documentation.

Each column in RadGridView has an ExcelExportType property that you can use to explicitly set the data type of the cells in the exported document.

To change the format of the exported data: Set the ExcelExportType property of the specific column to Custom and specify the ExcelExportFormatString property with the desired format. (Read more.)

this.radGridView1.Columns["ProductID"].ExcelExportType = Telerik.WinControls.UI.Export.DisplayFormatType.Custom;
this.radGridView1.Columns["ProductID"].ExcelExportFormatString = "#.#";
this.radGridView1.Columns["Unitprice"].ExcelExportType = Telerik.WinControls.UI.Export.DisplayFormatType.Currency;

Product ID and UnitPrice columns have been emphasized with a red box. In the export, the period is removed from the prouct ID and the currency formatting is removed from unit price

The ExportFormat property defines the format the grid will be exported to. The available values are:

  • XLSX
  • PDF
  • CSV
  • TXT

The default value of the property is XLSX, hence, if not otherwise specified, the exporter will export to XLSX.

Export with RadSpreadStreamProcessing

When it comes to a scenario of exporting a grid with a lot of records, e.g., 100,000 rows, it may require some time to generate the export file with the GridViewSpreadExport.

After performing a quick test, it took around 50 seconds to export 100K records with the GridViewSpreadExport:

51540 milliseconds

Here comes the GridViewSpreadStreamExport that uses the RadSpreadStreamProcessing library which allows you to create big documents (without loading the entire document in the memory) and export them to the most common formats. Thus, you can optimize the export time and memory consumption.

GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.radGridView1);
spreadStreamExport.ExportVisualSettings = true;
string fileName = @"..\..\exportedFile.xlsx";
spreadStreamExport.RunExport(fileName, new SpreadStreamExportRenderer());

As a result, the same number of grid rows are exported much faster with the stream export in around 20 seconds:

21761 milliseconds

A full list of the public API and fine-tuning for the exported file can be found here.

Export with RadPdfProcessing

RadGridView’s data can be exported natively to the PDF format by GridViewPdfExport, which utilizes the powerful RadPdfProcessing library.

The GridViewPdfExport functionality is also located in the TelerikExport.dll assembly. You need to include the following namespace in order to access the types contained in TelerikExport: Telerik.WinControls.Export.

Telerik.WinControls.Export.GridViewPdfExport pdfExporter = new Telerik.WinControls.Export.GridViewPdfExport(this.radGridView1);
pdfExporter.FileExtension = "pdf";
pdfExporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport; ;
pdfExporter.ShowHeaderAndFooter = true;

pdfExporter.HeaderHeight = 30;
pdfExporter.HeaderFont = new Font("Arial", 22);
pdfExporter.Logo = System.Drawing.Image.FromFile(@"C:\MyLogo.png");
pdfExporter.LeftHeader = "[Logo]";
pdfExporter.LogoAlignment = ContentAlignment.MiddleLeft;
pdfExporter.LogoLayout = Telerik.WinControls.Export.LogoLayout.Fit;

pdfExporter.MiddleHeader = "Middle header";
pdfExporter.RightHeader = "Right header";
pdfExporter.ReverseHeaderOnEvenPages = true;

pdfExporter.FooterHeight = 30;
pdfExporter.FooterFont = new Font("Arial", 22);
pdfExporter.LeftFooter = "Left footer";
pdfExporter.MiddleFooter = "Middle footer";
pdfExporter.RightFooter = "Right footer";
pdfExporter.ReverseFooterOnEvenPages = true;

pdfExporter.FitToPageWidth = true;
string fileName = @"..\..\exportedFile.pdf";
pdfExporter.RunExport(fileName, new Telerik.WinControls.Export.PdfExportRenderer());

PDF file has logo, middle header and right header

Once you have the grid’s content exported to one of the above-mentioned file formats, with the help of the Document Processing Libraries it is possible to manage further the file and convert it to any other format supported by the DPL.

Export to HTML

If you need to export the grid’s content in HTML format, it is suitable to use the ExportToHTML class. It offers excellent export performance and creates an HTML formatted file, which can be opened in a browser or MS Word. However, the HTML export is pretty old and its API is very limited.

That is why we strive to utilize as much as possible the rich API of our Document Processing Libraries for exporting the RadGridView. It includes a set of cross-platform libraries that let you import and export content between different formats and work with archive files.

Try It for Free

Try out the GridView and 160 other controls from Telerik UI for WinForms today with our free trial:

Try Telerik UI for WinForms

Better yet, try Telerik UI for WinForms and Document Processing Libraries (and a bunch of other good stuff) with Telerik DevCraft—the most comprehensive UI component suite you can get your hands on:

Try Telerik DevCraft


About the Author

Desislava Yordanova

Desislava Yordanova is a proactive ambassador of diligent processes and a customer-caring professional. Currently, she is a Technical Support Engineer, Principal in the Document Processing team after a successful journey in the Telerik UI for WinForms team. She joined the company in 2013. Desislava holds a master’s degree in Computer Systems and Technologies from the Technical University in Sofia. Apart from her job, she is keen on snowboarding and travelling. You can find Desislava on LinkedIn.

Related Posts

Comments

Comments are disabled in preview mode.