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.
- using DocumentFormat.OpenXml.Wordprocessing;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml;
- 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:
-
- int columncount = dataGridView1.Columns.Count;
- using(WordprocessingDocument wpc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document)) {
-
- MainDocumentPart mainPart = wpc.AddMainDocumentPart();
-
- mainPart.Document = new Document();
-
- Body body = new Body();
- Table table = new Table();
-
- TableProperties tblProp = new TableProperties(
- new TableBorders(
- new TopBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- },
- new BottomBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- },
- new LeftBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- },
- new RightBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- },
- new InsideHorizontalBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- },
- new InsideVerticalBorder() {
- Val = new EnumValue < BorderValues > (BorderValues.BasicBlackDots), Size = 20
- }));
-
- table.AppendChild < TableProperties > (tblProp);
-
- TableRow tr = new TableRow();
-
- TableCell[] tc1 = new TableCell[5];
-
- for (int i = 0; i < columncount; i++) {
- string[] columnhead = new string[columncount];
- columnhead[i] = dataGridView1.Columns[i].HeaderText.ToString();
-
- tc1[i] = new TableCell();
-
- tc1[i].Append(new TableCellProperties(
- new TableCellWidth() {
- Type = TableWidthUnitValues.Dxa, Width = "2400"
- }));
-
- tc1[i].Append(new Paragraph(new Run(new Text(columnhead[i]))));
-
- tr.Append(tc1[i]);
- }
-
- table.Append(tr);
- body.Append(table);
- mainPart.Document.Append(body);
- mainPart.Document.Save();
Then open the document and pass the data in the GridView using the following code.
- int rowcount = dataGridView1.Rows.Count;
- int columncount = dataGridView1.Columns.Count;
- {
- using(WordprocessingDocument doc = WordprocessingDocument.Open(filename, true)) {
-
- Table table = new Table();
-
- TableProperties tblProp = new TableProperties(
- new TableBorders(
- new TopBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- },
- new BottomBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- },
- new LeftBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- },
- new RightBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- },
- new InsideHorizontalBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- },
- new InsideVerticalBorder()
- {
- Val = new EnumValue < BorderValues > (BorderValues.BasicThinLines), Size = 20
- }));
-
- table.AppendChild < TableProperties > (tblProp);
- for (int i = 0; i < rowcount; i++)
- {
-
- TableRow tr = new TableRow();
-
- TableCell[] tc1 = new TableCell[columncount];
- for (int j = 0; j < columncount; j++)
- {
- tc1[j] = new TableCell();
- string data1 = dataGridView1.Rows[i].Cells[j].Value.ToString();
-
- tc1[j].Append(new TableCellProperties(
- new TableCellWidth()
- {
- Type = TableWidthUnitValues.Dxa, Width = "2400"
- }));
-
- tc1[j].Append(new Paragraph(new Run(new Text(data1))));
- tr.Append(tc1[j]);
- }
-
- table.Append(tr);
- }
-
- doc.MainDocumentPart.Document.Body.Append(table);
- doc.Close();
- }
- }
Design the table as you wish.
And have fun by using the OpenXML.