关于VS兼容WPS和OFFICE的问题
最近一直在做的开发是关于导出word的功能,一开始的做法是在VS中直接添加引用office PIA,Microsoft.Office.Interop.Word,VS08有两个版本,V11和V12,V11对应的是office03,V12对应的office07,试验之后得出结论,这两个PIA的引用只会影响开发机器的使用,就是说要与开发机器的office版本相对应。在目标机器上都是可以使用的,没有问题。
接下来就是解决wps和office兼容的问题了,目标机器上有三种情况,一是安装了WPS,二是安装了office ,三是同时安装了office和wps。估计第三种也就是我这个开发人员会这么干!!为了兼容性,需要这么干,把office的PIA-->> Microsoft.Office.Interop.Word添加引用 把wps 的V9版PIA--->>Kingsoft.Office.Interop.Wpsapi添加引用,接下来在代码中直接用wps的方法创建word 并执行所有操作。OK !在这种情况下,当目标机器只安装了offcie时,由于V9版本的兼容性会直接生成word。为了可以兼容word03.我在代码中也做了一些其他的操作,可以参考。
private void ExportToWps(){try{string strFileName = label14.Text + "-" + label15.Text;string saveFileName = "";SaveFileDialog saveDialog = new SaveFileDialog();saveDialog.DefaultExt = "doc";saveDialog.Filter = "Word文件|*.doc";saveDialog.FileName = strFileName;saveDialog.ShowDialog();saveFileName = saveDialog.FileName;if (saveFileName.IndexOf(":") < 0) return; //被点了取消 // Word.ApplicationClass oWordApp = new Word.ApplicationClass();//建立Word 对象,启动word程序 Word.Application oWordApp = new Word.Application();if (oWordApp == null){MessageBox.Show("word版本错误!", "error");return;}object missing = System.Reflection.Missing.Value;object oTemplate = System.Windows.Forms.Application.StartupPath + "\\Normal.dot";Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);//新建word文档 oWordApp.Visible = false;//设置Word程序可见,如果为false 那么word不可见 //页面设置 oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f); //上 oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下 oWordDoc.PageSetup.LeftMargin = oWordApp.CentimetersToPoints(2.2f);//左 oWordDoc.PageSetup.RightMargin = oWordApp.CentimetersToPoints(2.2f);//右 添加页眉 林总不需要//oWordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; //激活页眉的编辑 //oWordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; //设置对齐方式 //string headtext1 =PcaSettings.GetSettingString (101);//oWordApp.Selection.Font.Name = "宋体"; //设置字体 //oWordApp.Selection.Font.Size = 10.5f;//oWordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;//oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone; //bu添加下划线 //oWordApp.Selection.TypeText(headtext1);//oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;//添加页脚 string foottext1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter; //激活页脚的编辑 oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;oWordApp.Selection.Font.Name = "仿宋_GB2312";oWordApp.Selection.Font.Size = 8;oWordApp.Selection.TypeText(foottext1);//添加正文 oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑 oWordApp.Selection.Font.Name = "黑体";//标题使用黑体oWordApp.Selection.Font.Scaling = 100;//视图里面的比例控制//oWordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;oWordApp.Selection.Font.Size = 16;oWordApp.Selection.Font.Bold = 1;oWordApp.Selection.TypeText(label14.Text);//主标题oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;oWordApp.Selection.TypeText(label15.Text);//副标题oWordApp.Selection.Font.Name = "宋体";oWordApp.Selection.TypeParagraph();//另起一段 //oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;oWordApp.Selection.Font.Size = 11;oWordApp.Selection.Font.Bold = 0;#region 项不加粗//oWordApp.Selection.TypeText(layoutControlItem3.Text + label6.Text); oWordApp.Selection.TypeText(", ");//oWordApp.Selection.TypeText(layoutControlItem4.Text + label1.Text);//oWordApp.Selection.TypeParagraph();//另起一段 //oWordApp.Selection.TypeText(layoutControlItem5.Text + label2.Text); oWordApp.Selection.TypeText(", ");//oWordApp.Selection.TypeText(layoutControlItem6.Text + label3.Text);//oWordApp.Selection.TypeParagraph();//另起一段 //oWordApp.Selection.TypeText(layoutControlItem7.Text + label4.Text); oWordApp.Selection.TypeText(", ");//oWordApp.Selection.TypeText(layoutControlItem8.Text + label5.Text);//oWordApp.Selection.TypeParagraph();//另起一段 //oWordApp.Selection.TypeText(layoutControlItem10.Text);oWordApp.Selection.TypeParagraph();//另起一段 //oWordApp.Selection.TypeText(label10.Text);//SectDoc doc = GetDocument() as SectDoc;//if (doc.FileCount > 1)//{// switch (doc.FileCount)// {// case 2:// {// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem12.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(label17.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);// if (label12.Visible)// {// oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);// }// if (label13.Visible)// {// oWordApp.Selection.TypeText(layoutControlItem18.Text+label13.Text);// }// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(label8.Text+";");// oWordApp.Selection.TypeText(label9.Text);// //oWordApp.Selection.TypeParagraph();//另起一段 // break;// }// case 3:// {// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem12.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(label17.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem14.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(label19.Text);// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);// //oWordApp.Selection.TypeParagraph();//另起一段// break;// }// default:// break;// }//}//else//{// oWordApp.Selection.TypeParagraph();//另起一段 // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text); oWordApp.Selection.TypeText(", ");// oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text); oWordApp.Selection.TypeText(", ");// oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text); oWordApp.Selection.TypeText(", ");// oWordApp.Selection.TypeParagraph();//另起一段//}#endregionoWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem3.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label6.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem4.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label1.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem5.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label2.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem6.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label3.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem7.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label4.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem8.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label5.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem10.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label10.Text);//不加粗的值SectDoc doc = GetDocument() as SectDoc;if (doc.FileCount > 1){switch (doc.FileCount){case 2:{oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem12.Text);//加粗标题//oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label17.Text);//不加粗的值oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔if (label12.Visible){oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);}if (label13.Visible){oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text);}//oWordApp.Selection.TypeParagraph();//另起一段 break;}case 3:{oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.TypeText(layoutControlItem12.Text);oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.TypeText(label17.Text);oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.TypeText(layoutControlItem14.Text);oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.TypeText(label19.Text);oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);//oWordApp.Selection.TypeParagraph();//另起一段break;}default:break;}}else{oWordApp.Selection.TypeParagraph();//另起一段 oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem16.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label12.Text);//不加粗的值oWordApp.Selection.TypeText(", ");//各项之间间隔oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem18.Text);//加粗标题oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label13.Text);//不加粗的值//oWordApp.Selection.TypeParagraph();//另起一段 }oWordApp.Selection.Font.Size = 11.5f;//表插入行object start = oWordApp.Selection.Start;//在内容的最后插入表格object end = oWordApp.Selection.End; ;Word.Range tableLocation = oWordDoc.Range(ref start, ref end);oWordDoc.Tables.Add(tableLocation, dataGridView1.RowCount + 1, dataGridView1.ColumnCount, ref missing, ref missing);Word.Table newTable = oWordDoc.Tables[1];//设置表格的格式newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//内实体边框newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//外实体边框newTable.AllowAutoFit = true;newTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);//写入标题for (int i = 0; i < dataGridView1.ColumnCount; i++){newTable.Cell(1, i + 1).Range.Text = dataGridView1.Columns[i].HeaderText;}//写入数值for (int r = 0; r < dataGridView1.Rows.Count; r++){for (int i = 0; i < dataGridView1.ColumnCount; i++){//电阻计算 if (dataGridView1.Rows[r].Cells[i].Value == null){newTable.Cell(r + 2, i + 1).Range.Text = "";}else{newTable.Cell(r + 2, i + 1).Range.Text = dataGridView1.Rows[r].Cells[i].Value.ToString();}if (i == 6){newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphCenter;}else if (i == 7){}else{newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;}}System.Windows.Forms.Application.DoEvents();}object wdUnits;wdUnits = Word.WdUnits.wdLine;object nCount = dataGridView1.RowCount + 1+1;oWordApp.Selection.MoveDown(ref wdUnits, ref nCount, ref missing);oWordApp.Selection.Font.Size = 12;oWordApp.Selection.Font.Bold = 1;//防腐层和综合等级项加粗显示oWordApp.Selection.TypeText(label8.Text); oWordApp.Selection.TypeText(", ");oWordApp.Selection.TypeText(label9.Text);string strfilename = saveFileName;object filename = strfilename;//保存文档为word2000格式 oWordDoc.SaveAs2000(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);MessageBox.Show(strFileName + "导出成功", "提示", MessageBoxButtons.OK);//以下关闭Word程序 object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;oWordApp.Quit(ref nochanges, ref missing, ref missing);}catch (Exception ex){MessageBox.Show(ex.Message);}}
关于VS兼容WPS和OFFICE的问题相关推荐
- wps如何和matlab兼容,怎样使wps和office兼容
你知道怎样设置可以将wps和offic实现兼容 吗?下面就让学习啦小编告诉你设置wps和office兼容 的方法. 设置wps和office兼容的方法一 打开WPS文字,选择"文件--保存& ...
- wps和office哪个好用 wps和office兼容吗
wps和office是两款常见的办公软件,它们的功能非常相似,但在细节上又有着千差万别,因此很多人在初期都不知道如何选择,有些小伙伴则选择同时使用这两款软件,那么wps和office哪个好用,wps和 ...
- C# 10分钟入门基于WebOffice实现在线编辑文档,实时保存到服务器(所有office,兼容WPS)
今天,他来了(weboffice在线编辑文档). 上次写了一个在线预览的博,当然,效果并不是太理想,但是紧急解决了当时的问题. 后来,小编重新查找资料,求助大牛,终于使用新的方式替换了之前的low方法 ...
- wsp可以编辑html吗,wps和office兼容吗
wps和office兼容,但正常情况下只有WPS可以兼容WORD,WORD不能兼容WPS文件,如果想要实现两者的完美兼容,需要做好相关设置. 本文环境:windows10系统.thinkpad t48 ...
- freemarker生成word 兼容wps/office/小程序打开
freemarker生成word 兼容wps/office/小程序打开 (服务器必须是windows) 一:目的:兼容wps/电脑端office/小程序打开 freemarker生成的word底层技术 ...
- office怎么像wps一样多栏_工作中常用的两个工作软件,你觉得WPS和office哪个好用?...
工作中常用的两个工作软件,分别是WPS与office,对于这两个软件的适用性,大家也是说法不一,那么就WPS和office哪个好用,这里就来简单和大家闹闹. 一.占用空间 安装包 安装后占用的空间 不 ...
- 电脑同时安装了WPS和Office,怎么设置文件的默认打开方式?
Microsoft Office是微软公司的一款办公软件,学习工作中的必备工具.WPS则是由金山推出的新兴免费国产办公软件,也非常的好用,可以手机.平板.电脑三端同步. WPS的功能相对可能会较多,用 ...
- WPS与OFFICE同时存在,怎么设置office为默认打开方式?
WPS与OFFICE同时存在,怎么设置office为默认打开方式? 1.左下角开始程序找到WPS OFFice-->wps office工具-->配置工具 2.高级->兼容设置,取消 ...
- 艰难抉择,WPS和Office哪个好?
WPS是一个办公软件,它拥有完整的文字.表格.演示文稿功能,同时支持PPT.WORD.Excel等文件格式,使用也很方便.Office办公软件是全球最受欢迎的办公软件,Office软件由微软公司研发, ...
最新文章
- 自学python推荐书籍2019-2019最全Python入门学习路线,不是我吹,绝对是最全
- Flex CursorManager
- 使用SharedPreferences
- vue 编辑弹框,编辑页面,列表数据也会跟着变
- c语言xuanzeti1,c语言选择题库1
- xcode7: Undefined symbols for architecture i386: _iconv_open, referenced from:
- 测试人员转型是大势所趋:我的十年自动化测试经验分享
- 济宁与华为企业云战略合作携手推进云计算产业发展
- PMP 考点 第六章 项目进度管理
- 2018年1月份最热门的JavaScript开源项目
- 显卡内存足够但是torch报错RuntimeError: CUDA out of memory
- python网络爬虫之淘宝订单提取
- 正大国际期货:外盘黄金期货怎么做,需要注意什么?
- linux配置dns心得体会,dns实训报告心得体会.doc
- 【课程作业】西瓜书 机器学习课后习题 : 第五章
- Git - 时光机穿梭
- Linux系统下,下载谷歌浏览器
- 基频和倍频的概念_一倍频分析
- 指纹识别真安全吗?简单技术处理后橘子皮都能解锁
- Python中冷门但非常好用的内置函数
热门文章
- 使用ffmpeg将mp4文件转为h264文件或者264文件
- php 旅游网毕业论文,某旅游网站的设计与实现.docx
- 主流备份软件比较及特点分析,NBU/TSM/Legato/DP/Bakbone
- [JAVA毕业设计]废品回收管理系统设计与实现源码获取和系统演示
- 鸿蒙IDE的家居设备控制模板使用经验
- AWVS10.512超详细使用教程
- 南师大C:分鱼问题2(7筐鱼)
- 海尔集团 | 100道笔试题曝光(判断、问答、名词解释、综合题)附答案
- 缺少对公共可见类型或成员“XXX”的 XML 注释
- 实现用python简易演奏《数鸭子》