1. 程式人生 > >C#操作WPS和office兼容性的問題

C#操作WPS和office兼容性的問題

dsa r+ sdn 開發機 ror pep 一是 cells bar

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. // Word.ApplicationClass oWordApp = new Word.ApplicationClass();//建立Word 對象,啟動word程序
  15. Word.Application oWordApp = new Word.Application();
  16. if (oWordApp == null)
  17. {
  18. MessageBox.Show("word版本錯誤!", "error");
  19. return;
  20. }
  21. object missing = System.Reflection.Missing.Value;
  22. object oTemplate = System.Windows.Forms.Application.StartupPath + "\\Normal.dot";
  23. Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);//新建word文檔
  24. oWordApp.Visible = false;//設置Word程序可見,如果為false 那麽word不可見
  25. //頁面設置
  26. oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f); //上
  27. oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下
  28. oWordDoc.PageSetup.LeftMargin = oWordApp.CentimetersToPoints(2.2f);//左
  29. oWordDoc.PageSetup.RightMargin = oWordApp.CentimetersToPoints(2.2f);//右
  30. ////添加頁眉 林總不需要
  31. //oWordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; //激活頁眉的編輯
  32. //oWordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; //設置對齊方式
  33. //string headtext1 =PcaSettings.GetSettingString (101);
  34. //oWordApp.Selection.Font.Name = "宋體"; //設置字體
  35. //oWordApp.Selection.Font.Size = 10.5f;
  36. //oWordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
  37. //oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone; //bu添加下劃線
  38. //oWordApp.Selection.TypeText(headtext1);
  39. //oWordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
  40. //添加頁腳
  41. string foottext1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  42. oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter; //激活頁腳的編輯
  43. oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
  44. oWordApp.Selection.Font.Name = "仿宋_GB2312";
  45. oWordApp.Selection.Font.Size = 8;
  46. oWordApp.Selection.TypeText(foottext1);
  47. //添加正文
  48. oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//激活頁面內容的編輯
  49. oWordApp.Selection.Font.Name = "黑體";//標題使用黑體
  50. oWordApp.Selection.Font.Scaling = 100;//視圖裏面的比例控制
  51. //oWordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;
  52. oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
  53. oWordApp.Selection.Font.Size = 16;
  54. oWordApp.Selection.Font.Bold = 1;
  55. oWordApp.Selection.TypeText(label14.Text);//主標題
  56. oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
  57. oWordApp.Selection.TypeParagraph();//另起一段
  58. oWordApp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
  59. oWordApp.Selection.TypeText(label15.Text);//副標題
  60. oWordApp.Selection.Font.Name = "宋體";
  61. oWordApp.Selection.TypeParagraph();//另起一段
  62. //oWordApp.Selection.TypeParagraph();//另起一段
  63. oWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
  64. oWordApp.Selection.Font.Size = 11;
  65. oWordApp.Selection.Font.Bold = 0;
  66. #region 項不加粗
  67. //oWordApp.Selection.TypeText(layoutControlItem3.Text + label6.Text); oWordApp.Selection.TypeText(", ");
  68. //oWordApp.Selection.TypeText(layoutControlItem4.Text + label1.Text);
  69. //oWordApp.Selection.TypeParagraph();//另起一段
  70. //oWordApp.Selection.TypeText(layoutControlItem5.Text + label2.Text); oWordApp.Selection.TypeText(", ");
  71. //oWordApp.Selection.TypeText(layoutControlItem6.Text + label3.Text);
  72. //oWordApp.Selection.TypeParagraph();//另起一段
  73. //oWordApp.Selection.TypeText(layoutControlItem7.Text + label4.Text); oWordApp.Selection.TypeText(", ");
  74. //oWordApp.Selection.TypeText(layoutControlItem8.Text + label5.Text);
  75. //oWordApp.Selection.TypeParagraph();//另起一段
  76. //oWordApp.Selection.TypeText(layoutControlItem10.Text);
  77. ////oWordApp.Selection.TypeParagraph();//另起一段
  78. //oWordApp.Selection.TypeText(label10.Text);
  79. //SectDoc doc = GetDocument() as SectDoc;
  80. //if (doc.FileCount > 1)
  81. //{
  82. // switch (doc.FileCount)
  83. // {
  84. // case 2:
  85. // {
  86. // oWordApp.Selection.TypeParagraph();//另起一段
  87. // oWordApp.Selection.TypeText(layoutControlItem12.Text);
  88. // oWordApp.Selection.TypeParagraph();//另起一段
  89. // oWordApp.Selection.TypeText(label17.Text);
  90. // oWordApp.Selection.TypeParagraph();//另起一段
  91. // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
  92. // if (label12.Visible)
  93. // {
  94. // oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);
  95. // }
  96. // if (label13.Visible)
  97. // {
  98. // oWordApp.Selection.TypeText(layoutControlItem18.Text+label13.Text);
  99. // }
  100. // oWordApp.Selection.TypeParagraph();//另起一段
  101. // oWordApp.Selection.TypeText(label8.Text+";");
  102. // oWordApp.Selection.TypeText(label9.Text);
  103. // //oWordApp.Selection.TypeParagraph();//另起一段
  104. // break;
  105. // }
  106. // case 3:
  107. // {
  108. // oWordApp.Selection.TypeParagraph();//另起一段
  109. // oWordApp.Selection.TypeText(layoutControlItem12.Text);
  110. // oWordApp.Selection.TypeParagraph();//另起一段
  111. // oWordApp.Selection.TypeText(label17.Text);
  112. // oWordApp.Selection.TypeParagraph();//另起一段
  113. // oWordApp.Selection.TypeText(layoutControlItem14.Text);
  114. // oWordApp.Selection.TypeParagraph();//另起一段
  115. // oWordApp.Selection.TypeText(label19.Text);
  116. // oWordApp.Selection.TypeParagraph();//另起一段
  117. // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
  118. // //oWordApp.Selection.TypeParagraph();//另起一段
  119. // break;
  120. // }
  121. // default:
  122. // break;
  123. // }
  124. //}
  125. //else
  126. //{
  127. // oWordApp.Selection.TypeParagraph();//另起一段
  128. // oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text); oWordApp.Selection.TypeText(", ");
  129. // oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text); oWordApp.Selection.TypeText(", ");
  130. // oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text); oWordApp.Selection.TypeText(", ");
  131. // oWordApp.Selection.TypeParagraph();//另起一段
  132. //}
  133. #endregion
  134. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem3.Text);//加粗標題
  135. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label6.Text);//不加粗的值
  136. oWordApp.Selection.TypeText(", ");//各項之間間隔
  137. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem4.Text);//加粗標題
  138. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label1.Text);//不加粗的值
  139. oWordApp.Selection.TypeText(", ");//各項之間間隔
  140. oWordApp.Selection.TypeParagraph();//另起一段
  141. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem5.Text);//加粗標題
  142. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label2.Text);//不加粗的值
  143. oWordApp.Selection.TypeText(", ");//各項之間間隔
  144. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem6.Text);//加粗標題
  145. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label3.Text);//不加粗的值
  146. oWordApp.Selection.TypeText(", ");//各項之間間隔
  147. oWordApp.Selection.TypeParagraph();//另起一段
  148. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem7.Text);//加粗標題
  149. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label4.Text);//不加粗的值
  150. oWordApp.Selection.TypeText(", ");//各項之間間隔
  151. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem8.Text);//加粗標題
  152. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label5.Text);//不加粗的值
  153. oWordApp.Selection.TypeText(", ");//各項之間間隔
  154. oWordApp.Selection.TypeParagraph();//另起一段
  155. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem10.Text);//加粗標題
  156. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label10.Text);//不加粗的值
  157. SectDoc doc = GetDocument() as SectDoc;
  158. if (doc.FileCount > 1)
  159. {
  160. switch (doc.FileCount)
  161. {
  162. case 2:
  163. {
  164. oWordApp.Selection.TypeParagraph();//另起一段
  165. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem12.Text);//加粗標題
  166. //oWordApp.Selection.TypeParagraph();//另起一段
  167. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label17.Text);//不加粗的值
  168. oWordApp.Selection.TypeParagraph();//另起一段
  169. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗標題
  170. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值
  171. oWordApp.Selection.TypeText(", ");//各項之間間隔
  172. if (label12.Visible)
  173. {
  174. oWordApp.Selection.TypeText(layoutControlItem16.Text + label12.Text);
  175. }
  176. if (label13.Visible)
  177. {
  178. oWordApp.Selection.TypeText(layoutControlItem18.Text + label13.Text);
  179. }
  180. //oWordApp.Selection.TypeParagraph();//另起一段
  181. break;
  182. }
  183. case 3:
  184. {
  185. oWordApp.Selection.TypeParagraph();//另起一段
  186. oWordApp.Selection.TypeText(layoutControlItem12.Text);
  187. oWordApp.Selection.TypeParagraph();//另起一段
  188. oWordApp.Selection.TypeText(label17.Text);
  189. oWordApp.Selection.TypeParagraph();//另起一段
  190. oWordApp.Selection.TypeText(layoutControlItem14.Text);
  191. oWordApp.Selection.TypeParagraph();//另起一段
  192. oWordApp.Selection.TypeText(label19.Text);
  193. oWordApp.Selection.TypeParagraph();//另起一段
  194. oWordApp.Selection.TypeText(layoutControlItem15.Text + label11.Text);
  195. //oWordApp.Selection.TypeParagraph();//另起一段
  196. break;
  197. }
  198. default:
  199. break;
  200. }
  201. }
  202. else
  203. {
  204. oWordApp.Selection.TypeParagraph();//另起一段
  205. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem15.Text);//加粗標題
  206. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label11.Text);//不加粗的值
  207. oWordApp.Selection.TypeText(", ");//各項之間間隔
  208. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem16.Text);//加粗標題
  209. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label12.Text);//不加粗的值
  210. oWordApp.Selection.TypeText(", ");//各項之間間隔
  211. oWordApp.Selection.Font.Bold = 1; oWordApp.Selection.TypeText(layoutControlItem18.Text);//加粗標題
  212. oWordApp.Selection.Font.Bold = 0; oWordApp.Selection.TypeText(label13.Text);//不加粗的值
  213. //oWordApp.Selection.TypeParagraph();//另起一段
  214. }
  215. oWordApp.Selection.Font.Size = 11.5f;
  216. //表插入行
  217. object start = oWordApp.Selection.Start;//在內容的最後插入表格
  218. object end = oWordApp.Selection.End; ;
  219. Word.Range tableLocation = oWordDoc.Range(ref start, ref end);
  220. oWordDoc.Tables.Add(tableLocation, dataGridView1.RowCount + 1, dataGridView1.ColumnCount, ref missing, ref missing);
  221. Word.Table newTable = oWordDoc.Tables[1];
  222. //設置表格的格式
  223. newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//內實體邊框
  224. newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//外實體邊框
  225. newTable.AllowAutoFit = true;
  226. newTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
  227. //寫入標題
  228. for (int i = 0; i < dataGridView1.ColumnCount; i++)
  229. {
  230. newTable.Cell(1, i + 1).Range.Text = dataGridView1.Columns[i].HeaderText;
  231. }
  232. //寫入數值
  233. for (int r = 0; r < dataGridView1.Rows.Count; r++)
  234. {
  235. for (int i = 0; i < dataGridView1.ColumnCount; i++)
  236. {
  237. //電阻計算
  238. if (dataGridView1.Rows[r].Cells[i].Value == null)
  239. {
  240. newTable.Cell(r + 2, i + 1).Range.Text = "";
  241. }
  242. else
  243. {
  244. newTable.Cell(r + 2, i + 1).Range.Text = dataGridView1.Rows[r].Cells[i].Value.ToString();
  245. }
  246. if (i == 6)
  247. {
  248. newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphCenter;
  249. }
  250. else if (i == 7)
  251. {
  252. }
  253. else
  254. {
  255. newTable.Cell(r + 2, i + 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
  256. }
  257. }
  258. System.Windows.Forms.Application.DoEvents();
  259. }
  260. object wdUnits;
  261. wdUnits = Word.WdUnits.wdLine;
  262. object nCount = dataGridView1.RowCount + 1+1;
  263. oWordApp.Selection.MoveDown(ref wdUnits, ref nCount, ref missing);
  264. oWordApp.Selection.Font.Size = 12;
  265. oWordApp.Selection.Font.Bold = 1;//防腐層和綜合等級項加粗顯示
  266. oWordApp.Selection.TypeText(label8.Text); oWordApp.Selection.TypeText(", ");
  267. oWordApp.Selection.TypeText(label9.Text);
  268. string strfilename = saveFileName;
  269. object filename = strfilename;
  270. //保存文檔為word2000格式
  271. oWordDoc.SaveAs2000(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  272. MessageBox.Show(strFileName + "導出成功", "提示", MessageBoxButtons.OK);
  273. //以下關閉Word程序
  274. object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;
  275. oWordApp.Quit(ref nochanges, ref missing, ref missing);
  276. }
  277. catch (Exception ex)
  278. {
  279. MessageBox.Show(ex.Message);
  280. }
  281. }

C#操作WPS和office兼容性的問題