基於Microsoft.Office.Interop.Word的導出


public void WriteWord()
{
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;
InSysInterAnalyReportWordApp = new MSWord.Application();//Word應用程序變量初始化
InSysInterAnalyReportWordDoc = InSysInterAnalyReportWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //新建一個word文檔對象

 

string ConfidenceLevelResulInfo = "";// 優化方案信息文字
//表格
MSWord.Table AcellnameCellInfoTable;
MSWord.Table NcellnameCellInfoTable1;
MSWord.Table NcellnameCellInfoTable2;
MSWord.Table NcellnameCellInfoTable3;

ACellNameChecked = InvalidCellNameCharsRemoveForFilePath(AnalysisCellNamecomboBox.Text);
if (!Directory.Exists(InterferenceAnalysisResultSavePath + "\\各小區優化方案詳細信息"))
Directory.CreateDirectory(InterferenceAnalysisResultSavePath + "\\各小區優化方案詳細信息");
InSysInterAnalyReportWordSavePath = InterferenceAnalysisResultSavePath + "\\各小區優化方案詳細信息" + "\\" + ACellNameChecked + "各小區優化方案詳細信息.docx";

//整個文檔的文字設置(Selection表示當前選擇集,如果當前沒有選擇對像,則指對光標所在處進行設置)
InSysInterAnalyReportWordApp.Selection.Font.Name = "宋體";//字體設置
InSysInterAnalyReportWordApp.Selection.Font.Bold = 0; //不加粗
InSysInterAnalyReportWordApp.Selection.Font.Color = MSWord.WdColor.wdColorBlack; //字體顏色設置
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpace1pt5; //1.5倍行間距

//標題生成
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; //居中對齊
InSysInterAnalyReportWordApp.Selection.Font.Size = 16f;//三號字體
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.FirstLineIndent = 0; //首行無縮進
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Text = AnalysisCellNamecomboBox.Text + "\n";
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Select(); //光標移至文末

#region 受擾小區名稱生成
InSysInterAnalyReportWordDoc.Paragraphs.Last.OutlineLevel = MSWord.WdOutlineLevel.wdOutlineLevelBodyText;
InSysInterAnalyReportWordApp.Selection.Font.Size = 10.5f;//五號字體
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.FirstLineIndent = 0; //首行縮進2字符
ConfidenceLevelResulInfo = OptimizeDictionaryAl[AnalysisCellNamecomboBox.SelectedItem.ToString()].Keys.ToList()[0];// 優化方案信息文字
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Text = "受擾小區:" + ConfidenceLevelResulInfo + "\n"; ;
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Select(); //光標移至文末


InSysInterAnalyReportWordApp.Selection.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle; //單倍行間距
InSysInterAnalyReportWordApp.Selection.Font.Size = 7.5f;//7.5字體
InSysInterAnalyReportWordApp.Selection.EndKey(ref unite, ref Nothing); //光標移至文末
AcellnameCellInfoTable = InSysInterAnalyReportWordDoc.Tables.Add(InSysInterAnalyReportWordApp.Selection.Range, 4, 3);
AcellnameCellInfoTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
AcellnameCellInfoTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
AcellnameCellInfoTable.Select();//選中表格
InSysInterAnalyReportWordApp.Selection.Tables[1].Rows.Alignment = Microsoft.Office.Interop.Word.WdRowAlignment.wdAlignRowCenter;//表格居中
AcellnameCellInfoTable.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
//單元格信息填充
AcellnameCellInfoTable.Cell(1, 1).Range.Text = checkBox1.Text;
AcellnameCellInfoTable.Cell(1, 2).Range.Text = textBox3.Text;
AcellnameCellInfoTable.Cell(1, 3).Range.Text = textBox21.Text;
AcellnameCellInfoTable.Cell(2, 1).Range.Text = checkBox2.Text;
AcellnameCellInfoTable.Cell(2, 2).Range.Text = textBox4.Text;
AcellnameCellInfoTable.Cell(2, 3).Range.Text = textBox20.Text;
AcellnameCellInfoTable.Cell(3, 1).Range.Text = checkBox3.Text;
AcellnameCellInfoTable.Cell(3, 2).Range.Text = textBox5.Text;
AcellnameCellInfoTable.Cell(3, 3).Range.Text = textBox19.Text;
AcellnameCellInfoTable.Cell(4, 1).Range.Text = checkBox6.Text;
AcellnameCellInfoTable.Cell(4, 2).Range.Text = textBox10.Text;
AcellnameCellInfoTable.Cell(4, 3).Range.Text = textBox18.Text;
InSysInterAnalyReportWordApp.Selection.EndKey(ref unite, ref Nothing); //光標移至文末
#endregion

//將wordDoc 文檔對象的內容保存為DOC 文檔,並保存到path指定的路徑
InSysInterAnalyReportWordDoc.SaveAs(ref InSysInterAnalyReportWordSavePath, ref format, 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);
//關閉wordDoc文檔
InSysInterAnalyReportWordApp.Documents.Close(ref Nothing, ref Nothing, ref Nothing);
//關閉wordApp組件對象
InSysInterAnalyReportWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM