我想补充这三种类型的内容转换为Word文档。这就是我想现在就这样做。然而,每个项目取代最后一个。添加图像总是增加了页的开头。我有一个循环,调用一个函数来创建的标题和表,然后后面添加图像。我认为这个问题是范围。我使用的对象开始= 0的起始范围;
我怎样才能得到这些来一次添加一个到新行的文件?
的foreach(在observedColumns VAR类)
{
CreateHeadersAndTables();
createPictures();
}
添加页眉:
的对象开始= 0;
Word.Range RNG = doc.Range(REF开始,Missing.Value);
Word.Paragraph标题;
标题= doc.Content.Paragraphs.Add(Missing.Value);
heading.Range.Text =类别;
heading.Range.InsertParagraphAfter();
添加表:
Word.Table表;
表= doc.Content.Tables.Add(毫克,1,5);
添加图片:
doc.Application.Selection.InlineShapes.AddPicture(@path);
解决方案
一个简单的方法将使用段来处理范围
对象,并简单地通过插入一个新的第一段之一。
综观API文档显示,段落
实现了一个添加
方法,该方法:
返回重新presents一个新的空白段落段落对象 添加到文档。 (......)如果没有指定范围,则新的段落选择范围或后添加的或在文件末尾的
来源:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraphs.add(v=office.14).aspx
在这种方式,它会直截了当地追加新的内容到文档。
有关完整性我已经包含了一个示例,演示如何解决可能会奏效。该示例循环遍历一个为
循环,并为每个迭代它插入:
该样品是使用实现为一个C#控制台应用程序:
在.NET 4.5 的Microsoft Office对象库的版本15.0和 Microsoft Word对象库15.0版...也就是说,MS Word中互操作的API,船舶与MS Office 2013。
使用系统;
使用System.IO;
使用的Microsoft.Office.Interop.Word;
使用应用= Microsoft.Office.Interop.Word.Application;
命名空间StackOverflowWordInterop
{
类节目
{
静态无效的主要()
{
//打开Word和的docx文件
VAR wordApplication =新的应用程序(){可见=真};
VAR文件= wordApplication.Documents.Open(@C:用户 MyUserName输入文件 document.docx,可见:真正的);
//10的选择是通过随机 - 选择适合你的目的的值
为(变种I = 0; I&小于10;我+ +)
{
//插入文本
变种pText = document.Paragraphs.Add();
pText.Format.SpaceAfter = 10F;
pText.Range.Text =的String.Format(这是行#{0},我);
pText.Range.InsertParagraphAfter();
//插入表格
变种PTABLE = document.Paragraphs.Add();
pTable.Format.SpaceAfter = 10F;
变种表= document.Tables.Add(pTable.Range,2,3,WdDefaultTableBehavior.wdWord9TableBehavior);
对于(VAR R = 1;为r = table.Rows.Count; R ++)
对于(VAR C = 1; C< = table.Columns.Count; C ++)
table.Cell(R,C)= .Range.Text的String.Format(这是细胞{0}在表#{1},的String.Format(({0},{1})河, C),I);
//插入图片
变种pPicture = document.Paragraphs.Add();
pPicture.Format.SpaceAfter = 10F;
document.InlineShapes.AddPicture(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), img_1.png),范围:pPicture.Range);
}
//一些控制台ASCII-UI
Console.WriteLine(preSS任意键来保存文档并关闭Word ..);
到Console.ReadLine();
// 保存设置
document.Save();
//关闭字
wordApplication.Quit();
}
}
}
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();
}
Adding Headers:
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();
Adding Tables:
Word.Table table;
table = doc.Content.Tables.Add(rng, 1, 5);
Adding Pictures:
doc.Application.Selection.InlineShapes.AddPicture(@path);
解决方案
A simple approach will be using paragraphs to handle the Range
objects and simply insert a new paragraph one by one.
Looking at the API documentation reveals that Paragraphs
implements an Add
method which:
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.
Source: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraphs.add(v=office.14).aspx
In that way, it gets straight forward to append new content to the document.
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:
The sample has is implemented as a C# console application using:
.NET 4.5 Microsoft Office Object Library version 15.0, and Microsoft Word Object Library version 15.0... 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();
}
}
}