Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Feb 06, 2015 2:09 am

Hi,

Can the combination of Spire.Doc and Spire.XLS read a .docx file and convert embedded Excel spreadsheets into Word Tables? If not, what would be needed in order to accomplish this?

Cheers,

Alex

alexneblett01
 
Posts: 14
Joined: Sat Oct 18, 2014 2:09 pm

Fri Feb 06, 2015 3:21 am

Hello alexneblett01,

Thanks for your inquiry.

Please refer to the code below to meet your requirement:
Code: Select all
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("4380.docx", Spire.Doc.FileFormat.Docx2010);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc