最近一直在做的開發是關于導出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.我在代碼中也做了一些其他的操作,可以參考。
????上代碼 !!!
????????????接下來說一下關于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();//另起一段//}#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);}}
?
---------------------
作者:leader0414
來源:CSDN
原文:https://blog.csdn.net/yanpengliumin/article/details/50344799
版權聲明:本文為作者原創文章,轉載請附上博文鏈接!
內容解析By:CSDN,CNBLOG博客文章一鍵轉載插件