工作中用到 使用XSSFWorkbook 读取excel 具体代码如下,
private (List<(string columnName, string colomnDescription)> headers, List<IList<string>> rows) ParseExcel(string excelPath, string sheetName)
{
var stream = File.OpenRead(excelPath);
using (stream)
{
var workbook = new XSSFWorkbook(stream);
var sheet = workbook.GetSheet(sheetName);
var rows = new List<IList<string>>();
var headers = new List<(string columnName, string colomnDescription)>();
var rowLimit = 10000;
var columnLimit = 100;
var firstRow = sheet.GetRow(0);
for (int k = 0; k < columnLimit; k++)
{
var cell = firstRow.GetCell(k);
if (IsEndCell(cell)) break;
var header = ParseHeader(cell.ToString());
headers.Add(header);
}
for (var i = 1; i < rowLimit; i++)
{
var row = sheet.GetRow(i);
if (IsEndRow(row)) break;
var rowData = new List<string>();
rows.Add(rowData);
for (int j = 0; j < columnLimit; j++)
{
var cell = row.GetCell(j);
if (IsEndCell(cell)) break;
rowData.Add(cell.ToString());
}
}
return (headers, rows);
}
}
private (string columnName, string colomnDescription) ParseHeader(string rawHeader)
{
var regex = new Regex(@"^s*([^()()]+)(?:(|()([^()()]+)(?:)|))s*$");
var match = regex.Match(rawHeader);
var columnName = match.Groups[1].Value;
var columnDescription = match.Groups[2].Value;
if (string.IsNullOrEmpty(columnName) || string.IsNullOrEmpty(columnDescription))
{
throw new Exception($"header {rawHeader} invalid");
}
return (columnName.Trim(), columnDescription.Trim());
}
private bool IsEndRow(IRow row)
{
if (row?.Cells == null) return true;
if (!row.Cells.Any()) return true;
if (string.IsNullOrEmpty(row.Cells[0]?.ToString())) return true;
return false;
}
private bool IsEndCell(ICell cell)
{
return string.IsNullOrEmpty(cell?.ToString());
}