How to transfer data to tables and from tables back as picture? I just learned Aspose Word. Thankyou.
@asposewuser Please see our documentation to learn how to work with tables:
https://docs.aspose.com/words/net/table-overview/
In your case since it is required to fill repeating data, you can consider using Mail Merge with Regions or Linq Reporting Engine.
Unfortunately, there is no direct and common way to read data back from the table, so it will be required to parse your table structure.
I searched, tried the code before asking but got problems.
- The data entered into the table has an extra blank line above as shown in Figure 1. Sub ToTable()
- I did not add the correct data location. The data is below the words “student” as shown in Figure 2. Sub OutTable()
I have sent the code I use, can you help me fix it?
I used Google Translate so I hope you understand.
Code
Sub ToTable()
Dim srcDoc, dstDoc As Aspose.Words.Document
srcDoc = New Aspose.Words.Document("Input.docx")
dstDoc = New Aspose.Words.Document
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
Dim i, num As Integer
Dim nodes As NodeCollection
Dim enume As IEnumerator
Dim node1, node2, node3 As Node
Dim tbl As Table
num = srcDoc.Sections.Count
builder.MoveToDocumentEnd()
tbl = builder.StartTable()
For i = 0 To num - 1
nodes = srcDoc.Sections(i).Body.ChildNodes
enume = nodes.GetEnumerator()
While enume.MoveNext
node1 = enume.Current
node2 = node1.Clone(True)
node3 = dstDoc.ImportNode(node2, True)
builder.InsertCell()
builder.InsertCell()
tbl.LastRow.Cells(1).AppendChild(node3)
builder.EndRow()
End While
Next
builder.EndTable()
dstDoc.Save("Output-1.docx")
End Sub
Sub OutTable()
Dim srcDoc, dstDoc As Aspose.Words.Document
srcDoc = New Aspose.Words.Document("Output.docx")
dstDoc = New Aspose.Words.Document
Dim i, num, index As Integer
Dim node1, node2, node3 As Node
Dim sec As Section
Dim body As Body
Dim tbl As Table
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
sec = dstDoc.Sections(0)
body = sec.Body
tbl = srcDoc.Sections(0).Body.Tables(0)
num = tbl.Rows.Count
For i = 0 To num - 1
builder.Font.Bold = True
builder.ParagraphFormat.Alignment = ParagraphAlignment.Justify
builder.Write("Student " + CStr(index) + ". ")
node1 = tbl.Rows(i).Cells(1).LastChild.Clone(True)
node2 = node1.Clone(True)
node3 = dstDoc.ImportNode(node2, True)
body.AppendChild(node3)
index += 1
Next
dstDoc.Save("Output-2.docx")
End Sub
@asposewuser Could you please attach your documents here for our reference? Unfortunately it is impossible to analyze the problem using screenshots.
It is the same data as #1.
https://app.box.com/s/hphw3u27kk4ztse2lta28oyxhjm5gnw0
@asposewuser Thank you for additional information. Please try using the following code for creating table:
Sub ToTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Input.docx")
Dim dstDoc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
' Join runs with same formatting. In the input document first part is bold other part is regular.
srcDoc.JoinRunsWithSameFormatting()
builder.StartTable()
' loop thourhg paragraph in the input document
For Each para As Paragraph In srcDoc.FirstSection.Body.Paragraphs
' bold runs will be inserted into the first cell.
builder.InsertCell()
Dim current As Inline = para.FirstChild
While current IsNot Nothing And current.Font.Bold
builder.InsertNode(dstDoc.ImportNode(current, True, ImportFormatMode.UseDestinationStyles))
current = current.NextSibling
End While
builder.InsertCell()
While current IsNot Nothing
builder.InsertNode(dstDoc.ImportNode(current, True, ImportFormatMode.UseDestinationStyles))
current = current.NextSibling
End While
builder.EndRow()
Next
builder.EndTable()
dstDoc.Save("C:\Temp\Output-1.docx")
End Sub
Here is the produced output: Output-1.docx (7.7 KB)
And the following code to read the data from the table:
Sub OutTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Output-1.docx")
Dim dstDoc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
For Each r As Row In srcDoc.FirstSection.Body.Tables(0).Rows
builder.Font.Bold = True
builder.Write(r.Cells(0).ToString(SaveFormat.Text).Trim)
builder.Font.Bold = False
builder.Writeln(r.Cells(1).ToString(SaveFormat.Text).Trim)
Next
dstDoc.Save("C:\Temp\Output-2.docx")
End Sub
Here is the output: Output-2.docx (7.1 KB)
Thank you for your help. This method works well when just typing text. If the data has images, it will not work. Can you help me how to handle the image? The old way works with images.
@asposewuser Please modify the code like this:
Sub OutTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Output-1.docx")
Dim dstDoc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
For Each r As Row In srcDoc.FirstSection.Body.Tables(0).Rows
For Each n As Node In r.GetChildNodes(NodeType.Any, True)
If TypeOf n Is Inline Then
builder.InsertNode(dstDoc.ImportNode(n, True, ImportFormatMode.UseDestinationStyles))
End If
Next
builder.Writeln()
Next
dstDoc.Save("C:\Temp\Output-2.docx")
End Sub
It not work. The results are still only text. It was my fault for not giving enough information from the beginning.
Still the files above. I just add any image right before the student’s name (like the image #7)
@asposewuser Please modify the code like this:
Sub ToTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Input.docx")
Dim dstDoc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
' Join runs with same formatting. In the input document first part is bold other part is regular.
srcDoc.JoinRunsWithSameFormatting()
builder.StartTable()
' loop thourhg paragraph in the input document
For Each para As Paragraph In srcDoc.FirstSection.Body.Paragraphs
' bold runs will be inserted into the first cell.
builder.InsertCell()
Dim current As Node = para.FirstChild
While current IsNot Nothing And current.NodeType = NodeType.Run And DirectCast(current, Run).Font.Bold
builder.InsertNode(dstDoc.ImportNode(current, True, ImportFormatMode.UseDestinationStyles))
current = current.NextSibling
End While
builder.InsertCell()
While current IsNot Nothing
builder.InsertNode(dstDoc.ImportNode(current, True, ImportFormatMode.UseDestinationStyles))
current = current.NextSibling
End While
builder.EndRow()
Next
builder.EndTable()
dstDoc.Save("C:\Temp\Output-1.docx")
End Sub
Sub OutTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Output-1.docx")
Dim dstDoc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(dstDoc)
For Each r As Row In srcDoc.FirstSection.Body.Tables(0).Rows
For Each n As Node In r.GetChildNodes(NodeType.Any, True)
If TypeOf n Is Inline Or TypeOf n Is Shape Then
builder.InsertNode(dstDoc.ImportNode(n, True, ImportFormatMode.UseDestinationStyles))
End If
Next
builder.Writeln()
Next
dstDoc.Save("C:\Temp\Output-2.docx")
End Sub
How wonderful. Thank you very much.
It’s me again and it’s still the same problem. In case the data has a table. For example: Instead of the image like #7 being any table, the ToTable function cannot import the table. The OutTable function will convert the table into text.
Sorry if I’m bothering you. If possible, I hope you can continue to help. Thank you very much.
P/S: In OutTable. I tried changed to “UseDestinationStyles”, add “Or TypeOf n Is Table”. But not work.
@asposewuser The table cannot be a child of the paragraph. So the situation is not the same as with shape. Please see our documentation to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
In your input document there should be some delimiters for data to make it possible to convert the data into the table format.
Could you please attach your same input document and expected output. You simply drag and drop the file into the topic reply.
@asposewuser As I have mentioned table cannot be a child of paragraph, these nodes are on the same level in document structure. So the table should be inserted after the paragraph. Please try using the following code:
Sub OutTable()
Dim srcDoc As New Aspose.Words.Document("C:\Temp\Input.docx")
Dim dstDoc As New Aspose.Words.Document()
For Each r As Row In srcDoc.FirstSection.Body.Tables(0).Rows
For Each c As Cell In r.Cells
For Each child As Node In c.GetChildNodes(NodeType.Any, False)
If child.NodeType = NodeType.Paragraph Then
Dim para As Paragraph = DirectCast(child, Paragraph)
For Each n As Node In para.GetChildNodes(NodeType.Any, False)
dstDoc.LastSection.Body.LastParagraph.AppendChild(dstDoc.ImportNode(n, True, ImportFormatMode.UseDestinationStyles))
Next
Else
dstDoc.LastSection.Body.AppendChild(dstDoc.ImportNode(child, True, ImportFormatMode.UseDestinationStyles))
dstDoc.LastSection.Body.AppendChild(New Paragraph(dstDoc))
End If
Next
Next
dstDoc.LastSection.Body.AppendChild(New Paragraph(dstDoc))
Next
dstDoc.Save("C:\Temp\Output.docx")
End Sub
Thank you for your continued help. But this method will not output the data in the right place if you add text and repeat over the rows. For example: Before outputting the 1st row, add the word “Row 1”, for example. Same for other rows.
@asposewuser Unfortunately, it is not quite clear what you mean. As I can see the output produced by the code is correct. You should note that table cannot be child of the paragraph, so it will inserted after the paragraph.
Please see our documentation to learn mode about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
Thank you. I will learn more.