How to use tabular data with Xceed.Docx


Xceed.Words is a .Net library which can be use to process various file format such as xps,docx,pdf,html etc. In the last post we learned how to use templates with Docx.

We can create and use Tabular data with Table and Row object provided by this library. Based on document

var t = document.Tables;

The columns are automatically generated , so no need to worry about it. We can use Row object and Insert method to create rows.

var sorted = rep_grid.Items.Cast(Model).ToList();
{
Row newrow;
int i = 1;
foreach (var v in sorted)
{
newrow = t[1].InsertRow();
newrow.Cells[0].Paragraphs[0].Append(v.Date.ToShortDateString());

Complete Code

                using (var document = Xceed.Words.NET.DocX.Create(fname))
                {
                    document.ApplyTemplate(@"doctemplates /CashLedger.dotx");
                    document.ReplaceText("[MyCompany]", ViewModels_Variables.ModelViews.CompanyProfile[0].company);
                    document.ReplaceText("[Clandmark]", " | " + ViewModels_Variables.ModelViews.CompanyProfile[0].lmark);
 
                    var t = document.Tables;
                    var sorted = rep_grid.Items.Cast<CashBookModel>().ToList();
                    {
                        Row newrow;
                        int i = 1;
                        foreach (var v in sorted)
                        {
                            ++i;
                            newrow = t[1].InsertRow();
                            newrow.Cells[0].Paragraphs[0].Append(v.Date.ToShortDateString());
}
document.Save();
document.Dispose();


Do you know that Xceed allows you to convert documents to other format by simply using

doc.SaveToFile(xpsfn, Spire.Doc.FileFormat.XPS);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.