excelize icon indicating copy to clipboard operation
excelize copied to clipboard

Get CountRows from sheet

Open ivolkoff opened this issue 3 years ago • 6 comments

Description

I need to know the number of lines, and I'm ready to offer a solution. Is this idea okay?

ivolkoff avatar Nov 25 '22 13:11 ivolkoff

There are two kinds of methods to get total rows in the worksheet, for example, get the length of the trows by the GetRows function for small data scale:

f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
    return
}
defer func() {
    if err := f.Close(); err != nil {
        fmt.Println(err)
    }
}()
rows, err := f.GetRows("Sheet1")
if err != nil {
    return
}
fmt.Println("total rows:", len(rows))

For better performance, please use the row iterator:

f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
    return
}
defer func() {
    if err := f.Close(); err != nil {
        fmt.Println(err)
    }
}()
var totalRows int
rows, err := f.Rows("Sheet1")
if err != nil {
    return
}
for rows.Next() {
    totalRows++
}
if err = rows.Close(); err != nil {
    fmt.Println(err)
}
fmt.Println("total rows:", totalRows)

xuri avatar Nov 25 '22 14:11 xuri

Thanks for the answer, the proposed options work very slowly. I did faster and ran tests. Please take a look at my solution.

https://github.com/qax-os/excelize/pull/1403

ivolkoff avatar Nov 25 '22 15:11 ivolkoff

Thanks for your pull request, I've review it later.

xuri avatar Nov 25 '22 15:11 xuri

What about read TotalRows from dimension?

image

image

eaglexiang avatar Mar 09 '23 06:03 eaglexiang

What about read TotalRows from dimension?

This is optional and is not required. https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sheetdimension?view=openxml-2.8.1#remarks

ivolkoff avatar Mar 09 '23 07:03 ivolkoff

What about read TotalRows from dimension?

This is optional and is not required. https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sheetdimension?view=openxml-2.8.1#remarks

What a pity :(

eaglexiang avatar Mar 09 '23 09:03 eaglexiang