zoukankan      html  css  js  c++  java
  • 基于Microsoft.Office.Interop.Word的替换文字(超出字符处理)

    #region 优化报告生成
    private void button1_Click(object sender, EventArgs e)
    {
    if (AnalysisCellNamecomboBox.Text != "" && textBox27.Text.Length != 0)
    {

    MessageBox.Show("开始生成优化报告,请稍等..." + S.Length.ToString());

    Object Nothing = Missing.Value; //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
    object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
    object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
    string ACellNameChecked = "";
    object InSysInterAnalyReportWordSavePath;
    MSWord.Application InSysInterAnalyReportWordApp;//Word应用程序变量初始化
    MSWord.Document InSysInterAnalyReportWordDoc;

    ACellNameChecked = InvalidCellNameCharsRemoveForFilePath(AnalysisCellNamecomboBox.Text);
    if (!Directory.Exists(InterferenceAnalysisResultSavePath + "\各小区优化分析报告(Word)"))
    Directory.CreateDirectory(InterferenceAnalysisResultSavePath + "\各小区优化分析报告(Word)");
    InSysInterAnalyReportWordSavePath = InterferenceAnalysisResultSavePath + "\各小区优化分析报告(Word)" + "\" + ACellNameChecked + "网内干扰分析报告1.docx";

    string physicNewFile = @"C:UsersHYYXDesktop上行20180608模板深圳正大康F-HLH-2网内干扰分析报告.docx";

    InSysInterAnalyReportWordApp = new MSWord.Application();//创建word应用程序

    object fileName = (physicNewFile);//模板文件
    //打开模板文件

    InSysInterAnalyReportWordDoc = InSysInterAnalyReportWordApp.Documents.Open(ref fileName,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

    object replace = MSWord.WdReplace.wdReplaceAll;

    InSysInterAnalyReportWordApp.Selection.Find.Replacement.ClearFormatting();
    InSysInterAnalyReportWordApp.Selection.Find.MatchWholeWord = true;
    InSysInterAnalyReportWordApp.Selection.Find.ClearFormatting();

    object FindText = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:(待补充)";//需要被替换的文本
    //InSysInterAnalyReportWordApp.Selection.Find.Text = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:(待补充)";//需要被替换的文本
    object Replacement = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:" + " " + textBox27.Text.Replace(" ", " ").Replace(" ", "").Replace("受扰小区", " 受扰小区").Replace("干扰源邻区1", " 干扰源邻区1").Replace("干扰源邻区2", " 干扰源邻区2").Replace("干扰源邻区3", " 干扰源邻区3");//替换文本
    //InSysInterAnalyReportWordApp.Selection.Find.Replacement.Text = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:" + " " + textBox27.Text.Replace(" ","");//替换文本

    if (Replacement.ToString().Length > 110)
    FindAndReplaceLong(InSysInterAnalyReportWordApp, FindText, Replacement);
    else FindAndReplace(InSysInterAnalyReportWordApp, FindText, Replacement);

    //对替换好的word模板另存为一个新的word文档
    InSysInterAnalyReportWordDoc.SaveAs(InSysInterAnalyReportWordSavePath,
    Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing,
    Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
    //关闭wordDoc文档
    InSysInterAnalyReportWordApp.Documents.Close(ref Nothing, ref Nothing, ref Nothing);
    //关闭wordApp组件对象
    InSysInterAnalyReportWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);


    MessageBox.Show("优化报告生成成功");
    }
    }

    public static void FindAndReplaceLong(MSWord.Application wordApp, object findText, object replaceText)
    {
    int len = replaceText.ToString().Length; //要替换的文字长度
    int cnt = len / 110; //不超过220个字
    string newstr;
    object newStrs;
    if (len < 110) //小于220字直接替换
    {
    FindAndReplace(wordApp, findText, replaceText);
    }
    else
    {
    for (int i = 0; i <= cnt; i++)
    {
    if (i != cnt)
    newstr = replaceText.ToString().Substring(i * 110, 110) + findText; //新的替换字符串
    else
    newstr = replaceText.ToString().Substring(i * 110, len - i * 110); //最后一段需要替换的文字
    newStrs = (object)newstr;
    FindAndReplace(wordApp, findText, newStrs); //进行替换
    }
    }
    }

    public static void FindAndReplace(MSWord.Application wordApp, object findText, object replaceText)
    {
    object matchCase = true;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards,
    ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText,
    ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }
    #endregion

  • 相关阅读:
    git 从创建到推送到远程,到拉取,实操
    《React后台管理系统实战 :三》header组件:页面排版、天气请求接口及页面调用、时间格式化及使用定时器、退出函数
    《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件
    《React后台管理系统实战 :二》antd左导航:cmd批量创建子/目录、用antd进行页面布局、分离左导航为单独组件、子路由、动态写左导航、css样式相对陷阱
    《React后台管理系统实战 :四》产品分类管理页:添加产品分类、修改(更新)产品分类
    go的变量与常量
    Go 语言最简单程序的结构
    go的安装与测试
    java
    go语言
  • 原文地址:https://www.cnblogs.com/SiSui/p/9506059.html
Copyright © 2011-2022 走看看