在c#winforms中以mdi形式打开excel文件 [英] opening excel file as mdi form in c# winforms

查看:80
本文介绍了在c#winforms中以mdi形式打开excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
谁能告诉我如何在C#中以mdi形式打开excel工作表.

我搜索了代码项目,但没有找到任何值得的内容.

我的搜索结果:在MDI表单中托管Excel应用程序 [ ^ ]


请尽快帮助我.
谢谢,
Akky

Hi everyone,
Can anybody tell me that how can I open a excel worksheet as mdi form in C#.

I searched code project but didn''t find anything worthy.

My search results:Hosting an Excel application in an MDI Form[^]


Please help me as soon as possible.
Thanks,
Akky

推荐答案

您找不到有价值的东西,因为没有这样的东西.请看我对这个问题的评论.即使存在某种"Excel控件",它也将是Office的一部分,而不是Windows的一部分,因此它也可能不是.NET FCL的一部分(框架类库, ^ ]).

我建议您使用System.Windows.Forms.DataGridView:
模拟类似Excel的行为 http://msdn.microsoft.com/en-us/library/system. windows.forms.datagridview.aspx [ ^ ].

当您从1.0开始标记.NET的版本时(但是,我建议您在v.2.0之前不要考虑任何内容),我不会讨论在v.3.0中引入的WPF.

这样,您将需要自己开发功能,并且该组件足够靠近电子表格应用程序. Excel互操作所需的全部就是数据交换.您可以从找到的文章中找到足够的示例代码.如果您有任何疑问,也可以要求作者回答.如果您加载问题中引用的页面并找到评论和讨论",则可以单击添加评论或问题"来提问,并为作者提供回复的机会.

另一种方法是使用没有Excel数据格式的第三方开放源代码库使用Excel数据格式.例如,考虑LibreOffice:
http://en.wikipedia.org/wiki/LibreOffice [ http://www.libreoffice.org/ [ ^ ].

要在.NET项目中使用源代码,您将需要使用P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [^ ].

此CodeProject文章也可能有用:
基本P/调用 [ http://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ],
http://www.ecma-international.org/publications/standards/Ecma-372.htm [ ^ ].

最后,我建议您避免使用MDI.您不希望因这种过时且不方便的UI样式而遭受痛苦,而不会吓跑您的用户.帮自己一个忙:完全不要使用MDI.没有设计,您可以更轻松地实现设计,并且质量更高.即使Microsoft也不鼓励使用MDI,实际上,Microsoft已将其从WPF中删除,并且几乎不支持它.更重要的是,如果您使用MDI,则会吓跑所有用户.只是不要.我可以解释该怎么办.
首先,请参阅:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [如何在WPF中创建MDI父窗口? [ ^ ],
以及我过去的答案,同时我也解释了怎么做:
在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何最大程度地设置子表单,最小化最后一个子表单 [ ^ ].

—SA
You could not find anything worthy just because there is no such thing. Please see my comment to the question. Even if some kind of "Excel control" existed, it would be a part of Office, not Windows, so it could not be a part of .NET FCL (Framework Class Library, http://en.wikipedia.org/wiki/Framework_Class_Library[^]).

I suggested you to simulate Excel-like behavior using System.Windows.Forms.DataGridView:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx[^].

As you tag the versions of .NET from 1.0 (however, I suggest you should not consider anything prior v.2.0), I''m not discussing WPF which was introduced in v.3.0.

In this way, you would need to develop functionality by yourself, with this component being close enough for spreadsheet application; and all you need for Excel interop would be data exchange. You can find enough of sample code from the article you found. If you have questions, you can also ask the author to answer them. If you load the page you''ve referenced in your question and locate "Comments and Discussions", you can click "Add a Comment or Question" to ask your question and give the author a chance to reply.

The alternative way could be using third-party open-source library working with Excel data format without the interop. For example, consider LibreOffice:
http://en.wikipedia.org/wiki/LibreOffice[^],
http://www.libreoffice.org/[^].

To use the source code in your .NET projects, you will need to use P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

Alternatively to P/Invoke, you can use a mixed-mode project written in C++/CLI. It can contain unmanaged code, but you can wrap some code in "ref" managed classes or structures, and make them public to be accessible by other .NET assemblies. This way, the resulting executable module would become a .NET Assembly which you can use as any other assembly in your .NET project. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^].

Finally, I would recommend you to avoid MDI. You don''t want the suffering from this outdated and inconvenient UI style and scare off your users. Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don''t. I can explain what to do instead.
First, please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^],
as well as my past answers where I also explain what to do instead:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


这篇关于在c#winforms中以mdi形式打开excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