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.

Wed Dec 06, 2017 11:41 am

Hi,

We have around 10-15 tables in a word document and I need to extract specific tables and convert those tables in html. Is there a to extract the tables from word document and use and identifier for ex "Alt text" and convert those tables in html tables.

Regards,
Nitin Jain

nitinfromAnalec
 
Posts: 3
Joined: Wed Dec 06, 2017 9:18 am

Thu Dec 07, 2017 2:46 am

Hello,

Thanks for your inquiry.
Please refer to the below code to extract tables and convert them to html string.
Code: Select all
            Document TempDoc = new Document(path);
            List<Table> tbs = new List<Table>();

            //get all tables in the document body part
            foreach (Section s in TempDoc.Sections)
            {
                TableCollection temp = s.Tables;
                foreach (Table tb in temp)
                {
                    tbs.Add(tb);
                }
            }

            //clone the document and clear the sections.
            Document newDoc = TempDoc.Clone();
            //add a new section
            newDoc.Sections.Clear();
            if (tbs.Count > 0)
            {
                //eg. convert the 3rd table and 5th table to html string.
                newDoc.AddSection().Body.ChildObjects.Add((tbs[2] as Table).Clone());
                newDoc.AddSection().Body.ChildObjects.Add((tbs[4] as Table).Clone());
                newDoc.SaveToFile("tableHtml.html", FileFormat.Html);
            }

As for the identifier "Alt Text", I'm afraid you need to handle it on your side with the result html.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Dec 07, 2017 5:23 am

Hi,

Thanks for the Reply. I can loop through the table using interpop assemblies and get the required tables. Is there a way to copy the table in spire document, I am putting down my code here.
Code: Select all
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim file_name As String
        'Dim spire_document As Document

        Dim word_application As Word.Application
        Dim word_document As Word.Document
        Dim is_new_word_application_created As Boolean
        Dim word_file_open_dialog As OpenFileDialog
        Dim word_content As Word.Range
        Dim word_table As Word.Table

       


        'Dim spire_document As Spire.Doc.Document

        is_new_word_application_created = False
        word_application = Nothing
        word_document = Nothing
        Dim new_file_name As String

        Try
            word_file_open_dialog = New OpenFileDialog

            word_file_open_dialog.Filter = "Word Doaument (*.docm)|*.docm"
            word_file_open_dialog.FilterIndex = 1

            word_file_open_dialog.Multiselect = False

            If word_file_open_dialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                file_name = word_file_open_dialog.FileName
                word_application = GetActiveWordAccessInstance(is_new_word_application_created)
                If IsNothing(word_application) = False Then

                    If is_new_word_application_created = True Then
                        word_application.Visible = False
                    End If

#If DEBUG = True Then
                    word_application.Visible = True
#End If

                    word_application.DisplayAlerts = False
                    word_document = word_application.Documents.Open(CType(file_name, Object), ConfirmConversions:=False)
                    word_document.Activate()

                    word_content = word_document.Content
                    If IsNothing(word_content) = False Then
                      For each word_table In   word_content.Tables
                            Using spire_document As New  Spire.Doc.Document
                                new_file_name = IO.Path.Combine("C:\Users\jainn\Desktop\to be deleted\aaaaa",Guid.NewGuid.ToString + ".docx")
                                spire_document.AddSection().Body.ChildObjects.Add(word_table)
                                spire_document.SaveToFile(new_file_name,FileFormat.Docx2013)
                            End Using

                      Next
                    End If
                    ' now we need to get all the tables



                End If
            End If
        Catch ex As Exception
            MsgBox("Exception occured. Exception message was: " + ex.Message)
        Finally
            Try
                If IsNothing(word_document) = False Then
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(word_document)
                    word_document = Nothing
                End If
            Catch ex As Exception
            End Try

            Try
                If IsNothing(word_application) = False Then
                    word_application.DisplayAlerts = True

                    If is_new_word_application_created = True Then
                        word_application.Quit()
                    End If

                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(word_application)
                End If
            Catch ex As Exception
            End Try
        End Try


    End Sub

nitinfromAnalec
 
Posts: 3
Joined: Wed Dec 06, 2017 9:18 am

Thu Dec 07, 2017 6:35 am

Hello,

Thanks for your reply.
You need to use the method "clone" under the Spire.Doc.Table object to copy the tables, but I have observed that you were looping through the table objects of the interop, which made it impossible to copy the tables. Why not implementing the whole process using our Spire.Doc? :) Please refer to the entire sample code below.
Code: Select all
Dim word_file_open_dialog As New OpenFileDialog()
         word_file_open_dialog.Filter = "Word Doaument (*.docm)|*.docm"
         word_file_open_dialog.FilterIndex = 1
         word_file_open_dialog.Multiselect = False
         Dim file_name As String

         If word_file_open_dialog.ShowDialog() = DialogResult.OK Then
            file_name = word_file_open_dialog.FileName

            Dim TempDoc As New Document(file_name)
            Dim tbs As New List(Of Table)()

            'get all tables in the document body part
            For Each s As Section In TempDoc.Sections
               Dim temp As TableCollection = s.Tables
               For Each tb As Table In temp
                  tbs.Add(tb)
               Next tb
            Next s

            'clone the document and clear the sections.
            Dim newDoc As Document = TempDoc.Clone()
            'add a new section
            newDoc.Sections.Clear()
            If tbs.Count > 0 Then
               'eg. convert the 3rd table and 5th table to html string.
               newDoc.AddSection().Body.ChildObjects.Add((TryCast(tbs(2), Table)).Clone())
               newDoc.AddSection().Body.ChildObjects.Add((TryCast(tbs(4), Table)).Clone())
               newDoc.SaveToFile("newfile.docx",FileFormat.Docx2013)
               newDoc.SaveToFile("tableHtml.html", FileFormat.Html)
            End If
         End If


