我创建了一个简单的单词add,在单词document中插入一些文本,
using Microsoft.Office.Interop.Word
我想将我的自定义字段添加到C#模型的插入字段中 例子
public class test (){
public int id {get;set;}
public int Name{get;set;}
}
public string ExtractText()
{
string filename = RandomString(10);
#region Create copy of doc
var app = new Microsoft.Office.Interop.Word.Application();
//save document to the same location with different name
var path2 = System.IO.Path.Combine(
Directory.GetCurrentDirectory(), "Documents", filename.ToString().Replace(".docx", "") + "V2" + ".docx");
System.IO.File.Create(path2).Close();
Document document = app.Documents.Open(path2);
#endregion
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < document.Paragraphs.Count; i++)
{
string temp = document.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty)
data.Add(temp);
}
#region Add Paragraphs on backend
// ********************************************************************************
//----Add Paragraphs on backend -----
//********************************************************************************
data.Add("Next LINE");
data.Add("Next LINE1");
data.Add("Second method opens the existing Microsoft Office Word document specified by a fully qualified path and file name. This method returns a Document that represents the opened document");
data.Add("Contrary <Department> to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney C");
#endregion
object missing = System.Reflection.Missing.Value;
//Enable modification
app.ActiveDocument.Content.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);
//List<int> paragraphsPermission = new List< int>();
// List<Tuple<int, string>> paragraphsPermission = new List<Tuple<int, string>>();
List<int> paragraphsPermission = new List<int>();
//Add text to doc
foreach (var item in data)
{
var para = document.Content.Paragraphs.Add(ref missing);
document.MailMerge.Fields.Add(para.Range ,"Name");
para.Range.Text = item.Trim();
para.Range.InsertParagraphAfter();
#region Remove this after implementation
if (item.Contains("Second"))
{
paragraphsPermission.Add(1);
}
else if (item.Contains("Next LINE1"))
{
paragraphsPermission.Add(1);
}
else
{
paragraphsPermission.Add(0);
}
#endregion
}
#region Apply enforceStyle
app.Visible = true;
object noReset = true;
object password = System.String.Empty;
object useIRM = false;
object enforceStyleLock = true;
object PasswordEncryptionFileProperties = false;
app.ActiveDocument.EnforceStyle = true;
document.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref noReset, "000", ref useIRM, ref enforceStyleLock);
int nextparagraphs = 1;
foreach (var item in paragraphsPermission)
{
if (item == 1)
document.Paragraphs[nextparagraphs].Range.Editors.Add(WdEditorType.wdEditorEditors);
else
document.Paragraphs[nextparagraphs].Range.Editors.Add(WdEditorType.wdEditorEveryone);
nextparagraphs++;
}
document.Save();
document.Close();
因此,当用户单击“邮件合并”选项卡中提交的插入内容时,应该向他显示 ID和名称
因此ID和名称应显示在插入合并字段
谢谢