简体   繁体   中英

Fastest way to populate word table in c#?

Currently, I do it through for loop. But, my table has 58 rows and 13 columns and it takes around 20 seconds to save word document. Is there more better way to populate word table with data?

        for (int i = 1; i<58; i++)
        {
            document.Tables[2].Columns[4].Cells[i].Range.Text = form.report[i].debInfo.value1.ToString();
            document.Tables[2].Columns[5].Cells[i].Range.Text = form.report[i].debInfo.value2.ToString();
            document.Tables[2].Columns[6].Cells[i].Range.Text = form.report[i].debInfo.value3.ToString();
            document.Tables[2].Columns[7].Cells[i].Range.Text = form.report[i].debInfo.value4.ToString();
            document.Tables[2].Columns[8].Cells[i].Range.Text = form.report[i].kredInfo.value1.ToString();
            document.Tables[2].Columns[9].Cells[i].Range.Text = form.report[i].kredInfo.value2.ToString();
            document.Tables[2].Columns[10].Cells[i].Range.Text = form.report[i].kredInfo.value3.ToString();
            document.Tables[2].Columns[11].Cells[i].Range.Text = form.report[i].kredInfo.value4.ToString();
            document.Tables[2].Columns[12].Cells[i].Range.Text = form.report[i].kredInfo.value5.ToString();
            document.Tables[2].Columns[13].Cells[i].Range.Text = form.report[i].value.ToString();
        }

Just checked the execution time of my "save to word" method it takes ~12 seconds to execute. For loop takes ~5 sec and another ~6-7 seconds goes to generate word document.

I have tried the code from this article :

const int NumRows = 58;
const int NumCols = 13;
object objMiss = System.Reflection.Missing.Value;
objTab1 = objDoc.Tables.Add(objWordRng, NumRows, NumCols,
                        ref objMiss, ref objMiss); //add table object in word document
objTab1.Range.ParagraphFormat.SpaceAfter = 6;
int iRow, iCols;
string strText;

for (iRow = 1; iRow <= NumRows; iRow++)
    for (iCols = 1; iCols <= NumCols; iCols++)
       {
           strText = "row:" + iRow + "col:" + iCols;
           objTab1.Cell(iRow, iCols).Range.Text = strText; //add some text to cell
       }

It takes a runtime of 4s in visible mode and 2s with objApp.visible = false .

The reason why your code is slower might be due to the fact that you are resolving chains of object references in every line.

Rather than writing

document.Tables[2].Columns[4].Cells[i].Range.Text = form.report[i].debInfo.value1.ToString();

try to store the frequently used object references in auxiliary arrays. In this example, document.Tables[2].Columns[4] is re-evaluated for every row. You could evaluate it once and store it in a local array. The evaluation of COM Object properties involves a lot of background processing and is quite slow.

There might be other tricks to speed-up the Word COM-object. In Excel it makes a huge difference to switch off the display update. The Word layout rendering should be suspended while adding table cells. But I am not sure how to do this.

I Found the following to be a bit faster, Using the Range.ConvertToTable Method

Range r = doc.Paragraphs.Add().Range;

string s = string.Join("\n", data.Select(a => string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",a.Item1,a.Item2,a.Item3,a.Item4,a.Item5,a.Item6,a.Item7)));

r.Text = s;

Table t = r.ConvertToTable(Separator: WdTableFieldSeparator.wdSeparateByTabs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM