开发者

How to read excel file where office is not installed(C#3.0, dotnet 3.5)

Hi I am facing a problem.

In my server, there is no office installed. However, I need to access data from excel file.

I used Microsoft.Office.Interop.Excel dll file. I am under the impression that this will work

because the dll location is

C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

The same is available in the server machine also. But it does not have office installed there

But when I ran the program I got the exception

System Exception:S开发者_Python百科ystem.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.

Googling does not provide much support.

Also, it is very urgent.

Kindly help.


The Office PI assemblies simply wrap the Office COM components to provide an interface that can be called from .NET managed code. You still need Office installed, however, you have other options...

If you are working with Office 2007 files, you could try the Open XML SDK 2.0 for Microsoft Office.

Alternatively, if you are working with files form an earlier version of Office there are third party libraries available, e.g. SpreadsheetGear.


If you're using Excel & C#, try http://epplus.codeplex.com/

Free library that can read/write spreadsheets, very very easy to use with Excel.

Example of reading in Excel files as demanded by OP:

FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
    // get the first worksheet in the workbook
    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
    int col = 2; //The item description
    // output the data in column 2
    for (int row = 2; row < 5; row++)
        Console.WriteLine("\tCell({0},{1}).Value={2}", row, col, worksheet.Cells[row, col].Value);

    // output the formula in row 5
    Console.WriteLine("\tCell({0},{1}).Formula={2}", 3, 5, worksheet.Cells[3, 5].Formula);                
    Console.WriteLine("\tCell({0},{1}).FormulaR1C1={2}", 3, 5, worksheet.Cells[3, 5].FormulaR1C1);

} // the using statement automatically calls Dispose() which closes the package.

PS: Asking with please/thanks will likely get more people to help ;)


You're stuffed basically. The interop file simply points to a COM object which is registered by Excel when it is installed.

Since you don't have Excel, its COM registration won't be in the registry so the interop file is effectively pointing at a broken link hence you get the COMException.

You'll need to install Office to make this works as it stands.


Depending on your needs, NPOI may do the work (it does not requires office to be installed).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