.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

exporting table in ms word 2007 using openxml sdk

Posted By:rinku rambhiya       Posted Date: June 18, 2010    Points: 10    Category: XML    URL: http://www.dotnetspark.com  

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 table
    DocumentFormat.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.




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend