阅读使用EPPlus的Excel spreasheet(Reading Excel spreashe

2019-06-26 13:29发布

有人能指出我如何通过所有的行和列读取Excel的spreasheet,环中检索使用EPPlus和MVC值正确的方向? 所以车费我看到的例子来创建一个spreasheet,但没有发现任何在打开一个Excel文件,并从中读取值。 任何帮助,将不胜感激。

TIA苏..

Answer 1:

简单的例子

// 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
    var workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count > 0)
        {
            // Get the first worksheet
            var currentWorksheet = workBook.Worksheets.First();

            // read some data
            object col1Header = currentWorksheet.Cells[1, 1].Value;


Answer 2:

一个简单的例子,你如何读取Excel文件在.NET 4.5使用EPPlus

public void readXLS(string FilePath)
{
    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 colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;     //get row count
        for (int row = 1; row <= rowCount; row++)
        {
            for (int col = 1; col <= colCount; col++)
            {
                Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
            }
        }
    }
}


文章来源: Reading Excel spreasheet using EPPlus