using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ramitta { public static class Excel { public static List> ReadExcelAsDictRow( string filePath, object sheetName = null, List headerInit = null) { var result = new List>(); // 打开 Excel 文件 using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var workbook = new XSSFWorkbook(fs); ISheet sheet = null; // 判断 sheetName 类型 if (sheetName is string sheetNameStr) { sheet = workbook.GetSheet(sheetNameStr); // 根据工作表名称获取 } else if (sheetName is int sheetNameInt) { sheet = workbook.GetSheetAt(sheetNameInt); // 根据索引获取 } else { sheet = workbook.GetSheetAt(0); // 默认获取第一个工作表 } // 如果没有提供表头,则默认使用第一行作为表头 List header = null; if (headerInit != null && headerInit.Count > 0) { header = headerInit; // 使用提供的表头 } else { var headerRow = sheet.GetRow(0); if (headerRow == null) return result; // 如果第一行为空,直接返回空列表 header = new List(); for (int i = 0; i < headerRow.Cells.Count; i++) { var cell = headerRow.GetCell(i); header.Add(cell?.ToString().Trim() ?? $"Column_{i}"); // 处理空单元格 } } // 如果提供了表头数据,则从第二行开始读取数据 int startRowIndex = headerInit != null && headerInit.Count > 0 ? 0 : 1; // 遍历每一行数据 for (int rowIndex = startRowIndex; rowIndex <= sheet.LastRowNum; rowIndex++) // 从第二行开始 { var row = sheet.GetRow(rowIndex); if (row == null || IsRowEmpty(row)) continue; // 跳过空行 var rowDict = new Dictionary(); // 遍历每一列 for (int colIndex = 0; colIndex < header.Count; colIndex++) { var cell = row.GetCell(colIndex); rowDict[header[colIndex]] = cell?.ToString().Trim() ?? ""; // 直接转换为字符串 } result.Add(rowDict); } } return result; } public static Dictionary> ReadExcelAsDictCol( string filePath, object sheetName = null, List headerInit = null) { var result = new Dictionary>(); // 打开 Excel 文件 using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var workbook = new XSSFWorkbook(fs); ISheet sheet = null; // 如果 sheetName 不为 null,按名字找到对应的工作表 if (sheetName != null && sheetName is string sheetNameStr) { sheet = workbook.GetSheet(sheetNameStr); // 根据工作表名称获取 } else { sheet = workbook.GetSheetAt(0); // 默认第一个工作表 } if (sheet == null) return result; // 如果没有找到工作表,直接返回空字典 // 如果 headerInit 不为 null,使用它来初始化表头,否则从 Excel 中的第一行获取 var header = headerInit ?? new List(); if (header.Count == 0) { // 获取第一行作为键(headers) var headerRow = sheet.GetRow(0); if (headerRow == null) return result; // 如果第一行为空,直接返回空字典 for (int i = 0; i < headerRow.Cells.Count; i++) { var cell = headerRow.GetCell(i); string headerText = cell?.ToString().Trim() ?? $"Column_{i}"; // 处理空单元格 header.Add(headerText); } } // 初始化每一列对应的 List foreach (var headerText in header) { result[headerText] = new List(); } // 从第一行开始遍历数据,而不是从第二行开始 for (int rowIndex = headerInit == null ? 1 : 0; rowIndex <= sheet.LastRowNum; rowIndex++) // 修改这里,开始从第0行 { var row = sheet.GetRow(rowIndex); if (row == null || IsRowEmpty(row)) continue; // 跳过空行 // 遍历每一列数据并添加到对应的 List 中 for (int colIndex = 0; colIndex < header.Count; colIndex++) { var cell = row.GetCell(colIndex); string cellValue = cell?.ToString().Trim() ?? ""; // 直接转换为字符串 if (!string.IsNullOrEmpty(cellValue)) { result[header[colIndex]].Add(cellValue); } } } } return result; } private static bool IsRowEmpty(IRow row) { foreach (var cell in row.Cells) { if (cell != null && !string.IsNullOrEmpty(cell.ToString().Trim())) return false; } return true; } } }