C# EPPlus OpenXML 计数行

发布于 2024-10-13 05:24:05 字数 223 浏览 3 评论 0原文

对于 EPPlus 和 OpenXML,有人知道如何计算行数的语法吗?

假设我的工作表名为“worksheet”

int numberRows = worksheet.rows.count()?或 worksheet.rows.dimension

我当然对答案感兴趣,但如何找到答案会很酷,例如“转到定义”并查找这个或那个,等等。

With EPPlus and OpenXML does anyone know the syntax on how to count the rows?

Say my worksheet is called "worksheet"

int numberRows = worksheet.rows.count()? or worksheet.rows.dimension

I'm certainly interested in the answer, but how to find the answer would be cool to, like "Go to definition" and look for this or that, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┈┾☆殇 2024-10-20 05:24:05

使用名为 worksheet 的工作表对象,worksheet.Dimension.Start.Rowworksheet.Dimension.End.Row 应该为您提供所需的信息。

worksheet.Dimension.Address 将为您提供一个字符串,其中包含传统 Excel 范围格式的工作表维度(例如,第 1-5 行、第 1-9 列为“A1:I5”)。

有一个可用的文档文件。在许多情况下,使用库并以这种方式找到答案可能同样快。 EPPlus 似乎设计得很好 - 至少一切似乎都有逻辑命名。

With a worksheet object called worksheet, worksheet.Dimension.Start.Row and worksheet.Dimension.End.Row should give you the information you need.

worksheet.Dimension.Address will give you a string containing the worksheet dimensions in the traditional Excel range format (e.g. 'A1:I5' for rows 1-5, columns 1-9).

There is a documentation file available. In many cases it might be just as quick to play around with the library and find the answer that way. EPPlus seems to be well designed - everything seems to be logically named, at least.

无妨# 2024-10-20 05:24:05

谢谢你的提示,Quppa。我用它来从工作簿电子表格填充数据表,如下所示:

    /// <summary>
    /// Converts a Worksheet to a DataTable
    /// </summary>
    /// <param name="worksheet"></param>
    /// <returns></returns>
    private static DataTable WorksheetToDataTable(ExcelWorksheet worksheet)
    {
        // Vars
        var dt = new DataTable();
        var rowCnt = worksheet.Dimension.End.Row;
        var colCnt = worksheet.Dimension.End.Column + 1;

        // Loop through Columns
        for (var c = 1; c < colCnt; c++ )
        {
            // Add Column
            dt.Columns.Add(new DataColumn());

            // Loop through Rows
            for(var r = 1; r < rowCnt; r++ )
            {
                // Add Row
                if (dt.Rows.Count < (rowCnt-1)) dt.Rows.Add(dt.NewRow());

                // Populate Row
                dt.Rows[r - 1][c - 1] = worksheet.Cells[r, c];
            }
        }

        // Return
        return dt;
    }

Thanks for that tip Quppa. I used it in my bid to populate a DataTable from a Workbook Spreadsheet as below:

    /// <summary>
    /// Converts a Worksheet to a DataTable
    /// </summary>
    /// <param name="worksheet"></param>
    /// <returns></returns>
    private static DataTable WorksheetToDataTable(ExcelWorksheet worksheet)
    {
        // Vars
        var dt = new DataTable();
        var rowCnt = worksheet.Dimension.End.Row;
        var colCnt = worksheet.Dimension.End.Column + 1;

        // Loop through Columns
        for (var c = 1; c < colCnt; c++ )
        {
            // Add Column
            dt.Columns.Add(new DataColumn());

            // Loop through Rows
            for(var r = 1; r < rowCnt; r++ )
            {
                // Add Row
                if (dt.Rows.Count < (rowCnt-1)) dt.Rows.Add(dt.NewRow());

                // Populate Row
                dt.Rows[r - 1][c - 1] = worksheet.Cells[r, c];
            }
        }

        // Return
        return dt;
    }
追我者格杀勿论 2024-10-20 05:24:05

我正在使用 4.1 版本,看起来他们添加了一些属性(在之前答案的评论中提到)以使这更容易。

string Filepath = "c:\excelfile.xlsx";
FileInfo importFileInfo = new FileInfo(FilePath);
using(var excelPackage = new ExcelPackage(importFileInfo))
{
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
    int rowCount = worksheet.Dimension.Rows;
    int colCount = worksheet.Dimension.Columns;
}

I am working with version 4.1 and it looks like they have added some properties (mentioned in comments from previous answers) to make this easier.

string Filepath = "c:\excelfile.xlsx";
FileInfo importFileInfo = new FileInfo(FilePath);
using(var excelPackage = new ExcelPackage(importFileInfo))
{
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];
    int rowCount = worksheet.Dimension.Rows;
    int colCount = worksheet.Dimension.Columns;
}
初雪 2024-10-20 05:24:05

非常简单:

private int GetDimensionRows(ExcelWorksheet sheet)
{
    var startRow = sheet.Dimension.Start.Row;
    var endRow = sheet.Dimension.End.Row;
    return endRow - startRow;
}

Quite easy with:

private int GetDimensionRows(ExcelWorksheet sheet)
{
    var startRow = sheet.Dimension.Start.Row;
    var endRow = sheet.Dimension.End.Row;
    return endRow - startRow;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文