我有一个带有书签的单词模板,并且我想在这个Word文档中插入很多人的信息,
所以我有一个Person类:
public class Person {
string name;
int age;
}
在另一堂课中,我有:
List<Person> personList; // 5 person objects for example
object fileName = "C:\\Users\\akenna\\template.docx";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
// create instance of Word
Application oWordApp = new Application();
// create instance of Word Document
Document oWordDoc = new Document();
// open word document
oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly,ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
在这里我想做一个迭代以显示所有具有personList [i]的人,而不是仅显示第一条记录的personList [0]
if (oWordDoc.Bookmarks.Exists("name"))
{
Object name = "name";
Range range = oWordDoc.Bookmarks.get_Item(ref name).Range;
range.Text = personList[0].name;
object newRange = range;
oWordDoc.Bookmarks.Add("name", ref newRange);
}
if (oWordDoc.Bookmarks.Exists("age"))
{
Object name = "age";
Range range = oWordDoc.Bookmarks.get_Item(ref name).Range;
range.Text = personList[0].ToString();
object newRange = range;
oWordDoc.Bookmarks.Add("age", ref newRange);
}
Word文档中的预期结果:
Jack 20 Mark 25 Isabelle 15 Mike 22 ...... dynamic size
答案 0 :(得分:0)
如所讨论的,在进一步的对话基础上,以下内容演示了如何遍历人员列表,打开模板,用Person类中的内容替换书签并将模板另存为新文件。
假设一个类似于以下内容的“人”类
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public List<string> Experience { get; set; }
public List<string> Skills { get; set; }
}
object fileName = "C:\\Users\\akenna\\template.docx";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
List<Person> personList = new List<Person>(); //Populate your list here
foreach (Person p in personList)
{
// create instance of Word
var applicationWord = new Microsoft.Office.Interop.Word.Application();
var wordDoc = applicationWord.Documents.Open(fileName);
//Supress word becoming visible
wordDoc.Application.Visible = false;
wordDoc.Application.WindowState = WdWindowState.wdWindowStateMinimize;
wordDoc.Activate();
//Loop through template bookmarks
foreach (Bookmark bm in wordDoc.Bookmarks)
{
switch (bm.Name)
{
case "Name":
bm.Range.Text = p.Name;
break;
case "Address":
bm.Range.Text = p.Address;
break;
case "Email":
bm.Range.Text = p.Email;
break;
case "Phone":
bm.Range.Text = p.PhoneNumber;
break;
case "Experiences":
var experienceText = "";
foreach (var exp in p.Experience)
{
experienceText = experienceText + exp + Environment.NewLine;
}
bm.Range.Text = experienceText;
break;
case "Skills":
var skillsText = "";
foreach (var skill in p.Skills)
{
skillsText = skillsText + skill + Environment.NewLine;
}
bm.Range.Text = skillsText;
break;
}
}
var pathToSave = "C:\\Your Path To Save\\";
var newFileName = p.Name + "_Resume.docx";
wordDoc.SaveAs(pathToSave + newFileName);
wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges); //Supresses any messages from word.
//You must do this to release the COM objects from memory!
applicationWord.Quit();
Marshal.ReleaseComObject(applicationWord);
}
希望现在有所帮助。