public struct PublicationInfor
{
public string village;
public string fullSite;
public string area;
public string deadLine;
public string publicationTime;
}
class NPOIWriteToWord
{
///<summary>
/// 测试村实测结果公示公告
/// </summary>
public static void WriteToPublicationOfResult()
{
FileStream fs = new FileStream(@"测试村实测结果公示公告.docx", FileMode.Open, FileAccess.Read);
NPOI.XWPF.UserModel.XWPFDocument myDocx = new NPOI.XWPF.UserModel.XWPFDocument(fs);//打开07(.docx)以上的版本的文档
PublicationInfor plcInfor = new PublicationInfor
{
village = "窝窝乡",
fullSite = "神圣兽国游尾郡窝窝乡",
area = "70.60",
deadLine = "2018年12月12日",
publicationTime = "2018年11月12日"
};
//遍历word中的段落
foreach (var para in myDocx.Paragraphs)
{
string oldtext = para.ParagraphText;
if (oldtext == "")
continue;
string temptext = para.ParagraphText;
//以下为替换文档模版中的关键字
if (temptext.Contains("{$village}"))
temptext = temptext.Replace("{$village}", plcInfor.village);
if (temptext.Contains("{$fullSite}"))
temptext = temptext.Replace("{$fullSite}", plcInfor.fullSite);
if (temptext.Contains("{$area}"))
temptext = temptext.Replace("{$area}", plcInfor.area);
if (temptext.Contains("{$deadLine}"))
temptext = temptext.Replace("{$deadLine}", plcInfor.deadLine);
if (temptext.Contains("{$publicationTime}"))
temptext = temptext.Replace("{$publicationTime}", plcInfor.publicationTime);
para.ReplaceText(oldtext, temptext);
}
FileStream output = new FileStream(@"测试村实测结果公示公告.docx", FileMode.Create);
myDocx.Write(output);
fs.Close();
fs.Dispose();
output.Close();
output.Dispose();
}
}
————————————————
版权声明:本文为CSDN博主「飞鸟与青蛙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/RicardoMTan/java/article/details/82707813