有一些产品要写入Word文件中的表中。模板包含第1页上的目录和模板表。在第一页写入表的速度相对较快,但从第二页开始,每个单元格的写入时间几乎是第一页的两倍。有什么办法加快速度吗?
using Word = Microsoft.Office.Interop.Word;
public static void CreateWordDocument(Catalogue pCatalogue, Word._Document oWDFile, string pPicturePath, string pStorePath, Word.Application oWD) {
var wordSelection = oWD.Selection;
var oMissing = (object)Missing.Value;
var wordTableCount = oWDFile.Tables.Count;
if (wordTableCount > 0) {
var tableToUse = oWDFile.Tables[wordTableCount];
var range = tableToUse.Range;
range.Copy();
foreach (var currentChapter in pCatalogue.ChapterList) {
//Do Stuff
var groupCounter = 1;
var articleCounter = 0;
foreach (var currentGroup in currentChapter.ArticleList) {
//Get the current row depending on the current articlegroup and existing tables
tableToUse.Cell(HelpCollection.GetWordTableRow(groupCounter, true, wordTableCount), 2).Range.Text = currentGroup[0].ID;
//Do Stuff
if (groupCounter > 5) {
wordSelection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove);
Word.Table tableCopy = oWDFile.Tables.Add(wordSelection.Range, 1, 1, ref oMissing, oMissing);
tableCopy.Range.Paste();
articleCounter = 0;
groupCounter = 1;
wordTableCount = oWDFile.Tables.Count;
}
}
}
}
try {
oWDFile.SaveAs2(pStorePath + @"" + pCatalogue.CatalogueName + ".docx", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
} catch (Exception ex) {
//Message
}
}
我没有直接追加表格,而是创建了一个新段落。这似乎有很大的不同,因为它解决了问题。
wordSelection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove);
object breakType = Word.WdBreakType.wdPageBreak;
Word.Selection.InsertBreak(ref breakType);
Word.Paragraph tableCopy = oWDFile.Paragraphs.Add(oWD.Selection.Range);
tableCopy.Range.Paste();