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);
    }

  • 相关阅读:
    Linux strace命令使用详解
    Ubuntu 给应用程序添加桌面图标(本文以Eclipse为例)
    Linux(Ubuntu/Debian/CentOS/RedHat)下交叉编译boost库
    Ubuntu下安装RabbbitVCS(图形化svn管理工具)- Ubuntu也有TortoiseSVN
    Linux下编译C++程序遇到错误:undefined reference to `*::*
    Linux系统(Ubuntu/Debian/RedHat/CentOS)超级简单的samba配置文件smb.conf
    Makefile常用万能模板(包括静态链接库、动态链接库、可执行文件)
    Ubuntu/Debian交叉编译安装ARM平台版本的ffmpeg
    apue编程之参考du代码利用递归写的一个简单的du命令的源代码
    linux 编译kernel与svn版本冲突解决方法 [drivers/gpu/mali/mali/common/mali_kernel_core.o] 错误 1
  • 原文地址:https://www.cnblogs.com/SiSui/p/9506089.html
Copyright © 2011-2022 走看看