码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作WPS和office兼容性的问题

时间:2017-05-11 15:14:25      阅读:3190      评论:0      收藏:0      [点我收藏+]

标签:ret   assembly   doc   alt   plain   add   pac   销售人员   sharp   

http://blog.csdn.net/yanpengliumin/article/details/50344799

         最近一直在做的开发是关于导出word的功能,一开始的做法是在VS中直接添加引用office PIA,Microsoft.Office.Interop.Word,VS08有两个版本,V11和V12,V11对应的是office03,V12对应的office07,试验之后得出结论,这两个PIA的引用只会影响开发机器的使用,就是说要与开发机器的office版本相对应。在目标机器上都是可以使用的,没有问题。
            接下来说一下关于PIA的事情,Primary Interop Assembly,中文解释为:主互程序操作集。通过查阅MSDN 可以了解到,VS在调用COM和COM+组件时会通过解析自动生成 Interop Assembly,即程序操作集,成为IA,这个IA包含我们代码中可以调用的COM的接口,属性一类的东西,可以这样理解,IA就是你的程序和COM组件之间的一个桥接。而PIA是.NET官方生成的IA,这个是开发者根据常用的COM组件生成的专门用于.NET运行环境的IA,具有更高的可靠性。到这一步,经过验证,任何word的操作只需引用.net环境的下
Microsoft.Office.Interop.Word组件,操作EXCEL需要引用.net环境下的Microsoft.Office.Interop.Excel组件。
        关于word的实际操作代码可以查阅相应的API,在后面我会给出我的代码,主要涉及到操作页眉,设置字体,设置间距,插入表格等操作。
       问题来了,销售人员反应有的客户不使用office,只使用WPS。我差点就问WPS是个什么鬼。还是自己查查资料看看中国人写的办公软件吧。WPS发展到目前最新版本为WPS2016。版本就有点多了  02、03 、05、07、10、 13 等等。作为程序员我只关心你的二次开发用的是什么,经过测试,WPS10之前的版本需要自己生成.net支持的IA,WPS2013有两个版本,个人版和企业版,个人版中没有提供PIA,企业版中提供了WPSOFFICEPIA.EXE生成工具,安装之后,就会生成.net环境下可以用的PIA。不知道什么原因,我的VS2008没有在“引用”中没有看到生成的PIA,个人猜测由于我的VS2008是破解版,所以看不到,没什么关系,可以自己找到,在“运行”中输入“C:\windows\assembly\gac-32”回车之后就可以进入一个文件目录,这个目录中就是所有的PIA程序,找到Kingsoft开头的目录,有8个,分别提供了word、excel 、ppt 等操作,每个类型各有两个版本,分别是V8和V9,通过分别引用之后,可以看出 V8是支持老版本WPS的API。例如可以用et.Application创建ET表格,用WPS.Application创建wps文档。V9版本就比较高级了。提供了对于office相同的操作dll。可以直接使用word.application创建word文档或者wps文档。网上有人说V9版本提供了Kwps.Application创建wps文档,我努力一番,也没有找到这种方法,不过目前来说只要V9兼容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.我在代码中也做了一些其他的操作,可以参考。
    上代码 !!!
 
