
excellibrary - issue #99
DatasetHelper.CreateWorkbook throws "Invalid cell value" error for dbnull
What steps will reproduce the problem? 1. Create a dataset with some DBNull values in the cells 2. Pass the dataset to the CreateWorkbook function 3.
What is the expected output? What do you see instead? Expect to see nothing, and an xls file be created, but instead see a very generic "Invalid Cell Value" message.
What version of the product are you using? On what operating system? I am using v2.0.50727 of excel library and on MS Vista.
Please provide any additional information below.
The work-around i have used is to just change the dbnull values to blanks.
Thanks
Comment #1
Posted on Jul 30, 2011 by Massive DogYou can create you own version of CreateWorkbook checking for null values and changing to blanks. See code below
public static void CreateWorkbook(String filePath, DataSet dataset) { if (dataset.Tables.Count == 0) throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset");
Workbook workbook = new Workbook();
foreach (DataTable dt in dataset.Tables)
{
Worksheet worksheet = new Worksheet(dt.TableName);
for (int i = 0; i < dt.Columns.Count; i++)
{
// Add column header
worksheet.Cells[0, i] = new Cell(dt.Columns[i].ColumnName);
// Populate row data
for (int j = 0; j < dt.Rows.Count; j++)
//See here?? worksheet.Cells[j + 1, i] = new Cell(dt.Rows[j][i] == DBNull.Value ? "" : dt.Rows[j][i]); } workbook.Worksheets.Add(worksheet); } workbook.Save(filePath); }
Comment #2
Posted on Jan 26, 2012 by Happy CatI had to add a .ToString() at the end of dt.Rows[j][i] to get rid of "Invalid cell value" error finally :)
Comment #3
Posted on Aug 29, 2013 by Massive CatI also received this error when the dataset has an item with an Int64. I am just converting ToString()
Status: Done
Labels:
Type-Defect
Priority-Medium