I have a little problem with C# word documents. I use Word.Interop library to create a .doc document. 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). It's too much time for me. I need something to create and save document in about 1 second or less.
Is there any way to create document faster than Word.Interop? Maybe template document? But how to fill a table with dynamic size from Template document?
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. You can then use automation to convert the document to DOC, PDF and so forth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With