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



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 :18/06/2010   Points :10   Category: XML    URL: http://www.rinkur.wordpress.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>(BorderValues.Single), Size = 12 },
    new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
    new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
    new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
    new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
    new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 }));
    table.AppendChild<TableProperties>(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.





Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
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