Reading Excel spreasheet using EPPlus

前端 未结 2 1088
小鲜肉
小鲜肉 2020-12-09 16:54

Could someone point me in the right direction on how to read a Excel spreasheet, loop through all the rows and columns to retreive value using EPPlus and MVC? So fare I see

相关标签:
2条回答
  • 2020-12-09 17:21

    Simple example

    // 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;
    
    0 讨论(0)
  • 2020-12-09 17:27

    A simple example how you can read excel file using EPPlus in .net 4.5

    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());
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题