【问题标题】:How to read in Excel file in Win7 64bit?如何在 Win7 64bit 中读取 Excel 文件?
【发布时间】:2010-04-17 10:45:45
【问题描述】:

我有一个已移动到 64 位计算机的 c# 应用程序。此应用程序读入 Excel 文件以获取一些数据输入。我想将此项目构建为 64 位。有没有办法让我的程序读入这个文件?我很难相信没有办法使用 Excel 文件作为 64 位应用程序的输入。我已经安装了 Office 2010 64 位以及 2010 Office System Driver Beta: Data Connectivity Components,但没有成功。我确定我只是错过了一些非常简单的东西。

谢谢!! 比尔

【问题讨论】:

    标签: 64-bit office-2010


    【解决方案1】:

    Windows x64 不支持 Jet OLEDB 驱动程序。相反,您可以使用 Office 互操作库。添加对Microsoft.Office.Interop.Excel 程序集的引用并尝试以下代码:

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Interop.Excel;
    
    class Program
    {
        static void Main()
        {
            var file = @"C:\work\test.xlsx";
            var excel = new ApplicationClass();
            var workbook = excel.Workbooks.Open(file);
            var worksheet = (_Worksheet)workbook.Worksheets.Item[1];
    
            // read the value of the first row, first column
            var value = ((Range)worksheet.Cells[1, 1]).Value;
            Console.WriteLine(value);
    
            workbook.Close(false, file, null);
            Marshal.ReleaseComObject(workbook);
        }
    }
    

    请注意,您需要安装 Excel。

    【讨论】:

    • 非常感谢达林。我一定会试一试的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多