Read data from excel into the object of DataSet.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;
using System.IO;
using System.Data;

namespace ReadExcelData
{
class Program
{
static void Main(string[] args)
{
string filePath = @"E:\WorkDocuments\TestD\Book1.xlsx";
ReadDataFromExcel(filePath);
}

public static void ReadDataFromExcel(string filePath)
{
FileStream stream = new FileStream(filePath, FileMode.Open);

IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(stream);

DataSet result = excelReader2007.AsDataSet();

// Data Reader methods
foreach (DataTable table in result.Tables)
{
for (int i = 0; i < table.Rows.Count; i++ )
{
for (int j = 0; j < table.Columns.Count; j++ )
{
Console.Write("\"" + table.Rows[i].ItemArray[j] + "\";");

}
Console.WriteLine();
}
}
excelReader2007.Close();
Console.Read();

}
}
}

原文地址:https://www.cnblogs.com/tianjinquan/p/2210090.html