zoukankan      html  css  js  c++  java
  • C#+NPOI动态库 对Word的简单编辑

    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

  • 相关阅读:
    Codeforces Round #443 (Div. 2)ABC
    Codeforces Round #442 (Div. 2) ABC
    Wannafly挑战赛2 Cut
    热爱工作的蒜蒜
    Codeforces Round #441 (Div. 2) (ABC)
    Codeforces Round #440 (Div. 2)(ABC)
    Codeforces Round #439 (Div. 2)(ABC)
    sphinx 分词搭建手册
    [转]Chrome 控制台console的用法
    shell脚本复制文件夹内容到另外的文件夹,如果存在则自动备份
  • 原文地址:https://www.cnblogs.com/shuaiheng/p/13071686.html
Copyright © 2011-2022 走看看