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

  • 相关阅读:
    【引用】webkit内核浏览器支持的特殊CSS样式和CSS3.0
    catalina.home catalina.base 定义 位子 位置
    gvim 启动 全屏
    Log4j 配置文件(log4j.properties)的所在路径问题(转)
    ie6 div height bug css注意点(转)
    【引用】jdbc.properties 包含多种数据库驱动链接版本
    【引用】ActionContext和ServletActionContext介绍
    【引用】在Eclipse中将java Project转换成Dynamic Web Project
    flex查询xml字段绑定DataGrid小结
    Jquery 每天记一点200972
  • 原文地址:https://www.cnblogs.com/SiSui/p/9506059.html
Copyright © 2020-2023  润新知