Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Oct 01, 2021 12:52 pm

Hello.

It seems that all rows of a newly created worksheet are automatically considered as used rows, and the same goes for columns, when running the following code sample:
Code: Select all
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add("Test");

int lastrow = worksheet.LastRow; // Returns 65536
int lastColumn = worksheet.LastColumn; // Returns 256

So, once I have created my worksheet, I want to add an array of placeholder values for unit testing purpose:
Code: Select all
private static Worksheet SetupWorksheet(Workbook workbook, int numberOfRowsToInsert)
{
    Worksheet worksheet = workbook.Worksheets.Add("Test");

    CellRange firstRow = worksheet.Rows[0];
    CellRange firstCell = firstRow.Columns[0];
    CellRange secondCell = firstRow.Columns[1];
    CellRange thirdCell = firstRow.Columns[2];
    CellRange fourthCell = firstRow.Columns[3];
    firstCell.Value = "Column1";
    secondCell.Value = "Column2";
    thirdCell.Value = "Column3";
    fourthCell.Value = "Column4";

    int maxRowsIndex = numberOfRowsToInsert - 1;
    int maxColumnsIndex = fourthCell.Column - 1;
    var objs = new object[maxRowsIndex, maxColumnsIndex];
    for (int i = 0; i < maxRowsIndex; i++)
    {
        for (int j = 0; j < maxColumnsIndex; j++)
        {
            objs[i, j] = i + j;
        }
    }

    worksheet.InsertArray(objs, firstRow.Row + 1, firstCell.Column);
    return worksheet;
}

public static void Main()
{
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets.Add("Test");

    int lastrow = worksheet.LastRow; // Returns 65536
    int lastColumn = worksheet.LastColumn; // Returns 256

    Worksheet worksheet = SetupWorksheet(workbook, 100);

    int lastrow = worksheet.LastRow; // Still returns 65536
    int lastColumn = worksheet.LastColumn; // Still returns 256
}

Because of this, I can't seem to get the last used rows/columns when feeding a newly created worksheet with data.

However, I noticed that adding text with the "Text" property to any cell will correctly set the "LastRow" and "LastColumn" properties:
Code: Select all
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add("Test");

int lastrow = worksheet.LastRow; // Returns 65536
int lastColumn = worksheet.LastColumn; // Returns 256

worksheet.CellList[0].Text = "Test";

int lastrow = worksheet.LastRow; // Returns 1
int lastColumn = worksheet.LastColumn; // Returns 1

I've tried creating the workbook with the default Excel version and with the Excel 2016 version but to no avail.

Is it a misunderstanding from me of the way Spire.XLS works or is it a bug?

By the way, I'm using the "Spire.XLS Pro Edition" license.

olefebvre
 
Posts: 13
Joined: Wed May 26, 2021 11:36 am

Mon Oct 04, 2021 5:59 am

Hello,

Thanks for your inquiry.
Based on your code, I did some tests and noticed this issue. I have logged it in our bug tracking system with the ticket SPIREXLS-3480. I will inform you as soon as there is any update. Apologize for the inconvenience caused.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Mon Oct 25, 2021 10:13 am

Hello,

Thanks for your patient waiting.
Glad to tell you that we just released Spire.XLS Pack(Hotfix) Version:11.10.4, which fixes the issue of ticket SPIREXLS-3480. Welcome to download the new version from the following links and refer to the sample code for testing.

Website link: https://www.e-iceblue.com/Download/down ... t-now.html
NuGet link: https://www.nuget.org/packages/Spire.XLS/11.10.4

Sample code to get the used rows and columns:

Code: Select all
int lastrow = worksheet.LastDataRow;
int lastColumn = worksheet.LastDataColumn;
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Mon Nov 08, 2021 1:47 pm

Hello,

Yes, the problem has been solved by using these two new properties.
Thanks.

olefebvre
 
Posts: 13
Joined: Wed May 26, 2021 11:36 am

Tue Nov 09, 2021 1:26 am

Hi,

Thanks for your feedback.
Glad to hear that your problem has been resolved now. If you encounter any issues in the furture, just feel free to contact us.
Have a nice day!
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.XLS