zoukankan      html  css  js  c++  java
  • 基于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 + " ";
    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 + " "; ;
    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);
    }

  • 相关阅读:
    SQL:分析函数、排名函数、聚合函数配合窗口函数 OVER 的用法
    SQL:MySQL 中各子句的执行逻辑过程
    SQL:CASE 表达式应用总结
    SQL:BLOB 和 TEXT
    SQL:char 和 varchar、binary 和 varbinary、二进制字符串、严格模式、汉字编码方式
    大数据:数据质量
    大数据:存储和成本管理
    大数据:计算管理
    jQuery.validate.js笔记
    Vistual Studio 条件编辑应用
  • 原文地址:https://www.cnblogs.com/SiSui/p/9506089.html
Copyright © 2011-2022 走看看