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

来源:互联网 发布:sql中用not in值过多 编辑:程序博客网 时间:2024/05/04 16:39
           最近一直在做的开发是关于导出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.我在代码中也做了一些其他的操作,可以参考。
    上代码 !!!
 
  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();//另起一段                //}                #endregion                oWordApp.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);            }        }

0 0
原创粉丝点击