Export Grid. View To Word Excel PDF CSV Formats in ASP. Net. In this article, I will explain how to export Grid. View to Word, Excel, PDF and CSV formats.
Export HTML Table to PDF in ASP.NET In this article I’m going to explain how to export HTML table to PDF in ASP.NET. There are many ways we can export HTML Table to. Forum thread about Export To PDF to fit a page in UI for ASP.NET AJAX. Join the conversation now. In this article, I will explain how to export GridView to Word, Excel, PDF and CSV formats. Exporting to Word, Excel and CSV can be easily achieved using ASP.Net. Explains how you can export crystal reports to PDF, Excel, Word and HTML formats with ASP.NET application.
Exporting to Word, Excel and CSV can be easily achieved using ASP. Net without any third party tools, but for exporting Grid. View to PDF I am using i. Text. Sharp which is a free library for exporting html to PDF. To start with I have a Grid. View in which I am showing Customers records from the North.
Wind Database. The HTML markup of the Grid. View is as shown below< asp: Grid. View. ID="Grid. View. Auto. Generate. Columns="false"Font- Names="Arial" Font- Size="1.
Export to PDF file in ASP.Net – Gridview to PDF, ASPX Page Content to PDF Creating data driven application is one of the most commonly done task in our day today.
Alternating. Row. Style- Back. Color="#C2.
D6. 9B" Header. Style- Back. Color="green"Allow.
- How to export gridview data to PDF using asp.net or Export gridview data to PDF using asp.net.
- Here Mudassar Ahmed Khan has explained how to export ASP.Net Web Page with images to PDF using ITextsharp PDF conversion library.
- Is is possible to export directly to Excel or PDF from a code behind file without showing the reportviewer first and without the user having to click the export button.
- In this article we will learn how Export GridView to PDF Using Asp.net C# with the help itextsharp dll.
Paging="true" On. Page. Index. Changing="On. Paging"> < Columns> < asp: Bound. Field. Item. Style- Width="1. Data. Field="Customer.
ID" Header. Text="Customer. ID"/> < asp: Bound. Field. Item. Style- Width="1. Data. Field="City" Header. Text="City"/> < asp: Bound.
Field. Item. Style- Width="1. Data. Field="Country" Header. Text="Country"/> < asp: Bound. Field. Item. Style- Width="1.
Data. Field="Postal. Code" Header. Text="Postal. Code"/> < /Columns> < /asp: Grid. View> In the figure below the Grid. View is shown with four buttons. Export To Word. 2. Export To Excel.
Export To PDF4. Export To CSVExport to Microsoft Word Format. C#protectedvoid btn. Export. Word_Click(object sender, Event.
Args e){ Response. Clear(); Response. Buffer = true; Response. Add. Header("content- disposition", "attachment; filename=Grid. View. Export. doc"); Response. Charset = ""; Response.
Content. Type = "application/vnd. String. Writer sw= new. String. Writer(); Html. Text. Writer hw = new. Html. Text. Writer(sw); Grid. View. 1. Allow. Paging = false; Grid.
View. 1. Data. Bind(); Grid. View. 1. Render. Control(hw); Response. Output. Write(sw. To. String()); Response. Flush(); Response. End(); }VB. Net. Protected. Sub btn. Export. Word_Click(By.
Val sender As. Object, By. Val e As Event. Args) Response. Clear() Response. Buffer = True Response. Add. Header("content- disposition", "attachment; filename=Grid. View. Export. doc") Response.
Charset = "" Response. Content. Type = "application/vnd. Dim sw As. New String.
Writer() Dim hw As. New Html. Text. Writer(sw) Grid. View. 1. Allow. Paging = False Grid. View. 1. Data. Bind() Grid. View. 1. Render. Control(hw) Response. Output. Write(sw. To. String()) Response.
Flush() Response. End() End. Sub.
The above function renders the Grid. View contents as Microsoft Word format. You will notice I have disabled paging before exporting, so that all the pages are exported. The Output Exported File. Export to Microsoft Excel Format. For exporting the document to Excel if you do it directly as done in case of word the row background color is applied throughout to all the columns in the Excel Sheet hence in order to avoid it.
I have done a workaround below. First I am changing the background color of each row back to white.
Then I am applying the background color to each individual cell rather than the whole row. Thus when you export now you will notice that the formatting is applied only to the Grid. View cells and not all. Also I am applying textmode style class to all cells and then adding the style CSS class to the Grid. View before rendering it, this ensures that all the contents of Grid.
View are rendered as text. Export. Excel_Click(object sender, Event. Args e){Response. Clear(); Response. Buffer = true; Response. Add. Header("content- disposition", "attachment; filename=Grid. View. Export. xls"); Response.
Charset = ""; Response. Content. Type = "application/vnd. String. Writer sw = new. String. Writer(); Html. Text. Writer hw = new. Html. Text. Writer(sw); Grid.
View. 1. Allow. Paging = false; Grid. View. 1. Data. Bind(); //Change the Header Row back to white color. Grid. View. 1. Header. Row. Style. Add("background- color", "#FFFFFF"); //Apply style to Individual Cells.
Grid. View. 1. Header. Row. Cells[0]. Style. Add("background- color", "green"); Grid.
View. 1. Header. Row. Cells[1]. Style. Add("background- color", "green"); Grid. View. 1. Header. Row. Cells[2]. Style. Add("background- color", "green"); Grid.
View. 1. Header. Row. Cells[3]. Style. Add("background- color", "green"); for (int i = 0; i < Grid. View. 1. Rows. Count; i++ ){ Grid.
View. Row row = Grid. View. 1. Rows[i]; //Change Color back to white row. Back. Color = System. Drawing. Color. White; //Apply text style to each Row row.
Attributes. Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { row. Cells[0]. Style. Add("background- color", "#C2. D6. 9B"); row. Cells[1]. Style. Add("background- color", "#C2. D6. 9B"); row. Cells[2]. Style. Add("background- color", "#C2. D6. 9B"); row.
Cells[3]. Style. Add("background- color", "#C2. D6. 9B"); }}Grid.
View. 1. Render. Control(hw); //style to format numbers to stringstring style = @"< style> . Response. Write(style); Response. Output. Write(sw. To. String()); Response. Flush(); Response. End(); }VB. Net. Protected.
Sub btn. Export. Excel_Click(By. Val sender As. Object, By. Val e As Event. Args) Response. Clear() Response. Buffer = True Response.
Add. Header("content- disposition", "attachment; filename=Grid. View. Export. xls") Response. Charset = "" Response. Content. Type = "application/vnd.
Dim sw As. New String. Writer() Dim hw As. New Html. Text. Writer(sw) Grid. View. 1. Allow. Paging = False Grid. View. 1. Data. Bind() 'Change the Header Row back to white color Grid. View. 1. Header. Row. Style. Add("background- color", "#FFFFFF") 'Apply style to Individual Cells Grid.
View. 1. Header. Row. Cells(0). Style. Add("background- color", "green") Grid. View. 1. Header. Row. Cells(1). Style. Add("background- color", "green") Grid. View. 1. Header. Row. Cells(2). Style. Add("background- color", "green") Grid.
View. 1. Header. Row. Cells(3). Style. Add("background- color", "green") For i As. Integer = 0 To Grid.
View. 1. Rows. Count - 1 Dim row As Grid. View. Row = Grid. View. 1. Rows(i) 'Change Color back to white row. Back. Color = System. Drawing. Color. White 'Apply text style to each Row row.
Attributes. Add("class", "textmode") 'Apply style to Individual Cells of Alternating Row If i Mod 2 < > 0 Then row. Cells(0). Style. Add("background- color", "#C2. D6. 9B") row. Cells(1). Style. Add("background- color", "#C2. D6. 9B") row. Cells(2). Style. Add("background- color", "#C2.
D6. 9B") row. Cells(3). Style. Add("background- color", "#C2.
D6. 9B") End. If Next Grid. View. 1. Render. Control(hw) 'style to format numbers to string Dim style As. String = "< style>. Response. Write(style) Response. Output. Write(sw. To. String()) Response. Flush() Response.
End()End. Sub. The Output Exported File. Export to Portable Document Format.
For exporting the Grid. View to PDF I am using the i. Text. Sharp Library. You will need to Add Reference for the i.
Text. Sharp Library in your Website. Then import the following Namespaces.
C#using i. Text. Sharp. Text. Sharp. text. Text. Sharp. text. Text. Sharp. text. VB. Net. Imports i. Text. Sharp. text. Imports i. Text. Sharp.
Imports i. Text. Sharp. Imports i. Text. Sharp. By default the i. Text. Sharp Library does not support background color of table cells or table rows. Hence when you render it as PDF your Grid. View is rendered without any formatting.
Recently I read an article on hamang. Text. Sharp so that it exports the HTML with background color. For this tutorial, I have already modified the i. Text. Sharp Library DLL so that the Grid. View is rendered with all the background color used. You can refer the code for exporting Grid. View to PDF below.
C#protectedvoid btn. Export. PDF_Click(object sender, Event. Args e){ Response. Content. Type = "application/pdf"; Response. Add. Header("content- disposition", "attachment; filename=Grid. View. Export. pdf"); Response. Cache. Set. Cacheability(Http.
Cacheability. No. Cache); String. Writer sw = new. String. Writer(); Html. Text. Writer hw = new. Html. Text. Writer(sw); Grid.
View. 1. Allow. Paging = false; Grid. View. 1. Data. Bind(); Grid. View. 1. Render. Control(hw); String. Reader sr = new. String. Reader(sw. To. String()); Document pdf. Doc = new. Document(Page. Size. A4, 1. 0f,1.
HTMLWorker htmlparser = new. HTMLWorker(pdf. Doc); Pdf. Writer. Get. Instance(pdf.
Doc, Response. Output. Stream); pdf. Doc. Open(); htmlparser. Parse(sr); pdf. Doc. Close(); Response.
Write(pdf. Doc); Response. End(); }VB. Net. Protected. Sub btn. Export. PDF_Click(By. Val sender As. Object,By.
Val e As Event. Args) Response. Content. Type = "application/pdf" Response. Add. Header("content- disposition", "attachment; filename=Grid. View. Export. pdf") Response. Cache. Set. Cacheability(Http.
Cacheability. No. Cache) Dim sw As. New String. Writer() Dim hw As. New Html. Text. Writer(sw) Grid. View. 1. Allow. Paging = False Grid.
View. 1. Data. Bind() Grid. View. 1. Render. Control(hw) Dim sr As. New String. Reader(sw. To. String()) Dim pdf. Doc As. New Document(Page.
Size. A4, 1. 0. 0. F, 1. 0. 0. F, 1. F, 0. 0. F) Dim htmlparser As. New HTMLWorker(pdf. Doc) Pdf. Writer. Get. Instance(pdf. Doc, Response. Output.
Stream) pdf. Doc. Open() htmlparser. Parse(sr) pdf. Doc. Close() Response.
Write(pdf. Doc) Response. End()End. Sub. The Output Exported File. Export to Text/CSVFinally comes exporting Grid. View to CSV or Text File delimited by a separator like comma.
To export the Grid. View as CSV, I am running a two for loops. While looping through the Grid.
View columns and appending comma after each column and while looping through rows appending new line character. Refer the code below. C#protectedvoid btn. Export. CSV_Click(object sender, Event. Args e){ Response.
Clear(); Response.