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.