Is there any C# Word Interop solution to convert a piece of text to a Richtext-format table other than the ConvertToTable() method?

Dan Xiao 406 Reputation points
2021-08-20T09:19:05.187+00:00

In our project, now we are using mail-merge solution to populate data from a SQL Server DB into a Word table, and then eventually generate a Word document, based on a Word template.

In the Word Template, the original Word table gets only a Header, and a blank row in the Body. (see the screenshot below).
124994-template-table.jpg

However, there may be many records in the Database to be populated into the Word table, and thus the rows must be increased greatly to match the # of records in DB. So far we are using the C# Word InterOp method Word.Table.Rows.Add() to add rows one by one to match the # of records in DB. The snippet of C# codes is shown below.

==============================================================

            while (wordTable.Rows.Count < (dataTable.Rows.Count + colNameRow))  
            {  
                wordTable.Rows.Add();  
            }  

==============================================================

However, if there are hundreds or even thousands of records in a DB, the Word.Table.Rows.Add() method will definitely hit a performance issue. For example, to add 200 rows into a Word table, it will take about 35 seconds, from our benchmark test result.

After that, we surfed Internet and found out a C# Word Interop method, namely ConvertToTable(), which can convert a text into a table, and have a much higher performance than the Table.Rows.Add() method. Please refer to the hyperlink filling-table-takes-a-lot-of-time-in-ms-word

However, the ConvertToTable() method can only generate a table with a plain format, and there is not any RichText format, e.g, background color, the various column widths, the various text fonts, the various text effects (like bold/italic/underlined/color), so on and so forth. You will have to write more C# codes to apply the Richtext format upon the generated plain-format table, which will take more overhead and reduce the overall performance.

Now there is a C# Word Interop method, namely ConvertToText(), to convert the plain text in a range of a table into a piece of text. May I ask whether Microsoft can provide a solution, something like ConvertToRichtext(), which can not only encode the plain text of a table, but also can encode the Richtext-Format info of a table, e.g., the background color, the text font/color/effect, into a string. Subsequently, we can apply something like a ConvertToRichtextTable() method to convert the encoded string into a Richtext format. The solution will definitely bring us not only a high performance, but also an elegant Richtext table?

Thanks for any help in this regard.

Rgds,
Daniel

Developer technologies | C#
{count} votes

Accepted answer
  1. Dan Xiao 406 Reputation points
    2021-08-29T01:23:07.443+00:00

    Hello, JackJJun-MSFT,

    Thank you for the feedback. I will try to follow up.

    Best Regards,
    Daniel

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.