• 基于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

  • 相关阅读:
    三级菜单打怪升级,young -> plus -> pro
    Python注释是什么东东
    腾讯云中的mysql镜像数据定时同步到本机数据库
    linux重复命令的简洁化
    快速查询mysql中每个表的数据量
    MGR与MHA
    mysql基础练习
    mongo日常操作备忘
    MongoDB:删除操作
    MongoDB插入数据的3种方法
  • 原文地址:https://www.cnblogs.com/SiSui/p/9506059.html
Copyright © 2020-2023  润新知