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

SetCellValue() and MergeCell() are very slowly when populate 10,000 rows #1349

Open
xuzue opened this issue Sep 15, 2022 · 2 comments
Open

Comments

@xuzue
Copy link

xuzue commented Sep 15, 2022

Description

SetCellValue() and MergeCell() are very slowly when populate a large number of cells if style value is bigger than 0

Describe the results you received:

when SetCellValue() or MergeCell() is called, the prepareCellStyle() method will loop all rows in sheet everytime. So when populate a large number of cells about 450,000 cells that there are 10,000 rows, the span time is very very long.

Describe the results you expected:

prepareCellStyle() need return the style value directly if parameter "style" is bigger than 0.

Output of go version:

1.18.4

Excelize version or commit ID:

v2.6.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):

@xuri
Copy link
Member

xuri commented Sep 15, 2022

The prepareCellStyle function gets rows style by one-time addressing when it has been called. When setting the cell's value, we need to know if the cell's style should be inherited from a row or column's style. Currently, which returns the style ID directly if given a specific style ID.

@elias506
Copy link

elias506 commented Nov 14, 2023

The problem is resolved in the latest version of the release


I propose a temporary but very effective solution to this problem.
Before you use SetCellStr or any other function to insert a value into a cell, use this call: SetRowStyle("sheet_name", 1, 1, styleID). With this, the insert function will not go through every value of every line in the file. It is necessary to set the style specifically for the first line.
In this option, all subsequent cells will be filled with the provided style. Your cells should not be styled previously! If you need to change the style of subsequent inserted cells, simply call a new SetRowStyle("sheet_name", 1, 1, newStyleID) with the new style.

Note that you need to pass a non-zero styleID.

Remember that you can use the SetCellStyle function to overwrite the old style.

It is also worth noting that such a problem does not exist when using StreamWriter. If you have roughly 10000 lines or more it's always worth using StreamWriter I think. It gives too much of a performance boost.

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