123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- namespace Universal;
- public class ExcelReader
- {
- public static Dictionary<string, Dictionary<int, Dictionary<int, string>>> Read(string filePath, int startRow)
- {
- Dictionary<string, Dictionary<int, Dictionary<int, string>>> sheets = [];
-
- using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
- for (int i = 0; ; i++)
- {
- ISheet sheet;
- try
- {
- sheet = new XSSFWorkbook(file).GetSheetAt(i);
- if (sheet is null)
- break;
- }
- catch
- {
- break;
- }
- Dictionary<int, Dictionary<int, string>> contents = [];
- sheets[sheet.SheetName] = contents;
- for (int row = startRow; row <= sheet.LastRowNum; row++)
- {
- if (sheet.GetRow(row) is not IRow currentRow)
- continue;
- contents[row] = [];
- for (int colum = 0; colum < currentRow.LastCellNum; colum++)
- {
- ICell cell = currentRow.GetCell(colum);
- contents[row][colum] = cell?.ToString() ?? string.Empty;
- }
- }
- }
- return sheets;
- }
- }
|