#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
