简体   繁体   English

使用Interop在C#中创建Word文档的更快捷方式

[英]Faster way to create Word document in C# using Interop

I have a little problem with C# word documents. 我对C#word文档有点问题。 I use Word.Interop library to create a .doc document. 我使用Word.Interop库来创建.doc文档。 I put into this document a dynamic table, which has some text and picture in last column My problem is that saving/generating document is really slow (ab. 4 sec for only 30 rows in table, 7 sec for 54 row). 我在这个文档中加入了一个动态表,在最后一栏中有一些文本和图片我的问题是保存/生成文档非常慢(表格中只有30行,54行时为7秒,为4秒)。 It's too much time for me. 这对我来说太多了。 I need something to create and save document in about 1 second or less. 我需要在大约1秒或更短的时间内创建和保存文档。

Is there any way to create document faster than Word.Interop? 有没有办法比Word.Interop更快地创建文档? Maybe template document? 也许模板文件? But how to fill a table with dynamic size from Template document? 但是如何从Template文档填充动态大小的表?

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);
}

Assuming you don't mind saving the document as DOCX, you can use the Open XML SDK 2.0 as described in this article . 假设你不介意将文档保存为DOCX,您可以使用开放XML SDK 2.0中所述这篇文章 You can then use automation to convert the document to DOC, PDF and so forth. 然后,您可以使用自动化将文档转换为DOC,PDF等。

Depending on how much work you've done already, you can probably improve performance substantially by hiding Word while it's running. 根据您已经完成的工作量,您可以通过在Word运行时隐藏Word来大幅提高性能。 Most of the time taken is probably due to Word trying to render your changes in realtime (that's my experience, anyway). 大部分时间可能是由于Word试图实时渲染您的更改(这是我的经验,无论如何)。 Try this in the line after you instantiate WordApp : 在实例化WordApp之后尝试使用此行:

WordApp.Visible = false;

That having been said, Office interop is being phased out, so Werner Strydom 's solution is probably the way to go. 话虽如此,Office互操作正在逐步淘汰,因此Werner Strydom的解决方案可能还有很长的路要走。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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