为已安装的 Excel 选择正确的用途

发布于 2024-12-10 16:51:51 字数 1057 浏览 0 评论 0原文

我有一个场景,客户可以安装 Excel 2007 或安装 Excel 2010,我需要能够导出和导入他们创建的 Excel 文件,OLEDB 不是一个选项,所以我将使用 Interop 服务。

问题是,当我为 2010 年编写代码时,它在 2007 年不起作用,反之亦然,我怎样才能实现这一目标?

我可以以某种方式导入两个引用并根据安装的版本定义 Excel 吗?

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range xlRange;

            object misValue = System.Reflection.Missing.Value;

            pbarExcelGenerate.Visible = true;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "Material Results";

            pbarExcelGenerate.Value = 5;


            xlRange = xlWorkSheet.get_Range("a1", "b1");
            xlRange.Merge(false);
            xlRange.Value = "Material Name";
            xlRange.HorizontalAlignment = 3;
            xlRange.Font.Bold = true;
            xlRange.Font.Size = 16;

I have a scenario where a client could have either Excel 2007 installed or Excel 2010 installed, I need to be able to export and import excel files that they create, OLEDB isn't an option so I was going to use Interop services.

Question is when I coded for 2010 it didn't work for 2007 and vice versa how can I achieve this?

Can I import both references somehow and define Excel depending on the version installed?

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range xlRange;

            object misValue = System.Reflection.Missing.Value;

            pbarExcelGenerate.Visible = true;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "Material Results";

            pbarExcelGenerate.Value = 5;


            xlRange = xlWorkSheet.get_Range("a1", "b1");
            xlRange.Merge(false);
            xlRange.Value = "Material Name";
            xlRange.HorizontalAlignment = 3;
            xlRange.Font.Bold = true;
            xlRange.Font.Size = 16;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-12-17 16:51:51

不,你不能...您可以同时使用较低版本...或者使用根本不需要安装 Excel 的第 3 方库(免费或商业),或者您可以使用原始 COM 来访问 Excel 会不太舒服,但允许这样的事情。

编辑 - 根据评论(感谢 @Rup):

您可以在 http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=23656 应该附带2007 年的 Interop 库

no you can't... you can either use the lower version for both... or use a 3rd-party library (free or commercial) which doesn't need Excel to be installed at all or you can use raw COM to access Excel which would be much less comfortable but allows for such things.

EDIT - as per comments (thanks to @Rup):

You can download the older version of VSTO at http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=23656 which should come with the Interop library for 2007

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文