基于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:\Users\HYYX\Desktop\上行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网内干扰优化方案制定如下:" + "\r\n" + textBox27.Text.Replace("\r\n\r\n", "\r\n").Replace("\r\n\t", "").Replace("受扰小区", "\t受扰小区").Replace("干扰源邻区1", "\t干扰源邻区1").Replace("干扰源邻区2", "\t干扰源邻区2").Replace("干扰源邻区3", "\t干扰源邻区3");//替换文本
//InSysInterAnalyReportWordApp.Selection.Find.Replacement.Text = "根据干扰源定位分析结果,深圳正大康F-HLH-2网内干扰优化方案制定如下:" + "\r\n\t" + textBox27.Text.Replace("\r\n\t","");//替换文本

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

posted @ 2018-08-20 16:00  四岁  阅读(1637)  评论(2编辑  收藏  举报