Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get CountRows from sheet #1402

Open
ivolkoff opened this issue Nov 25, 2022 · 6 comments
Open

Get CountRows from sheet #1402

ivolkoff opened this issue Nov 25, 2022 · 6 comments

Comments

@ivolkoff
Copy link

Description

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

@xuri
Copy link
Member

xuri commented Nov 25, 2022

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)

ivolkoff added a commit to ivolkoff/excelize that referenced this issue Nov 25, 2022
@ivolkoff
Copy link
Author

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

#1403

@xuri
Copy link
Member

xuri commented Nov 25, 2022

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

ivolkoff added a commit to ivolkoff/excelize that referenced this issue Nov 25, 2022
add close file and benchmark
ivolkoff added a commit to ivolkoff/excelize that referenced this issue Nov 25, 2022
ivolkoff added a commit to ivolkoff/excelize that referenced this issue Nov 25, 2022
@eaglexiang
Copy link
Contributor

What about read TotalRows from dimension?

image

image

@ivolkoff
Copy link
Author

ivolkoff commented Mar 9, 2023

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

@eaglexiang
Copy link
Contributor

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 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants