• 使用XSSFWorkbook 读取excel


    工作中用到 使用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());
    }

  • 相关阅读:
    javaweb学习总结二十六(response对象的用法二 下载文件)
    javaweb学习总结二十五(response对象的用法一)
    线程池的使用
    javaweb学习总结二十四(servlet经常用到的对象)
    javaweb学习总结二十三(servlet开发之线程安全问题)
    创建与删除索引
    Oracle中exists与in的区别
    win7安装IIS及将网站发布到IIS上
    C# toolstrip 上添加DateTimePicker Control控件
    用java实现zip压缩
  • 原文地址:https://www.cnblogs.com/xiaohouye/p/11454982.html
Copyright © 2020-2023  润新知