C# 使用后期绑定读取 Excel

发布于 2024-10-08 12:33:40 字数 426 浏览 0 评论 0原文

你好 我以前没有使用过后期绑定,但这似乎是解决方案,如果我能找到一个简洁的例子就好了!

或者可能这不是解决方案,但我相信你们会知道!

我需要从 Excel 中的一列填充下拉组合框列表,一直读到第一个空白单元格。该解决方案需要与 Excel 2003 及以上版本一起使用,有些电脑从未安装过 2003,仅安装了 Office 2010,其他电脑已从 2003 升级,有些电脑仍在 2003!

我需要一个适用于上述所有问题的解决方案。

所以我正在研究后期绑定,这是正确的方法吗? Linq 会帮忙吗?

它是一个使用 .Net 4 的经典 Windows 窗体应用程序。

我想我应该编写一个方法,该方法获取文件名和路径并返回一个列表,然后将其分配给组合框。

但作为新人,我还没有获得通过!

请提供任何帮助/示例

Hi
I have not used late binding before but it would seem to be the solution, if only I could find a concise example!

Or may be it's not the solution but I'm sure you guys will know!

I need to fill a dropdown combbox list from a column in excel reading down to the first blank cell. the solution needs to work with excel 2003 and above some PCs never have had 2003 install only office 2010 other have been upgraded from 2003 and some are still on 2003!

I need a solution that works on all of the above.

So I'm looking into late binding is this the correct way to go? would Linq help!?

Its a clasic windows Form app using .Net 4.

I thought I would write a method that takes the file name and path and returns a list which I would then assign to the combobox.

But being new I'm not getting pass go!

Any help/examples PLEASE

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

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

发布评论

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

评论(1

丑疤怪 2024-10-15 12:33:40

听起来您正在考虑将 COM 互操作/自动化与安装在客户端计算机上的 Excel 应用程序一起使用。

如果您的唯一要求是从 Excel 文件中提取数据,那么您最好使用可以简单地从文件本身读取数据的库,而不是在后台启动 Excel 进程。

它更快、更干净、更易于测试。

我已经将 NPOI 用于 .xls 文件(当然还有其他文件),并且有很多选项.xlsx 文件。 这个问题是关于创建文件的,但是建议的库当然也可以读取文件。

我唯一一次使用 COM 自动化是与正在运行的 Excel 实例交互。

编辑(回应评论)

以下是获取 B 列值作为字符串的示例:

using System;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var stream = new FileStream(@"c:\my_workbook.xls", FileMode.Open);
            var workbook = new HSSFWorkbook(stream);
            stream.Close();

            var sheet = workbook.GetSheet("My Sheet Name");
            var row_enumerator = sheet.GetRowEnumerator();

            while (row_enumerator.MoveNext())
            {
                var row = (Row)row_enumerator.Current;
                var cell = row.GetCell(1); // in Excel, indexes are 1-based; in NPOI the indexes are 0-based
                Console.WriteLine(cell.StringCellValue);
            }

            Console.ReadKey();
        }
    }
}

It sounds like you're looking at using COM interop/automation with the Excel application installed on client machines.

If your sole requirement is to extract data from an Excel file, you'll be better off using a library that can simply read data out of the file itself, rather than launching the Excel process in the background.

It is faster, cleaner, and more testable.

I've used NPOI for .xls files (and there are certainly others), and there are LOTS of options for .xlsx files. This SO question is about creating a file, but any of the suggested libraries can of course read files as well.

The only time I'd use COM automation is to interact with a running instance of Excel.

Edit (in response to comments)

Here is a sample of getting the values of column B as strings:

using System;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var stream = new FileStream(@"c:\my_workbook.xls", FileMode.Open);
            var workbook = new HSSFWorkbook(stream);
            stream.Close();

            var sheet = workbook.GetSheet("My Sheet Name");
            var row_enumerator = sheet.GetRowEnumerator();

            while (row_enumerator.MoveNext())
            {
                var row = (Row)row_enumerator.Current;
                var cell = row.GetCell(1); // in Excel, indexes are 1-based; in NPOI the indexes are 0-based
                Console.WriteLine(cell.StringCellValue);
            }

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