Last active
October 23, 2020 16:07
-
-
Save chuongmep/70bc50b50d7c0727e1d8d64e96e7a960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<List<string>> ReadExcelByType(string filePath, string worksheetName, DataStorage type, | |
int Index) | |
{ | |
List<List<string>> data = new List<List<string>>(); | |
ExcelPackage _excelPackage; | |
if (File.Exists(filePath)) | |
{ | |
FileInfo file = new FileInfo(filePath); | |
_excelPackage = new ExcelPackage(file); | |
} | |
else | |
{ | |
return data; | |
} | |
if (_excelPackage == null) | |
return data; | |
if (!_excelPackage.Workbook.Worksheets.Select(x => x.Name).Contains(worksheetName)) | |
return data; | |
ExcelWorksheet worksheet = _excelPackage.Workbook.Worksheets[worksheetName]; | |
//check if the worksheet is completely empty | |
if (worksheet.Dimension == null) | |
return data; | |
if (type == DataStorage.ByColumn) | |
{ | |
int rows = worksheet.Dimension.Rows; | |
int columns = worksheet.Dimension.Columns; | |
for (int i = Index; i <= rows; i++) | |
{ | |
List<string> rowData = new List<string>(); | |
for (int j = Index; j <= columns; j++) | |
rowData.Add(worksheet.Cells[i, j].Text); | |
data.Add(rowData); | |
} | |
} | |
else | |
{ | |
//sheet range | |
int rows = worksheet.Dimension.Columns; | |
int columns = worksheet.Dimension.Rows; | |
for (int i = Index; i <= rows; i++) | |
{ | |
List<string> rowData = new List<string>(); | |
for (int j = Index; j <= columns; j++) | |
// MessageBox.Show(worksheet.Cells[i, j].Text); | |
rowData.Add(worksheet.Cells[j, i].Text); | |
data.Add(rowData); | |
} | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment