word模板导出的几种方式:第二种:C#通过模板导出Word(文字,表格,图片) 占位符替换

时间:2021-10-04 09:04:30

原文出处:https://www.cnblogs.com/ilefei/p/3508463.html

一:模板的创建   (注意文件后缀只能是.docx或.doct)

在需要位置 插入-文档部件-域,

  域名:MacroButton
  宏名:DoFieldClick
  显示文字:这个自己设置,为了与模板其他文字区分,可以用"[]"括起来.
  需要多少替换内容,添加多少域即可.

word模板导出的几种方式:第二种:C#通过模板导出Word(文字,表格,图片) 占位符替换

word模板导出的几种方式:第二种:C#通过模板导出Word(文字,表格,图片) 占位符替换

二:添加项目                                 

  在解决方案中添加项目WordMLHelper,在原项目中添加对WordMLHelper的引用后可以直接调用.(见头部出处)

三:调用方法                      

  首先确定模板文件位置和导出文件的生成路径.

        private string mubanFile = "muban.docx";
private string outputPath = @"test1.docx";
  string templatePath = System.Web.HttpContext.Current.Request.MapPath(mubanFile );

  //mvc读取文件路径跟原文出处不一样

 List<TagInfo> tagInfos = wordMLHelper.GetAllTagInfo(System.IO.File.OpenRead(templatePath));//打开模板文件,获取所有填充域
for (int i = 0; i < tagInfos.Count; i++)
{
//填充域有两种类型,1:段落或图片,2:表格
//对填充域填充时需先判断填充域类型
if (tagInfos[i].Tbl == null)
{
if (string.Equals(tagInfos[i].TagTips.Trim(), "[NO]"))
{
TxtInfo txtInfo = new TxtInfo();
txtInfo.Content = info.NO;////////NO;--info实体类
txtInfo.ForeColor = "000000";
txtInfo.Size = 32;
txtInfo.HightLight = HighlightColor.None;
txtInfo.underlineStyle = UnderlineStyle.Single;
tagInfos[i].AddContent(txtInfo);
} }
else
{
TableStructureInfo tblInfo = tagInfos[i].Tbl;
if (tagInfos[i].Seq == 2)//整个表格创建
{
for (int j = 0; j < 3; j++)
{
RowStructureInfo row = new RowStructureInfo(); for (int k = 0; k < 3; k++)
{
CellStructureInfo cell = new CellStructureInfo();
TxtInfo txtInfo = new TxtInfo();
txtInfo.Content = "第" + (j + 1) + "行,第" + (k + 1) + "列";
txtInfo.Size = 25;
txtInfo.ForeColor = "0000ff";
cell.AddContentLine(txtInfo);
row.AddCell(cell);
}
tblInfo.AddRow(row);
}
} }
}
if (!string.IsNullOrEmpty(saveFile))
{
templatePath = System.Web.HttpContext.Current.Request.MapPath(tempFile);
wordMLHelper.GenerateWordDocument(System.IO.File.OpenRead(templatePath)
, System.Web.HttpContext.Current.Request.MapPath(saveFile)
, tagInfos); Assistance.RemoveAllTmpFile();// 删除所有临时文件
//Response.Redirect(Request.Url.AbsoluteUri);
}

  四、导出结果文档如果原来有下滑线,则在引用的项目中添加枚举类(在HighlightColor枚举下添加)

public enum UnderlineStyle
{
Single=0,
Words,
Double,
Thick,
Dotted,
DottedHeavy,
Dash,
DashedHeavy,
DashLong,
DashLongHeavy,
DotDash,
DashDotHeavy,
DotDotDash,
DashDotDotHeavy,
Wave,
WavyHeavy,
WavyDouble,
None,
}

  添加下滑线枚举变量(在TxtInfo类中)

   public UnderlineStyle underlineStyle = UnderlineStyle.None;

  在WordMLHelper类中的AssembleTxtRun方法中添加判断:

 if (txtInfo.underlineStyle!=UnderlineStyle.None)
{
Underline underline = new Underline();
underline.Val = (UnderlineValues)((int)txtInfo.underlineStyle);
rPr.AppendChild(underline);
}
txtRun.AppendChild(rPr);//在上面插入下滑线的判断

  其他内容参照原出处

对word已有表格更新操作:

if (tagInfos[i].Seq == 0)
{
TxtInfo txtInfo;
var cell2 = tblInfo.Rows[2].Cells[1];//通过表格索引修改,word文档不需要操作,注意:word中不能含相同名称否则会报索引大小错误
  ,同名可通过修改word中的同名文字然后用代码替换回来
txtInfo = new TxtInfo();
txtInfo.Content = newRow["RecordType1"].ToString();////////
cell121.AddContentLine(new TxtInfo() { Content = "" });
cell121.AddContentLine(new TxtInfo() { Content = "☑ A", Size = 24 });
cell121.AddContentLine(new TxtInfo() { Content = "☐ B", Size = 24 });
cell121.AddContentLine(new TxtInfo() { Content = "☐ C", Size = 24 }); } }