安全资格远程培训平台(安全培训管理系统)
最近遇到一个就是对应word 模板 进行赋值然后进行保存。
用C# sql2008R2 进行开发,进行编辑。
以下代码
string templatePath = "1.docx";
Document doc = new Document(templatePath);
DocumentBuilder builder = new DocumentBuilder(doc);
string[] fieldsNames = new string[] { "Safe_Name", "Card_No", "Person_Year", "Person_Month", "Person_Day" };
object[] fieldValues = new object[] { "国仁", "3212251850329752323", "2022", "03", "27" };
doc.MailMerge.Execute(fieldsNames, fieldValues);
//添加复选框
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("NAMETwo", "163");
dic.Add("NAME", "82");
//创建table
DataTable table = new DataTable("Table");
table.Columns.Add("TypeName");
table.Columns.Add("XH");
table.Columns.Add("TrainLocation");
table.Columns.Add("TrainTime");
table.Columns.Add("TrainContent");
table.Columns.Add("Times");
table.Columns.Add("TrainTeachers");
table.Columns.Add("CheckSorce");
table.Columns.Add("DoubleSorce");
table.Columns.Add("MineRemark");
table.Rows.Add(new object[] { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" });
table.Rows.Add(new object[] { "2", "2", "2", "2", "2", "2", "2", "2", "2", "2" });
table.Rows.Add(new object[] { "3", "3", "3", "3", "3", "3", "3", "3", "3", "3" });
doc.MailMerge.ExecuteWithRegions(table);
foreach (Bookmark mark in doc.Range.Bookmarks)
{
if (mark != null)
{
switch (mark.Name)
{
case "NAME":
mark.Text = "龚辉";
builder.MoveToBookmark(mark.Name);
builder.Font.Name = "Wingdings 2";
foreach(var item in dic)
{
builder.Write(mark.Text char.ConvertFromUtf32(int.Parse(item.Value.ToString())));
}
break;
case "Document_Image":
string imgPath = "1.png";
if (File.Exists(imgPath))
{
builder.MoveToBookmark("Document_Image");
builder.InsertImage(imgPath);
}
break;
default:
break;
}
}
}
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com