How to read in Excel file in Win7 64bit?
I have a c# application that I have moved to a 64bit machine. This application reads in an Excel file for some data input. I would like to build this project as 64bit. Is there any way to have my program read in this file? I find it hard to believe that there is no way to use and Excel file as input into a 64bit app. I have installed Office 2010开发者_运维知识库 64 bit as well as the 2010 Office System Driver Beta: Data Connectivity Components with no luck. I'm sure that I'm just missing something really simple.
thanks!! Bill
The Jet OLEDB driver is not supported on Windows x64. Instead you could use the Office Interop library. Add reference to the Microsoft.Office.Interop.Excel
assembly and try the following code:
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);
}
}
Note that you will need to have Excel installed.
精彩评论