In this tip, I am going to show how to export the gridview
table into document using the OpenXML.
Download the OPENXML from here.
After downloading the SDK, install the .exe into your PC.
Now, open a new project in your C# tool.
Add the OpenXML SDK Reference in your project. Also include Windowsbase into the reference.
Using the Code
Now use the following namespace in your source code:
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using System.IO.Packaging;
After including the above namespace, design your table using the XML file. Mine looks like below:
Add the data to the Datagridview
.
Now it’s time for us to create document Template using the below 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 then pass the data in the Gridview
by using the below 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 according to your wish.
And have fun by using the OpenXML.
Happy coding!!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.