Please feel free to write back if you have any doubt.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Dec 07, 2017 11:24 am

Hi,

Thanks for your reply. I am using interpop assemblies because I need to export certain tables instead of all tables in a word document. No. of tables would be dynamic hence interpop. anyways I am coming to achieve this using your library, however When I export the Save As the document to HTML using your library, the table width is same as in word document. is there a way to set the exported table width in 100% instead of fixed width.

Regards,
Nitin Jain

nitinfromAnalec
 
Posts: 3
Joined: Wed Dec 06, 2017 9:18 am

Fri Dec 08, 2017 2:26 am

Hi Jain,

Thanks for your response.
Our Spire.Doc supports setting the table width to be 100 percent. Please refer to the following code.
Code: Select all
Private Sub button19_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim word_file_open_dialog As OpenFileDialog = New OpenFileDialog()
    word_file_open_dialog.Filter = "Word Doaument (*.docm)|*.docm"
    word_file_open_dialog.FilterIndex = 1
    word_file_open_dialog.Multiselect = False
    Dim file_name As String
    If word_file_open_dialog.ShowDialog() = DialogResult.OK Then
        file_name = word_file_open_dialog.FileName
        Dim TempDoc As Document = New Document(file_name)
        Dim tbs As List(Of Table) = New List(Of Table)()
        For Each s As Section In TempDoc.Sections
            Dim temp As TableCollection = s.Tables
            For Each tb As Table In temp
                tbs.Add(tb)
            Next
        Next

        Dim newDoc As Document = TempDoc.Clone()
        newDoc.Sections.Clear()
        Dim sec As Section = newDoc.AddSection()
        Dim extractTbs As List(Of Table) = New List(Of Table)()
        extractTbs.Add(tbs(2).Clone())
        extractTbs.Add(tbs(3).Clone())
        For Each tb As Table In extractTbs
            tb.PreferredWidth = New PreferredWidth(WidthType.Percentage, 100)
        Next

        For i As Integer = 0 To extractTbs.Count - 1
            sec.Body.ChildObjects.Add(extractTbs(i))
        Next

        newDoc.SaveToFile("newfile.docx", FileFormat.Docx2013)
        newDoc.SaveToFile("tableHtml.html", FileFormat.Html)
    End If
End Sub

Feel free to write back if you have any questions.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Jun 28, 2019 9:10 am

Hi,

I have around 10-20 tables in a word document and I need to extract specific tables and convert those tables in excel. Is there a way to extract the tables from word document and convert those tables in excel tables in C#?

Regards,
Sakshi

s_dhiman
 
Posts: 3
Joined: Fri Jun 28, 2019 9:06 am

Fri Jun 28, 2019 10:54 am

Dear Sakshi,

Thanks for your inquiry.
The concepts of table in Word and Excel are different, there is no directly way to achieve it. An indirect approach may suit for you, that is creating a DataTable and filling it with the data of Word table, then inserting the DataTable in Excel file. Below is the sample code for your kind reference. If there is any question, please feel free to write back.
Code: Select all
 Document doc = new Spire.Doc.Document();
 doc.LoadFromFile("table.docx");
 Section section = doc.Sections[0];
 Table table = section.Tables[0] as Table;
 DataTable dt = new DataTable();
 //add columns
 for (int i = 0; i < table.Rows[0].Cells.Count; i++)
 {
     dt.Columns.Add("column"+i.ToString(), typeof(string));
 }
 //fill data of table to DataTable
 for (int r = 0; r < table.Rows.Count; r++)
 {
     DataRow row = dt.NewRow();
     for (int c = 0; c < table.Rows[r].Cells.Count; c++)
     {
         string text = "";
         if (table.Rows[r].Cells[c].Paragraphs.Count < 2)
         {
             text = table.Rows[r].Cells[c].Paragraphs[0].Text;
         }
         else
         {
             foreach (Paragraph p in table.Rows[r].Cells[c].Paragraphs)
             {
                 text +="\n"+ p.Text;
             }
         }
         row[c] = text;
     }
     dt.Rows.Add(row);
 }
 //create a Workbook
 Workbook wb = new Workbook();
 Worksheet sheet = wb.Worksheets[0];
 //insert the DataTable into Excel
 sheet.InsertDataTable(dt, false, 1, 1);
 wb.SaveToFile("insertTableToExcel.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Tue Jul 02, 2019 5:27 am

Hi Nina,
I'm still facing errors while running this code. Please assist.

s_dhiman
 
Posts: 3
Joined: Fri Jun 28, 2019 9:06 am

Tue Jul 02, 2019 5:54 am

Hello,

Thanks for your feedback.
As soon as you get the following pieces of information ready, we'll start investigation into your issue and provide an accurate response. Thanks in advance.
1) The detailed information of errors you faced.
2) Your input Word file as well as generated Excel file. You could send them to us via email (support@e-iceblue.com).

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Wed Jul 03, 2019 6:12 am

Hi Nina,
I have sent you an email. Please check and revert.

s_dhiman
 
Posts: 3
Joined: Fri Jun 28, 2019 9:06 am

Wed Jul 03, 2019 8:55 am

Hello,

Thanks for your more information. Here I made a demo for your reference, please have a check. If there is any question, please provide your input Word document to help further look into it.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc