Spire.Doc

In our daily work, we may receive Word documents that will sometimes contain embedded Excel object (sheet) and we need to convert embedded Excel sheet to Word table so that we can easily change the date or format the table with style. In this article, you will learn how to convert embedded Excel sheet to Word table using Spire.Doc and Spire.XLS in C#, VB.NET.

Firstly, you need to download Spire.Office because Spire.Doc and Spire.XLS will be used in the same program. Add Spire.Doc.dll and Spire.XLS.dll as references in your VS project. Then follow the program guidance below to finish this work.

Step 1: Create a new Word document, load the sample file. Get the paragraph that contains the Excel object from the section. Initialize a new datatable.

Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
DataTable dt = new DataTable();

Step 2: Traverse every DocumentObject in the paragraph, use IF statement to test if DocumentObject is OLE object, use another IF statement to test if OLE object type is Excel.Sheet.12. If yes, save the data of OLE object to a workbook through LoadFromStrem(). Then export data from worksheet to datatable.

foreach (DocumentObject obj in para.ChildObjects)
{
    if (DocumentObjectType.OleObject == obj.DocumentObjectType)
    {
        DocOleObject dObj = obj as DocOleObject;
        if (dObj.ObjectType == "Excel.Sheet.12")
        {
            Workbook wb = new Workbook();
            wb.LoadFromStream(new MemoryStream(dObj.NativeData));
            Worksheet ws = wb.Worksheets[0];
            dt = ws.ExportDataTable(ws.AllocatedRange, false);
        }
    }
}

Step 3: Create a new Word table and set row number and column number according to rows and columns of datatable. Export data from datatable to Word table.

Table table = section.AddTable(true);
 table.ResetCells(dt.Rows.Count, dt.Columns.Count);

 for (int i = 0; i < dt.Rows.Count; i++)
 {
     for (int j = 0; j < dt.Columns.Count; j++)
     {
         string text = dt.Rows[i][j] as string;
         table.Rows[i].Cells[j].AddParagraph().AppendText(text);
     }
 }

Step 4: Save the file.

doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);

Result:

How to Convert Embedded Excel Sheet to Word Table in C#, VB.NET

Full Code:

[C#]
Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
DataTable dt = new DataTable();

foreach (DocumentObject obj in para.ChildObjects)
{
    if (DocumentObjectType.OleObject == obj.DocumentObjectType)
    {
        DocOleObject dObj = obj as DocOleObject;
        if (dObj.ObjectType == "Excel.Sheet.12")
        {
            Workbook wb = new Workbook();
            wb.LoadFromStream(new MemoryStream(dObj.NativeData));
            Worksheet ws = wb.Worksheets[0];
            dt = ws.ExportDataTable(ws.AllocatedRange, false);
        }
    }
}

Table table = section.AddTable(true);
table.ResetCells(dt.Rows.Count, dt.Columns.Count);

for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        string text = dt.Rows[i][j] as string;
        table.Rows[i].Cells[j].AddParagraph().AppendText(text);
    }
}

doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);
[VB.NET]
Dim doc As New Document("Sample.docx", Spire.Doc.FileFormat.Docx2010)
Dim section As Section = doc.Sections(0)
Dim para As Paragraph = section.Paragraphs(2)
Dim dt As New DataTable()

For Each obj As DocumentObject In para.ChildObjects
	If DocumentObjectType.OleObject = obj.DocumentObjectType Then
		Dim dObj As DocOleObject = TryCast(obj, DocOleObject)
		If dObj.ObjectType = "Excel.Sheet.12" Then
			Dim wb As New Workbook()
			wb.LoadFromStream(New MemoryStream(dObj.NativeData))
			Dim ws As Worksheet = wb.Worksheets(0)
			dt = ws.ExportDataTable(ws.AllocatedRange, False)
		End If
	End If
Next

Dim table As Table = section.AddTable(True)
table.ResetCells(dt.Rows.Count, dt.Columns.Count)

For i As Integer = 0 To dt.Rows.Count - 1
	For j As Integer = 0 To dt.Columns.Count - 1
		Dim text As String = TryCast(dt.Rows(i)(j), String)
		table.Rows(i).Cells(j).AddParagraph().AppendText(text)
	Next
Next

doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010)

With the help of Spire.Doc for .NET, developers can easily set bullet style for the existing word documents via invoke p.ListFormat.ApplyBulletStyle() method to format. This article will show you how to convert HTML list into word and set the bullet style for the word list in C#.

Firstly, please view the effective screenshot for the result word document:

How to set word bullet style by appending the HTML code in C#

Here comes to the steps:

Step 1: Create a new document and add a section to the document.

Document document = new Document();
Section section=document.AddSection();

Step 2: Add word list to the paragraph by appending HTML.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendHTML("<ol><li>Version 1</li><li>Version 2</li><li>Version 3</li></ol>");

Step 3: Set the bullet style for the paragraph.

foreach (Paragraph p in section.Paragraphs)
{
 p.ApplyStyle(BuiltinStyle.Heading2);
 p.ListFormat.CurrentListLevel.NumberPosition = 20;
 p.ListFormat.CurrentListLevel.TextPosition = 30;
 }

Step 4: Save the document to file

document.SaveToFile("result.docx",FileFormat.Docx);

Full codes:

static void Main(string[] args)
{

    Document document = new Document();
    Section section = document.AddSection();
    Paragraph paragraph = section.AddParagraph();
    paragraph.AppendHTML("<ol><li>Version 1</li><li>Version 2</li><li>Version 3</li></ol>");


    foreach (Paragraph p in section.Paragraphs)
    {

        p.ApplyStyle(BuiltinStyle.Heading2);

        p.ListFormat.CurrentListLevel.NumberPosition = 20;

        p.ListFormat.CurrentListLevel.TextPosition = 30;

    }

        document.SaveToFile("result.docx",FileFormat.Docx);


}

Spire.Doc can help developers to create word table with data and format cells easily and it also supports to add text watermark into the word documents. This article will show you how to create a vertical table at one side of the word document, which looks like the vertical watermark in the word document.

Firstly, please check the effective screenshot of the vertical table at the right of the word document added by Spire.Doc:

How to create vertical table at one side of the word document

Here comes to the steps of how to create vertical table in C#.

Step 1: Create a new document and add a section to the document.

Document document = new Document();
Section section=document.AddSection();

Step 2: Add a table with rows and columns and set the text for the table.

Table table = section.AddTable();
table.ResetCells(1, 1);
TableCell cell = table.Rows[0].Cells[0];
table.Rows[0].Height = 150;
cell.AddParagraph().AppendText("Draft copy in vertical style");

Step 3: Set the TextDirection for the table to RightToLeftRotated.

cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;

Step 4: Set the table format.

table.TableFormat.WrapTextAround = true;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Page;
table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Page;
table.TableFormat.Positioning.HorizPosition = section.PageSetup.PageSize.Width- table.Width;
table.TableFormat.Positioning.VertPosition = 200;

Step 5: Save the document to file.

document.SaveToFile("result.docx");

Full codes in C#:

using Spire.Doc;
using Spire.Doc.Documents;

namespace CreateVerticalTable
{
    class Program
    {
        static void Main(string[] args)
        {

            Document document = new Document();
            Section section=document.AddSection();
            Table table = section.AddTable();
            table.ResetCells(1, 1);
            TableCell cell = table.Rows[0].Cells[0];
            table.Rows[0].Height = 150;
            cell.AddParagraph().AppendText("Draft copy in vertical style");
            cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;
            table.TableFormat.WrapTextAround = true;
            table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Page;
            table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Page;
            table.TableFormat.Positioning.HorizPosition = section.PageSetup.PageSize.Width- table.Width;
            table.TableFormat.Positioning.VertPosition = 200;

            document.SaveToFile("result.docx");

        }
    }
}
Page 1 of 27