近来公司有个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仁兄的,高手。