如何使用 word interop 一次将一项添加到 word 文档的新行 [英] How to add items one at a time to to a new line a word document using word interop
问题描述
我正在尝试将这三种类型的内容添加到 Word 文档中.这就是我现在尝试这样做的方式.但是,每一项都会替换最后一项.添加图像总是添加到页面的开头.我有一个循环,它调用一个函数来创建标题和表格,然后在之后添加图像.我认为问题是范围.我使用对象 start = 0 的起始范围;
I am trying to add these three types of content into a word doc. This is how I am trying to do it now. However, each item replaces the last one. Adding images always adds to the beginning of the page. I have a loop that calls a function to create the headers and tables, and then adds images after. I think the problem is ranges. I use a starting range of object start = 0;
如何让这些文件一次添加一个到文档中的新行?
How can I get these to add one at a time to to a new line in the document?
foreach (var category in observedColumns)
{
CreateHeadersAndTables();
createPictures();
}
添加标题:
object start = 0;
Word.Range rng = doc.Range(ref start , Missing.Value);
Word.Paragraph heading;
heading = doc.Content.Paragraphs.Add(Missing.Value);
heading.Range.Text = category;
heading.Range.InsertParagraphAfter();
添加表格:
Word.Table table;
table = doc.Content.Tables.Add(rng, 1, 5);
添加图片:
doc.Application.Selection.InlineShapes.AddPicture(@path);
推荐答案
一个简单的方法是使用段落来处理 Range
对象并简单地一个一个地插入一个新段落.
A simple approach will be using paragraphs to handle the Range
objects and simply insert a new paragraph one by one.
查看 API 文档发现 Paragraphs
实现了一个 Add
方法,该方法:
Looking at the API documentation reveals that Paragraphs
implements an Add
method which:
返回一个代表新的空白段落的 Paragraph 对象添加到文档中.(...) 如果未指定范围,则新段落将添加到所选内容或范围之后或文档末尾.
Returns a Paragraph object that represents a new, blank paragraph added to a document. (...) If Range isn't specified, the new paragraph is added after the selection or range or at the end of the document.
通过这种方式,可以直接将新内容附加到文档中.
In that way, it gets straight forward to append new content to the document.
为了完整起见,我提供了一个示例,展示了解决方案的工作原理.示例通过 for
循环进行循环,并为每次迭代插入:
For completeness I have included a sample that shows how a solution might work. The sample loops through a for
loop, and for each iteration it inserts:
- 新的一行文本
- 一张桌子
- 一张照片
该示例使用以下方法实现为 C# 控制台应用程序:
The sample has is implemented as a C# console application using:
- .NET 4.5
- Microsoft Office 对象库 15.0 版和
- Microsoft Word 对象库 15.0 版
...即 MS Office 2013 附带的 MS Word Interop API.
... that is, the MS Word Interop API that ships with MS Office 2013.
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
namespace StackOverflowWordInterop
{
class Program
{
static void Main()
{
// Open word and a docx file
var wordApplication = new Application() { Visible = true };
var document = wordApplication.Documents.Open(@"C:UsersmyUserNameDocumentsdocument.docx", Visible: true);
// "10" is chosen by random - select a value that fits your purpose
for (var i = 0; i < 10; i++)
{
// Insert text
var pText = document.Paragraphs.Add();
pText.Format.SpaceAfter = 10f;
pText.Range.Text = String.Format("This is line #{0}", i);
pText.Range.InsertParagraphAfter();
// Insert table
var pTable = document.Paragraphs.Add();
pTable.Format.SpaceAfter = 10f;
var table = document.Tables.Add(pTable.Range, 2, 3, WdDefaultTableBehavior.wdWord9TableBehavior);
for (var r = 1; r <= table.Rows.Count; r++)
for (var c = 1; c <= table.Columns.Count; c++)
table.Cell(r, c).Range.Text = String.Format("This is cell {0} in table #{1}", String.Format("({0},{1})", r,c) , i);
// Insert picture
var pPicture = document.Paragraphs.Add();
pPicture.Format.SpaceAfter = 10f;
document.InlineShapes.AddPicture(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "img_1.png"), Range: pPicture.Range);
}
// Some console ascii-UI
Console.WriteLine("Press any key to save document and close word..");
Console.ReadLine();
// Save settings
document.Save();
// Close word
wordApplication.Quit();
}
}
}
这篇关于如何使用 word interop 一次将一项添加到 word 文档的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!