如何在c#中阅读excel 2007

-1 c# asp.net

我的服务器没有任何Microsoft Office,我也不想安装Microsoft Office.

当我使用此代码读取Excel 2007文件时

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;
Extended Properties="Excel 8.0;HDR=YES";
Run Code Online (Sandbox Code Playgroud)

它无法读取Excel文件.有谁知道什么是错的?

Mik*_*keM 5

您可以使用EPPlus

EPPlus是一个.net库,使用Open Office Xml格式(xlsx)读取和写入Excel 2007/2010文件.

不必安装MS Office.

用于打开的示例脚本.xlsx:

using OfficeOpenXml;
Run Code Online (Sandbox Code Playgroud)
// Get the file we are going to process
var existingFile = new FileInfo(filePath);
// Open and read the XlSX file.
using (var package = new ExcelPackage(existingFile))
{
    // Get the work book in the file
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count > 0)
        {
            // Get the first worksheet
            ExcelWorksheet currentWorksheet = workBook.Worksheets.First();

            // read some data
            object col1Header = currentWorksheet.Cells[0, 1].Value;
            ...
Run Code Online (Sandbox Code Playgroud)

代码示例来自:http://blog.fryhard.com/archive/2010/10/28/reading-xlsx-files-using-c-and-epplus.aspx