EPPlus - Read Excel Table

后端 未结 6 2024
一个人的身影
一个人的身影 2020-12-08 14:20

Using EPPlus, I want to read an excel table, then store all the contents from each column into its corresponding List. I want it to recognize the table\'s headi

6条回答
  •  死守一世寂寞
    2020-12-08 15:26

    Not sure why but none of the above solution work for me. So sharing what worked:

    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());
                }
            }
        }
    }
    

提交回复
热议问题