[csharp] view plain copy
 
  1. private void ExportToWps()  
  2.       {  
  3.           try  
  4.           {  
  5.               string strFileName = label14.Text + "-" + label15.Text;  
  6.               string saveFileName = "";  
  7.               SaveFileDialog saveDialog = new SaveFileDialog();  
  8.               saveDialog.DefaultExt = "doc";  
  9.               saveDialog.Filter = "Word文件|*.doc";  
  10.               saveDialog.FileName = strFileName;  
  11.               saveDialog.ShowDialog();  
  12.               saveFileName = saveDialog.FileName;  
  13.               if (saveFileName.IndexOf(":") < 0) return; //被点了取消   
  14.                
  15.           //   Word.ApplicationClass oWordApp = new Word.ApplicationClass();//建立Word   对象,启动word程序    
  16.              Word.Application oWordApp = new Word.Application();  
  17.                
  18.               if (oWordApp == null)  
  19.               {  
  20.                   MessageBox.Show("word版本错误!", "error");  
  21.                   return;  
  22.               }  
  23.               object missing = System.Reflection.Missing.Value;  
  24.               object oTemplate = System.Windows.Forms.Application.StartupPath + "\\Normal.dot";  
  25.   
  26.             Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);//新建word文档     
  27.   
  28.               oWordApp.Visible = false;//设置Word程序可见,如果为false   那么word不可见     
  29.               //页面设置     
  30.               oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f);       //上     
  31.               oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下     
  32.               oWordDoc.PageSetup.LeftMargin = oWordApp.CentimetersToPoints(2.2f);//左     
  33.               oWordDoc.PageSetup.RightMargin = oWordApp.CentimetersToPoints(2.2f);//右     
  34.               ////添加页眉   林总不需要  
  35.               //oWordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;   //激活页眉的编辑     
  36.               //oWordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;   //设置对齐方式     
  37.               //string headtext1 =PcaSettings.GetSettingString (101);  
  38.               //oWordApp.Selection.Font.Name = "宋体";   //设置字体     
  39.               //oWordApp.Selection.Font.Size = 10.5f;  
  40.               //oWordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;  
  41.               //oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;   //bu添加下划线     
  42.               //oWordApp.Selection.TypeText(headtext1);  
  43.               //oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;  
  44.               //添加页脚     
  45.               string foottext1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  46.               oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;   //激活页脚的编辑     
  47.               oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  48.               oWordApp.Selection.Font.Name = "仿宋_GB2312";  
  49.               oWordApp.Selection.Font.Size = 8;  
  50.               oWordApp.Selection.TypeText(foottext1);  
  51.               //添加正文     
  52.               oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑     
  53.               oWordApp.Selection.Font.Name = "黑体";//标题使用黑体  
  54.               oWordApp.Selection.Font.Scaling = 100;//视图里面的比例控制  
  55.               //oWordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;  
  56.               oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  57.               oWordApp.Selection.Font.Size = 16;  
  58.               oWordApp.Selection.Font.Bold = 1;  
  59.               oWordApp.Selection.TypeText(label14.Text);//主标题  
  60.               oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  61.               oWordApp.Selection.TypeParagraph();//另起一段    
  62.               oWordApp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;  
  63.               oWordApp.Selection.TypeText(label15.Text);//副标题  
  64.               oWordApp.Selection.Font.Name = "宋体";  
  65.               oWordApp.Selection.TypeParagraph();//另起一段    
  66.               //oWordApp.Selection.TypeParagraph();//另起一段     
  67.               oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;  
  68.               oWordApp.Selection.Font.Size = 11;  
  69.               oWordApp.Selection.Font.Bold = 0;  
  70.               #region 项不加粗  
  71.               //oWordApp.Selection.TypeText(layoutControlItem3.Text + label6.Text); oWordApp.Selection.TypeText(",    ");  
  72.               //oWordApp.Selection.TypeText(layoutControlItem4.Text + label1.Text);  
  73.               //oWordApp.Selection.TypeParagraph();//另起一段   
  74.               //oWordApp.Selection.TypeText(layoutControlItem5.Text + label2.Text); oWordApp.Selection.TypeText(",    ");  
  75.               //oWordApp.Selection.TypeText(layoutControlItem6.Text + label3.Text);  
  76.               //oWordApp.Selection.TypeParagraph();//另起一段   
  77.               //oWordApp.Selection.TypeText(layoutControlItem7.Text + label4.Text); oWordApp.Selection.TypeText(",    ");  
  78.               //oWordApp.Selection.TypeText(layoutControlItem8.Text + label5.Text);  
  79.               //oWordApp.Selection.TypeParagraph();//另起一段   
  80.               //oWordApp.Selection.TypeText(layoutControlItem10.Text);  
  81.               ////oWordApp.Selection.TypeParagraph();//另起一段   
  82.               //oWordApp.Selection.TypeText(label10.Text);  
  83.               //SectDoc doc = GetDocument() as SectDoc;  
  84.               //if (doc.FileCount > 1)  
  85.               //{  
  86.               //    switch (doc.FileCount)  
  87.               //    {  
  88.               //        case 2:  
  89.               //            {  
  90.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  91.               //                oWordApp.Selection.TypeText(layoutControlItem12.Text);  
  92.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  93.               //                oWordApp.Selection.TypeText(label17.Text);  
  94.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  95.               //                oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);  
  96.   
  97.               //                if (label12.Visible)  
  98.               //                {  
  99.               //                    oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);  
  100.               //                }  
  101.               //                if (label13.Visible)  
  102.               //                {  
  103.               //                    oWordApp.Selection.TypeText(layoutControlItem18.Text+label13.Text);  
  104.               //                }  
  105.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  106.               //                oWordApp.Selection.TypeText(label8.Text+";");  
  107.               //                oWordApp.Selection.TypeText(label9.Text);  
  108.               //                //oWordApp.Selection.TypeParagraph();//另起一段   
  109.               //                break;  
  110.               //            }  
  111.               //        case 3:  
  112.               //            {  
  113.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  114.               //                oWordApp.Selection.TypeText(layoutControlItem12.Text);  
  115.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  116.               //                oWordApp.Selection.TypeText(label17.Text);  
  117.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  118.               //                oWordApp.Selection.TypeText(layoutControlItem14.Text);  
  119.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  120.               //                oWordApp.Selection.TypeText(label19.Text);  
  121.               //                oWordApp.Selection.TypeParagraph();//另起一段   
  122.               //                oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);  
  123.               //                //oWordApp.Selection.TypeParagraph();//另起一段  
  124.               //                break;  
  125.               //            }  
  126.               //        default:  
  127.               //            break;  
  128.               //    }  
  129.               //}  
  130.               //else  
  131.               //{  
  132.               //    oWordApp.Selection.TypeParagraph();//另起一段   
  133.               //    oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text); oWordApp.Selection.TypeText(", ");  
  134.               //    oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text); oWordApp.Selection.TypeText(", ");  
  135.               //    oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text); oWordApp.Selection.TypeText(", ");  
  136.               //    oWordApp.Selection.TypeParagraph();//另起一段  
  137.               //}  
  138.               #endregion  
  139.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem3.Text);//加粗标题  
  140.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label6.Text);//不加粗的值  
  141.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  142.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem4.Text);//加粗标题  
  143.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label1.Text);//不加粗的值  
  144.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  145.               oWordApp.Selection.TypeParagraph();//另起一段   
  146.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem5.Text);//加粗标题  
  147.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label2.Text);//不加粗的值  
  148.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  149.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem6.Text);//加粗标题  
  150.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label3.Text);//不加粗的值  
  151.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  152.               oWordApp.Selection.TypeParagraph();//另起一段   
  153.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem7.Text);//加粗标题  
  154.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label4.Text);//不加粗的值  
  155.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  156.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem8.Text);//加粗标题  
  157.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label5.Text);//不加粗的值  
  158.               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  159.               oWordApp.Selection.TypeParagraph();//另起一段   
  160.               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem10.Text);//加粗标题  
  161.               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label10.Text);//不加粗的值  
  162.               SectDoc doc = GetDocument() as SectDoc;  
  163.               if (doc.FileCount > 1)  
  164.               {  
  165.                   switch (doc.FileCount)  
  166.                   {  
  167.                       case 2:  
  168.                           {  
  169.                               oWordApp.Selection.TypeParagraph();//另起一段   
  170.                               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem12.Text);//加粗标题  
  171.                               //oWordApp.Selection.TypeParagraph();//另起一段   
  172.                               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label17.Text);//不加粗的值  
  173.                               oWordApp.Selection.TypeParagraph();//另起一段   
  174.                               oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题  
  175.                               oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值  
  176.                               oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  177.                               if (label12.Visible)  
  178.                               {  
  179.                                   oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);  
  180.                               }  
  181.                               if (label13.Visible)  
  182.                               {  
  183.                                   oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text);  
  184.                               }  
  185.                               //oWordApp.Selection.TypeParagraph();//另起一段   
  186.                               break;  
  187.                           }  
  188.                       case 3:  
  189.                           {  
  190.                               oWordApp.Selection.TypeParagraph();//另起一段   
  191.                               oWordApp.Selection.TypeText(layoutControlItem12.Text);  
  192.                               oWordApp.Selection.TypeParagraph();//另起一段   
  193.                               oWordApp.Selection.TypeText(label17.Text);  
  194.                               oWordApp.Selection.TypeParagraph();//另起一段   
  195.                               oWordApp.Selection.TypeText(layoutControlItem14.Text);  
  196.                               oWordApp.Selection.TypeParagraph();//另起一段   
  197.                               oWordApp.Selection.TypeText(label19.Text);  
  198.                               oWordApp.Selection.TypeParagraph();//另起一段   
  199.                               oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);  
  200.                               //oWordApp.Selection.TypeParagraph();//另起一段  
  201.                               break;  
  202.                           }  
  203.                       default:  
  204.                           break;  
  205.                   }  
  206.               }  
  207.               else  
  208.               {  
  209.                   oWordApp.Selection.TypeParagraph();//另起一段   
  210.                   oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗标题  
  211.                   oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值  
  212.                   oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  213.                   oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem16.Text);//加粗标题  
  214.                   oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label12.Text);//不加粗的值  
  215.                   oWordApp.Selection.TypeText(",    ");//各项之间间隔  
  216.                   oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem18.Text);//加粗标题  
  217.                   oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label13.Text);//不加粗的值  
  218.                   //oWordApp.Selection.TypeParagraph();//另起一段   
  219.               }  
  220.               oWordApp.Selection.Font.Size = 11.5f;  
  221.               //表插入行  
  222.               object start = oWordApp.Selection.Start;//在内容的最后插入表格  
  223.               object end = oWordApp.Selection.End; ;  
  224.               Word.Range tableLocation = oWordDoc.Range(ref start, ref end);  
  225.               oWordDoc.Tables.Add(tableLocation, dataGridView1.RowCount + 1, dataGridView1.ColumnCount, ref missing, ref missing);  
  226.               Word.Table newTable = oWordDoc.Tables[1];  
  227.               //设置表格的格式  
  228.               newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//内实体边框  
  229.               newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//外实体边框  
  230.               newTable.AllowAutoFit = true;  
  231.               newTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);  
  232.               //写入标题  
  233.               for (int i = 0; i < dataGridView1.ColumnCount; i++)  
  234.               {  
  235.                   newTable.Cell(1, i + 1).Range.Text = dataGridView1.Columns[i].HeaderText;  
  236.               }  
  237.               //写入数值  
  238.               for (int r = 0; r < dataGridView1.Rows.Count; r++)  
  239.               {  
  240.                   for (int i = 0; i < dataGridView1.ColumnCount; i++)  
  241.                   {  
  242.                       //电阻计算                      
  243.                       if (dataGridView1.Rows[r].Cells[i].Value == null)  
  244.                       {  
  245.                           newTable.Cell(r + 2, i + 1).Range.Text = "";  
  246.                       }  
  247.                       else  
  248.                       {  
  249.                           newTable.Cell(r + 2, i + 1).Range.Text = dataGridView1.Rows[r].Cells[i].Value.ToString();  
  250.                       }  
  251.                       if (i == 6)  
  252.                       {  
  253.                           newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  254.                       }  
  255.                       else if (i == 7)  
  256.                       {  
  257.   
  258.                       }  
  259.                       else  
  260.                       {  
  261.                           newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;  
  262.                       }  
  263.                   }  
  264.                   System.Windows.Forms.Application.DoEvents();  
  265.               }  
  266.               object wdUnits;  
  267.               wdUnits = Word.WdUnits.wdLine;  
  268.               object nCount = dataGridView1.RowCount + 1+1;  
  269.               oWordApp.Selection.MoveDown(ref wdUnits, ref nCount, ref missing);  
  270.               oWordApp.Selection.Font.Size = 12;  
  271.               oWordApp.Selection.Font.Bold = 1;//防腐层和综合等级项加粗显示  
  272.               oWordApp.Selection.TypeText(label8.Text); oWordApp.Selection.TypeText(", ");  
  273.               oWordApp.Selection.TypeText(label9.Text);  
  274.               string strfilename = saveFileName;  
  275.               object filename = strfilename;  
  276.               //保存文档为word2000格式     
  277.               oWordDoc.SaveAs2000(ref   filename, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing);  
  278.               MessageBox.Show(strFileName + "导出成功", "提示", MessageBoxButtons.OK);  
  279.   
  280.               //以下关闭Word程序     
  281.               object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;  
  282.               oWordApp.Quit(ref   nochanges, ref   missing, ref   missing);  
  283.           }  
  284.           catch (Exception ex)  
  285.           {  
  286.               MessageBox.Show(ex.Message);  
  287.           }  
  288.       }  

C#操作WPS和office兼容性的问题

标签:ret   assembly   doc   alt   plain   add   pac   销售人员   sharp   

原文地址:http://www.cnblogs.com/zcm123/p/6840963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!