将数据添加到Word文档






2.62/5 (4投票s)
如何从 .net 向现有的 Word 文档添加数据
从 .net 向现有的 Word 文档添加表格/数据
如果您想知道如何向现有文档添加表格/数据,这里有一个小而简单的代码示例,展示了如何操作。
背景
使用代码
添加对 Interop.Office 和 Interop.Word 的引用
object missing = System.Reflection.Missing.Value; object fileName = _filename; object saveChanges = true; object newTemplate = false; object docType = 1; object isVisible = false; object confirmConversions = Type.Missing; object readOnly = false; object addToRecentFiles = Type.Missing; object passwordDoc = Type.Missing; object passwordTemplate = Type.Missing; object revert = Type.Missing; object writepwdoc = Type.Missing; object writepwTemplate = Type.Missing; object format = Type.Missing; object encoding = Type.Missing; object visible = true; object openRepair = Type.Missing; object docDirection = Type.Missing; object notEncoding = Type.Missing; object xmlTransform = Type.Missing; int row = -1; object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ Word._Application wordApp = null; Word._Document wordDoc = null; try { //Proceed only if there are some comments to be displayed if (_CommentResultsDS != null) { //exit if there are no rows in CommentsResult if (_CommentResultsDS.COMMENTS.Rows.Count == 0) return; wordApp = new Word.Application(); //wordApp.Version wordApp.Visible = false; //Open the doc wordDoc = wordApp.Documents.Open( ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc, ref writepwTemplate, ref format, ref encoding, ref visible); if (wordDoc != null) { //Create a table in word doc Word.Table oTable = null; Word.Range wrdRng = wordDoc.Bookmarks.Item(ref oEndOfDoc).Range; if (wordDoc.Tables.Count > 0) { oTable = wordDoc.Tables.Item(1); } if (oTable == null) { oTable = wordDoc.Tables.Add(wrdRng, _CommentResultsDS.COMMENTS.Rows.Count + 1, 3, ref missing, ref missing); oTable.Range.ParagraphFormat.SpaceAfter = 6; oTable.ID = TABLE_ID; row = 1; //Add the header row oTable.Cell(row, 1).Range.Text = "User Id"; oTable.Cell(row, 2).Range.Text = "Date Added"; oTable.Cell(row, 3).Range.Text = "Comments"; //Change the font and color of the header row oTable.Rows.Item(row).Range.Font.Bold = 1; oTable.Rows.Item(row).Range.Font.Size = 12; oTable.Rows.Item(row).Range.Font.Color = Word.WdColor.wdColorDarkBlue; oTable.Rows.Item(row).Range.Font.Name = TABLE_FONT_NAME; } else { row = oTable.Rows.Count; } //Don't show the border lines oTable.Borders.Enable = 1; foreach (CMPNCommentsDS.COMMENTSRow commentsRow in _CommentResultsDS.COMMENTS.Rows) { object beforeRow = Type.Missing; row += 1; oTable.Rows.Add(ref beforeRow); oTable.Cell(row, 1).Range.Text = commentsRow.ADD_USR_ID; oTable.Cell(row, 2).Range.Text = commentsRow.ADD_TMSTMP.ToShortDateString(); oTable.Cell(row, 3).Range.Text = commentsRow.COMMENTS; oTable.Rows.Item(row).Range.Font.Size = 10; oTable.Rows.Item(row).Range.Font.Color = Word.WdColor.wdColorBlue; oTable.Rows.Item(row).Range.Font.Name = TABLE_FONT_NAME; } } Remember to set the Language of your code snippet using the Language dropdown.