近來公司有個MS Office 編程的活,借機也研究了一下,略有收獲,不敢獨享,希望大家扔雞蛋,但最好不是臭的。
這個項目里,主要是想將Word ,Excel做成的文檔中的圖形導出為XML文件。
這里的關鍵是找出文檔中的圖形對象,并識別出類型。做法如下:
??? ??? ??? ApplicationClass app = new ApplicationClass();
??????????? Document doc = null;
??????????? object missing = System.Reflection.Missing.Value;
??????????? object FileName = @"E:\aa.doc";// Sample Doc
??????????? object readOnly = false;
??????????? object isVisible = true;
??????????? object index = 0;
??????????? try
??????????? {
??????????????? doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
??????????????????? ref missing, ref missing, ref missing, ref missing, ref missing,
??????????????????? ref missing, ref missing, ref missing, ref isVisible, ref missing,
??????????????????? ref missing, ref missing, ref missing);
??????????????? doc.ActiveWindow.Selection.WholeStory();
??????????????? doc.ActiveWindow.Selection.Copy();
??????????????
??????????????? foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
??????????????? {
??????????????????? Console.WriteLine("WordShape-->" + shape.Name);
??????????????? }
??? ??? }finally
??? ??? {
??? ?? ?? ?? ?? if (doc != null)
??????????????? {
??????????????????? doc.Close(ref missing, ref missing, ref missing);
??????????????????? doc = null;
??????????????? }
??????????????? if (app != null)
??????????????? {
??????????????????? app.Quit(ref missing, ref missing, ref missing);
??????????????????? app = null;
??????????????? }
??? ??? }
輸出為:
=================================
WordShape-->Canvas 3
WordShape-->Rectangle 7
WordShape-->Oval 8
WordShape-->Text Box 9
WordShape-->Text Box 12
WordShape-->AutoShape 13
Text-->
Aaa
A
A
A
=================================
aa.doc截圖如下:
剩下的怎么做不用多說了吧!!!
關于建立,讀取,存儲 Office文檔,我想網上有都是,不細說了。不懂的可參考下面鏈接:
??
http://msdn2.microsoft.com/en-us/library/aa140045(office.10).aspx ? MSDN的很基礎的。
http://www.cnblogs.com/macroxu-1982/archive/2006/11/27/573856.html 一位Blog仁兄的,高手。
這個項目里,主要是想將Word ,Excel做成的文檔中的圖形導出為XML文件。
這里的關鍵是找出文檔中的圖形對象,并識別出類型。做法如下:
??? ??? ??? ApplicationClass app = new ApplicationClass();
??????????? Document doc = null;
??????????? object missing = System.Reflection.Missing.Value;
??????????? object FileName = @"E:\aa.doc";// Sample Doc
??????????? object readOnly = false;
??????????? object isVisible = true;
??????????? object index = 0;
??????????? try
??????????? {
??????????????? doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
??????????????????? ref missing, ref missing, ref missing, ref missing, ref missing,
??????????????????? ref missing, ref missing, ref missing, ref isVisible, ref missing,
??????????????????? ref missing, ref missing, ref missing);
??????????????? doc.ActiveWindow.Selection.WholeStory();
??????????????? doc.ActiveWindow.Selection.Copy();
??????????????
??????????????? foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
??????????????? {
??????????????????? Console.WriteLine("WordShape-->" + shape.Name);
??????????????? }
??? ??? }finally
??? ??? {
??? ?? ?? ?? ?? if (doc != null)
??????????????? {
??????????????????? doc.Close(ref missing, ref missing, ref missing);
??????????????????? doc = null;
??????????????? }
??????????????? if (app != null)
??????????????? {
??????????????????? app.Quit(ref missing, ref missing, ref missing);
??????????????????? app = null;
??????????????? }
??? ??? }
輸出為:
=================================
WordShape-->Canvas 3
WordShape-->Rectangle 7
WordShape-->Oval 8
WordShape-->Text Box 9
WordShape-->Text Box 12
WordShape-->AutoShape 13
Text-->
Aaa
A
A
A
=================================
aa.doc截圖如下:

剩下的怎么做不用多說了吧!!!
關于建立,讀取,存儲 Office文檔,我想網上有都是,不細說了。不懂的可參考下面鏈接:
??
http://msdn2.microsoft.com/en-us/library/aa140045(office.10).aspx ? MSDN的很基礎的。
http://www.cnblogs.com/macroxu-1982/archive/2006/11/27/573856.html 一位Blog仁兄的,高手。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
