namespace ConsoleApp17
{
public class WriteUsingLightCellsAPI
{
public static void Run()
{
// The path to the documents directory.
var fileName = $“F:\temp\{DateTime.Now:yyyyMMHHmmss}.xls”;
// Specify your desired matrix
int rowsCount = 10000;
int colsCount = 30;
var workbook = new Workbook();
var saveOptions = new XlsSaveOptions(); // when use XlsSaveOptions PutValue("") throw error
//var saveOptions = new OoxmlSaveOptions();
saveOptions.LightCellsDataProvider = new TestDataProvider(workbook, rowsCount, colsCount);
workbook.Save(fileName, saveOptions);
}
}
class TestDataProvider : LightCellsDataProvider
{
private int _row = -1;
private int _column = -1;
private int maxRows;
private int maxColumns;
private Workbook _workbook;
public TestDataProvider(Workbook workbook, int maxRows, int maxColumns)
{
this._workbook = workbook;
this.maxRows = maxRows;
this.maxColumns = maxColumns;
}
#region LightCellsDataProvider Members
public bool IsGatherString()
{
return false;
}
public int NextCell()
{
++_column;
if (_column < this.maxColumns)
return _column;
else
{
_column = -1;
return -1;
}
}
public int NextRow()
{
++_row;
if (_row < this.maxRows)
{
_column = -1;
return _row;
}
else
return -1;
}
public void StartCell(Cell cell)
{
if (_column == 2)
cell.PutValue(""); // when use XlsSaveOptions PutValue("") throw error
else
cell.PutValue(_row + _column);
if (_row == 1)
{
}
else
{
cell.Formula = "=Rand() + A2";
}
}
public void StartRow(Row row)
{
}
public bool StartSheet(int sheetIndex)
{
if (sheetIndex == 0)
{
return true;
}
else
return false;
}
#endregion
}
The exception “Object reference not set to an instance of an object” typically occurs when you attempt to access a member of an object that has not been initialized. In your case, it seems to happen when you call cell.PutValue("") in the StartCell method of your TestDataProvider class.
To resolve this issue, you should ensure that the cell object is properly initialized before you attempt to use it. Here are a few suggestions to help you troubleshoot and fix the problem:
Check for Null Values: Before calling cell.PutValue(""), you can add a null check to ensure that the cell object is not null. For example:
public void StartCell(Cell cell)
{
if (cell == null)
{
throw new ArgumentNullException(nameof(cell), "Cell cannot be null");
}
if (_column == 2)
cell.PutValue(""); // Ensure cell is initialized
else
cell.PutValue(_row + _column);
}
Review the LightCellsDataProvider Implementation: Make sure that your implementation of the LightCellsDataProvider is correctly set up and that the NextCell and NextRow methods are returning valid indices. If these methods return invalid values, it could lead to uninitialized cells.
Use the Correct Save Options: You are currently using XlsSaveOptions, which is appropriate for the XLS format. However, if you are trying to use features that are more compatible with the XLSX format, consider switching to OoxmlSaveOptions:
var saveOptions = new OoxmlSaveOptions();
Refer to Example Implementations: You can look at example implementations of the LightCells API to ensure that your usage aligns with best practices. For instance, the provided examples in the Aspose documentation demonstrate how to properly implement the LightCellsDataProvider and handle cell data.
Here is a simplified version of your StartCell method that includes a null check:
public void StartCell(Cell cell)
{
if (cell == null)
{
throw new ArgumentNullException(nameof(cell), "Cell cannot be null");
}
if (_column == 2)
cell.PutValue(""); // Ensure cell is initialized
else
cell.PutValue(_row + _column);
if (_row != 1)
{
cell.Formula = "=Rand() + A2";
}
}
By implementing these suggestions, you should be able to avoid the “Object reference not set to an instance of an object” exception and successfully use the LightCells API to build your XLS file.
If you continue to experience issues, consider checking the Aspose documentation or community forums for additional insights and examples related to the LightCells API.
@523218882
We have reproduced the issue and we have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSNET-58413
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.