该技巧用于已有表格的填写。
比如说:
在要填写的地方加上书签。
数据读入代码:
Document dutyDoc = new Document(@"./xxxx.docx"); dutyDoc.Range.Bookmarks["ProjectName"].Text = moduleName;//书签名字,英文。 dutyDoc.Range.Bookmarks["ProjectName1"].Text = moduleName;//书签名字,英文。 dutyDoc.Range.Bookmarks["ProjectName2"].Text = moduleName; dutyDoc.Range.Bookmarks["SafeCode"].Text = safetyManagerment.SafeCode; dutyDoc.Range.Bookmarks["DutyManagement"].Text = safetyManagerment.DutyManagement; dutyDoc.Range.Bookmarks["IllegaTime"].Text = safetyManagerment.IllegaTime.ToString("yyyy/MM/dd"); dutyDoc.Range.Bookmarks["Place"].Text = safetyManagerment.Place; dutyDoc.Range.Bookmarks["ProjectStepName"].Text = safetyManagerment.ProjectStepName; dutyDoc.Range.Bookmarks["IllegalPerson"].Text = safetyManagerment.IllegalPerson; dutyDoc.Range.Bookmarks["Behavior"].Text = safetyManagerment.Behavior; dutyDoc.Range.Bookmarks["IllegaType"].Text = safetyManagerment.IllegaType; dutyDoc.Range.Bookmarks["IllegaType1"].Text = safetyManagerment.IllegaType; dutyDoc.Range.Bookmarks.Clear(); SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "(Word文档)*.docx,*.doc|*.docx;*.doc"; dialog.Title = "导出Word"; dialog.FileName = "(" + "考核通知单" + ")" + DateTime.Now.ToString("yyyyMMddHHmm") + ".docx"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string fileName = dialog.FileName; dutyDoc.Save(fileName); }