Export GridView Using OpenXML Part 1

Hi guys. In this article I will show how to export a GridView table into a document using the Open XML.

Download the OPENXML using this location http://www.microsoft.com/en-in/download/details.aspx?id=30425.

After downloading the SDK install the .exe into your PC.

Now open a new project in your C# tool.

Add the OpenXML SDK reference to your project. Also include Windowsbase in the references.

Now use the following namespace in the code.

  1. using DocumentFormat.OpenXml.Wordprocessing;  
  2. using DocumentFormat.OpenXml.Packaging;  
  3. using DocumentFormat.OpenXml;  
  4. using System.IO.Packaging;  
After including the preceding namespace, design your table using the XML file. Mine looks as in the following.



Figure 1: Design Table

Add the data to the DataGridView.

Now create the document template using the following code:
  1. //count number of column available in Gridview    
  2. int columncount = dataGridView1.Columns.Count;  
  3. using(WordprocessingDocument wpc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document)) {  
  4.     // Add a new main document part.     
  5.     MainDocumentPart mainPart = wpc.AddMainDocumentPart();  
  6.     //Create DOM tree for simple document.     
  7.     mainPart.Document = new Document();  
  8.     // Save changes to the main document part.    
  9.     Body body = new Body();  
  10.     Table table = new Table();  
  11.     // Create a TableProperties object and specify its border information.    
  12.     TableProperties tblProp = new TableProperties(  
  13.     new TableBorders(  
  14.     new TopBorder() {  
  15.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  16.     },  
  17.     new BottomBorder() {  
  18.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  19.     },  
  20.     new LeftBorder() {  
  21.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  22.     },  
  23.     new RightBorder() {  
  24.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  25.     },  
  26.     new InsideHorizontalBorder() {  
  27.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  28.     },  
  29.     new InsideVerticalBorder() {  
  30.         Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20  
  31.     }));  
  32.     // Append the TableProperties object to the empty table.    
  33.     table.AppendChild < TableProperties > (tblProp);  
  34.     // Create a row.    
  35.     TableRow tr = new TableRow();  
  36.     //create Dynamic object by using array.    
  37.     TableCell[] tc1 = new TableCell[5];  
  38.     // this loop allow us to create multiple column according to the Gridview    
  39.     for (int i = 0; i < columncount; i++) {  
  40.         string[] columnhead = new string[columncount];  
  41.         columnhead[i] = dataGridView1.Columns[i].HeaderText.ToString();  
  42.         // Create a cell.    
  43.         tc1[i] = new TableCell();  
  44.         // Specify the width property of the table cell.    
  45.         tc1[i].Append(new TableCellProperties(  
  46.         new TableCellWidth() {  
  47.             Type = TableWidthUnitValues.Dxa, Width = "2400"  
  48.         }));  
  49.         // Specify the table cell content.    
  50.         tc1[i].Append(new Paragraph(new Run(new Text(columnhead[i]))));  
  51.         // Append the table cell to the table row.    
  52.         tr.Append(tc1[i]);  
  53.     }  
  54.     // Append the table row to the table.    
  55.     table.Append(tr);  
  56.     body.Append(table);  
  57.     mainPart.Document.Append(body);  
  58.     mainPart.Document.Save();

 Then open the document and pass the data in the GridView using the following code.

  1. int rowcount = dataGridView1.Rows.Count;  
  2. int columncount = dataGridView1.Columns.Count;   
  3. {  
  4.     using(WordprocessingDocument doc = WordprocessingDocument.Open(filename, true)) {  
  5.         // Create an empty table.    
  6.         Table table = new Table();  
  7.         // Create a TableProperties object and specify its border information.    
  8.         TableProperties tblProp = new TableProperties(  
  9.         new TableBorders(  
  10.         new TopBorder()   
  11.         {  
  12.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  13.         },  
  14.         new BottomBorder()   
  15.         {  
  16.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  17.         },  
  18.         new LeftBorder()   
  19.         {  
  20.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  21.         },  
  22.         new RightBorder()   
  23.         {  
  24.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  25.         },  
  26.         new InsideHorizontalBorder()   
  27.         {  
  28.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  29.         },  
  30.         new InsideVerticalBorder()   
  31.         {  
  32.             Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20  
  33.         }));  
  34.         // Append the TableProperties object to the empty table.    
  35.         table.AppendChild < TableProperties > (tblProp);  
  36.         for (int i = 0; i < rowcount; i++)   
  37.         {  
  38.             // Create a row.    
  39.             TableRow tr = new TableRow();  
  40.             // Create a cell.    
  41.             TableCell[] tc1 = new TableCell[columncount];  
  42.             for (int j = 0; j < columncount; j++)   
  43.             {  
  44.                 tc1[j] = new TableCell();  
  45.                 string data1 = dataGridView1.Rows[i].Cells[j].Value.ToString();  
  46.                 // Specify the width property of the table cell.    
  47.                 tc1[j].Append(new TableCellProperties(  
  48.                 new TableCellWidth()   
  49.                 {  
  50.                     Type = TableWidthUnitValues.Dxa, Width = "2400"  
  51.                 }));  
  52.                 // Specify the table cell content.    
  53.                 tc1[j].Append(new Paragraph(new Run(new Text(data1))));  
  54.                 tr.Append(tc1[j]);  
  55.             }  
  56.             // Append the table row to the table.    
  57.             table.Append(tr);  
  58.         }  
  59.         // Append the table to the document.    
  60.         doc.MainDocumentPart.Document.Body.Append(table);  
  61.         doc.Close();  
  62.     }  
  63. }
Design the table as you wish.

And have fun by using the OpenXML.

 


Similar Articles