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

原文地址:https://www.cnblogs.com/SiSui/p/9506089.html

时间: 2024-07-29 23:58:24

基于Microsoft.Office.Interop.Word的导出的相关文章

基于Microsoft.Office.Interop.Word的替换文字

using MSWord = Microsoft.Office.Interop.Word; MSWord.Application app = null; MSWord.Document doc = null; //将要导出的新word文件名 string physicNewFile = @"C:\Users\HYYX\Desktop\上行20180608\模板\深圳正大康F-HLH-2网内干扰分析报告.docx"; app = new MSWord.Application();//创建

无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。

无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”.此操作失败的原因是对 IID 为“{00020970-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错. (异常来自 HRESULT:0x80029C4A

VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE

在使用vs2005. vs2008. vs2010 制作包含 word等office的应用程序时,有时找不到对Microsoft.Office.Core. Microsoft.Office.Interop.Word和VBIDE 三个文件的引用,此时,如果已经安装了office 2003,那么可能是选择“典型安装”或者是“最小化安装”,那需要从“控制面板”中将其删除,然后再次安装,并选择“完全安装”,安装之后再使用以下两 种方法之一即可. 在选择“完全安装”office 2003 后,使用以下两种

2015.5.21 VS2010中引用Word组件后提示 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数 解决方法

wordApp = new Word.ApplicationClass();//这句在VS2005中没问题,在2010中会报错. 解决方法:在资源管理器 "引用"项的"Microsoft.Office.Interop.Word" 上点击鼠标右键,选择"属性",将属性中的"嵌入互操作类型"的值改为"false"即可.  这项在vs2005中没有

System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。

报错:System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”.此操作失败的原因是对 IID 为“{00020970-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/D

Microsoft.Office.Interop.Word 创建word

Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746.html 功能总结或者完善. 一.添加页眉 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System

错误类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数

原文网址:http://zhidao.baidu.com/link?url=WcvaYFI1JeEGvbjD77nDbGp21sjaNCnCTRLGrU5YjwUGbHbhHJxQolKbsMZbZsLr8R5K0IGGKj8IbPkERucU7NThRXcbIdq5hl90nWdtLYe 解决办法:解决方案资源管理器 -> 引用 -> "Microsoft.Office.Interop.Word" -> 右键选择属性 -> 嵌入互操作类型的值改为"f

类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数

错误 4317 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”.请改用适用的接口. 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数 解决办法: 在Visual Studio 中点击菜单项“视图->解决方案资源管理器”,在其中点开“引用”文件夹,在"Microsoft.Office.Interop.Word" 上点击鼠标右键,选择“属性”,将属性中

Could not load file or assembly 'Microsoft.Office.Interop.Word, Version=14.0

参考地址一:点击这里 参考地址二:点击这里 解决方法: 使用office COM元件,电脑里必须要有相对应的版本,比如 Excel 14.0是对应Excel 2010 Excel 12.0是对应Excel 2007 Excel 11.0是对应Excel 2003 使用Excel COM必须要注意版本问题,并非用最高版本就能操作低版本. Could not load file or assembly 'Microsoft.Office.Interop.Word, Version=14.0