lamquanthanh999 / excellibrary

Automatically exported from code.google.com/p/excellibrary
1 stars 0 forks source link

DatasetHelper.CreateWorkbook throws "Invalid cell value" error for dbnull #99

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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 

Original issue reported on code.google.com by gregvick...@googlemail.com on 1 Jul 2011 at 8:29

GoogleCodeExporter commented 9 years ago
You 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);
        }

Original comment by regisbsb...@gmail.com on 30 Jul 2011 at 3:02

GoogleCodeExporter commented 9 years ago
I had to add a .ToString() at the end of dt.Rows[j][i] to get rid of  "Invalid 
cell value" error finally :)

Original comment by paronik...@gmail.com on 26 Jan 2012 at 4:39

GoogleCodeExporter commented 9 years ago
I also received this error when the dataset has an item with an Int64. I am 
just converting ToString()

Original comment by CurlyHai...@gmail.com on 29 Aug 2013 at 3:16