ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Set PageSize while Exporting ASP.Net GridView to Word document using C# and VB.Net<p>Hi&nbsp;<a class="username" rel="democloud">democloud,</a></p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using Word = Microsoft.Office.Interop.Word;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports Word = Microsoft.Office.Interop.Word</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BindGrid(); Export_Data_To_Word(GridView1, @&#34;C:\Test.doc&#34;); } } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); GridView1.DataSource = dt; GridView1.DataBind(); } public void Export_Data_To_Word(GridView gridView, string filename) { if (gridView.Rows.Count != 0) { int RowCount = gridView.Rows.Count; int ColumnCount = gridView.Columns.Count; Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1]; //Adding Rows. int r = 0; for (int c = 0; c &lt;= ColumnCount - 1; c++) { for (r = 0; r &lt;= RowCount - 1; r++) { DataArray[r, c] = gridView.Rows[r].Cells[c].Text; } } Word.Document oDoc = new Word.Document(); oDoc.Application.Visible = true; //Page Orientation. oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; //Page PaperSize. oDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4; dynamic oRange = oDoc.Content.Application.Selection.Range; string oTemp = &#34;&#34;; for (r = 0; r &lt;= RowCount - 1; r++) { for (int c = 0; c &lt;= ColumnCount - 1; c++) { oTemp = oTemp + DataArray[r, c] + &#34;\t&#34;; } } //Table Format oRange.Text = oTemp; object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs; object ApplyBorders = true; object AutoFit = true; object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent; oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount, Type.Missing, Type.Missing, ref ApplyBorders, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing); oRange.Select(); oDoc.Application.Selection.Tables[1].Select(); oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0; oDoc.Application.Selection.Tables[1].Rows.Alignment = 0; oDoc.Application.Selection.Tables[1].Rows[1].Select(); oDoc.Application.Selection.InsertRowsAbove(1); oDoc.Application.Selection.Tables[1].Rows[1].Select(); //header row style oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1; oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = &#34;Tahoma&#34;; oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14; //Add header row manually. for (int c = 0; c &lt;= ColumnCount - 1; c++) { oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = gridView.Columns[c].HeaderText; } //header text foreach (Word.Section section in oDoc.Application.ActiveDocument.Sections) { Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage); headerRange.Text = &#34;your header text&#34;; headerRange.Font.Size = 16; headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; } //save the file oDoc.SaveAs(filename); oDoc.Application.Quit(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then BindGrid() Export_Data_To_Word(GridView1, &#34;C:\Test.doc&#34;) End If End Sub Private Sub BindGrid() Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;) dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;) dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;) GridView1.DataSource = dt GridView1.DataBind() End Sub Public Sub Export_Data_To_Word(ByVal gridView As GridView, ByVal filename As String) If gridView.Rows.Count &lt;&gt; 0 Then Dim RowCount As Integer = gridView.Rows.Count Dim ColumnCount As Integer = gridView.Columns.Count Dim DataArray As Object(,) = New Object(RowCount + 1 - 1, ColumnCount + 1 - 1) {} Dim r As Integer = 0 For c As Integer = 0 To ColumnCount - 1 For r = 0 To RowCount - 1 DataArray(r, c) = gridView.Rows(r).Cells(c).Text Next Next Dim oDoc As Word.Document = New Word.Document() oDoc.Application.Visible = True oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape oDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4 Dim oRange As Object = oDoc.Content.Application.Selection.Range Dim oTemp As String = &#34;&#34; For r = 0 To RowCount - 1 For c As Integer = 0 To ColumnCount - 1 oTemp = oTemp &amp; DataArray(r, c) &amp; vbTab Next Next oRange.Text = oTemp Dim Separator As Object = Word.WdTableFieldSeparator.wdSeparateByTabs Dim ApplyBorders As Object = True Dim AutoFit As Object = True Dim AutoFitBehavior As Object = Word.WdAutoFitBehavior.wdAutoFitContent oRange.ConvertToTable(Separator, RowCount, ColumnCount, Type.Missing, Type.Missing, ApplyBorders, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, AutoFit, AutoFitBehavior, Type.Missing) oRange.[Select]() oDoc.Application.Selection.Tables(1).[Select]() oDoc.Application.Selection.Tables(1).Rows.AllowBreakAcrossPages = 0 oDoc.Application.Selection.Tables(1).Rows.Alignment = 0 oDoc.Application.Selection.Tables(1).Rows(1).[Select]() oDoc.Application.Selection.InsertRowsAbove(1) oDoc.Application.Selection.Tables(1).Rows(1).[Select]() oDoc.Application.Selection.Tables(1).Rows(1).Range.Bold = 1 oDoc.Application.Selection.Tables(1).Rows(1).Range.Font.Name = &#34;Tahoma&#34; oDoc.Application.Selection.Tables(1).Rows(1).Range.Font.Size = 14 For c As Integer = 0 To ColumnCount - 1 oDoc.Application.Selection.Tables(1).Cell(1, c + 1).Range.Text = gridView.Columns(c).HeaderText Next For Each section As Word.Section In oDoc.Application.ActiveDocument.Sections Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage) headerRange.Text = &#34;your header text&#34; headerRange.Font.Size = 16 headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter Next oDoc.SaveAs(filename) oDoc.Application.Quit() End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/owVUetf.jpg" alt="" width="471" height="332" /></span></strong></p>https://www.aspforums.net:443/Threads/615773/Set-PageSize-while-Exporting-ASPNet-GridView-to-Word-document-using-C-and-VBNet/https://www.aspforums.net:443/Threads/615773/Set-PageSize-while-Exporting-ASPNet-GridView-to-Word-document-using-C-and-VBNet/Mon, 11 Mar 2019 08:06:55 GMT