Here’s simple example how can we export some table data to docx file using Open XML SDK. It creates docx file and inserts table in it.
Steps
1> Create a New Web applciation project in visual studio 2008
File-> New->Visual C#->Web ->ASP.Net Web application
2> For using open xml sdk , you need to add following refrences:
WindowsBase
DocumentFormat.OpenXml
3> Create a class file called export.cs
4> Add the following code in it.
using System;using System.Collections.Generic;using System.Linq;using System.Web;using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Spreadsheet;using DocumentFormat.OpenXml.Wordprocessing;namespace WebApplication1{public class Exportmod{public void waddtable(string[,] data){WordprocessingDocument doc = WordprocessingDocument.Create(@”c:\dox-export.docx”,WordprocessingDocumentType.Document);MainDocumentPart mainDocPart = doc.AddMainDocumentPart();mainDocPart.Document = new Document();Body body = new Body();mainDocPart.Document.Append(body);//rinks@::creating new tableDocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();TableProperties props = new TableProperties(new TableBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new InsideHorizontalBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new InsideVerticalBorder { Val = new EnumValue(BorderValues.Single), Size = 12 })); table.AppendChild(props); for (int i = 0; i <= data.GetUpperBound(0); ++i){TableRow row = new TableRow();for (int j = 0; j <= data.GetUpperBound(0); ++j){TableCell cell = new TableCell();cell.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));cell.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = “1200″ }));row.Append(cell);}table.Append(row);}body.Append(table);doc.MainDocumentPart.Document.Save();doc.Dispose();}}}5> Thats it. After this you just need to make a button in default.aspx page and call the function as shown below:protected void Button1_Click(object sender, EventArgs e)
{
const int rows = 8;
const int columns = 8;
string[,] stringArray = new string[rows, columns];
stringArray[0, 0] = “Asset Name”;
stringArray[0, 1] = “a”;
stringArray[0, 2] = “b”;
stringArray[0, 3] = “c”;
stringArray[0, 4] = “d”;
stringArray[0, 5] = “e”;
stringArray[0, 6] = “f”;
stringArray[1, 0] = “P”;
stringArray[2, 3] = “K”;
stringArray[3, 5] = “B”;
stringArray[4, 4] = “A”;
stringArray[5, 3] = “E”;
Exportmod em = new Exportmod();
em.waddtable(stringArray);
}
So now if you open the docx file you can see the table inserted in it as shown below
No comments:
Post a Comment