使用互操作在C#中创建Word文档的更快方法

使用互操作在C#中创建Word文档的更快方法,c#,performance,ms-word,interop,C#,Performance,Ms Word,Interop,我对C#word文档有点问题。 我使用Word.Interop库创建.doc文档。 我在这个文档中放了一个动态表,在最后一列中有一些文本和图片 我的问题是保存/生成文档的速度非常慢(表中只有30行需要4秒,54行需要7秒)。 对我来说时间太多了。 我需要一些东西来创建和保存文件在大约1秒或更短的时间 有没有办法比Word.Interop更快地创建文档? 也许是模板文档? 但是如何用模板文档中的动态大小填充表呢 Microsoft.Office.Interop.Word.Application W

我对C#word文档有点问题。 我使用Word.Interop库创建.doc文档。 我在这个文档中放了一个动态表,在最后一列中有一些文本和图片 我的问题是保存/生成文档的速度非常慢(表中只有30行需要4秒,54行需要7秒)。 对我来说时间太多了。 我需要一些东西来创建和保存文件在大约1秒或更短的时间

有没有办法比Word.Interop更快地创建文档? 也许是模板文档? 但是如何用模板文档中的动态大小填充表呢

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Word.Document adoc;
Word.Range rng;
adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
rng = adoc.Range(ref start1, ref missing);

adoc.Tables.Add(rng, list.Count + 2, 4);
adoc.PageSetup.LeftMargin = 20.0f;
adoc.PageSetup.RightMargin = 20.0f;

adoc.Tables[1].Columns[2].Cells.PreferredWidth = 60;
adoc.Tables[1].Columns[1].Cells.PreferredWidth = 200;
adoc.Tables[1].Cell(1, 1).Range.Text = "Symbol";
adoc.Tables[1].Cell(1, 2).Range.Text = "Qnt";
adoc.Tables[1].Cell(1, 3).Range.Text = "Barcode";
adoc.Tables[1].Cell(1, 4).Range.Text = "Barcode Image";
adoc.Tables[1].Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
adoc.Tables[1].Cell(1, 1).Range.Font.Bold = 1;
adoc.Tables[1].Cell(1, 2).Range.Font.Bold = 1;
adoc.Tables[1].Cell(1, 3).Range.Font.Bold = 1;
adoc.Tables[1].Cell(1, 4).Range.Font.Bold = 1;

for (int i = 0; i < list.Count; i++)
{

    adoc.Tables[1].Cell(i + 2, 1).Range.Text = list[i].symbol;
    adoc.Tables[1].Cell(i + 2, 2).Range.Text = list[i].qnt;
    adoc.Tables[1].Cell(i + 2, 3).Range.Text = list[i].code;
    adoc.Tables[1].Cell(i + 2, 4).Range.InlineShapes.AddPicture(list[i].code_picture, ref missing, ref missing, ref missing);
}

adoc.Tables[1].Range.Font.Size = 10;
adoc.Tables[1].Rows.Height = 2.0f;

#region Border
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
adoc.Tables[1].Borders[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
#endregion

try
{                       
    object filename = @"E:\Test.doc";
    adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    // Word.Table wt1 = WordApp.Selection.Tables.Add(WordApp.Selection.Range, 5, 2, ref missing, ref missing);
    //adoc.Tables[1].Rows.Add(adoc.Tables[1].Rows[1]);
    //WordApp.Visible = true;

    //adoc.Close();
    WordApp.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
Microsoft.Office.Interop.Word.Application WordApp=new Microsoft.Office.Interop.Word.Application();
Word.adoc文件;
单词。范围rng;
adoc=WordApp.Documents.Add(缺少引用,缺少引用,缺少引用,缺少引用);
rng=adoc.范围(参考起点1,参考缺失);
adoc.Tables.Add(rng,list.Count+2,4);
adoc.PageSetup.LeftMargin=20.0f;
adoc.PageSetup.RightMargin=20.0f;
adoc.Tables[1]。Columns[2]。Cells.PreferredWidth=60;
adoc.Tables[1]。Columns[1]。Cells.PreferredWidth=200;
adoc.Tables[1].单元格(1,1).Range.Text=“Symbol”;
adoc.Tables[1].单元格(1,2).Range.Text=“Qnt”;
adoc.Tables[1]。单元格(1,3)。Range.Text=“条形码”;
adoc.Tables[1].单元格(1,4).Range.Text=“条形码图像”;
adoc.Tables[1]。单元格(1,1)。Range.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft;
adoc.Tables[1]。单元格(1,1)。Range.Font.Bold=1;
adoc.Tables[1]。单元格(1,2)。Range.Font.Bold=1;
adoc.Tables[1]。单元格(1,3)。Range.Font.Bold=1;
adoc.Tables[1]。单元格(1,4)。Range.Font.Bold=1;
for(int i=0;i
假设您不介意将文档另存为DOCX,则可以使用中所述的。然后,您可以使用自动化将文档转换为DOC、PDF等格式。

根据您已经完成的工作量,您可以通过在Word运行时隐藏Word来大幅提高性能。花费的大部分时间可能是由于Word试图实时呈现您的更改(无论如何,这是我的经验)。在实例化
WordApp
后,在行中尝试此操作:

WordApp.Visible = false;

话虽如此,Office interop正在被逐步淘汰,因此的解决方案可能是一条可行之路。

thx,我将很快研究它。但是关于行和列的格式呢?太棒了:-)我们使用它生成报告,其中有数百个表,每个表包含数百行,我们从来没有遇到过性能问题。这里有一个指向SDK参考的链接,SDK提供了格式化行和列的低级方法。看看这个班。这是。记住标记回答您问题的答案,因为这有助于参与StackOverflow的每个人。ScreenUpdate=False在执行操作时可以设置为False,记住在保存之前再次将其设置为true。