How to read blank cell column value of excel using OpenXML in C# -


here in execl sheet there blank value column cell, when use code in error "object reference not set instance of object".

foreach (row row in rows) {    datarow datarow = datatable.newrow();    (int = 0; < row.descendants<cell>().count(); i++)    {         datarow[i] = getcellvalue(spreadsheetdocument, row.descendants<cell>().elementat(i));    }     datatable.rows.add(datarow); }  private static string getcellvalue(spreadsheetdocument document, cell cell) {     sharedstringtablepart stringtablepart = document.workbookpart.sharedstringtablepart;      string value = cell.cellvalue.innerxml;      if (cell.datatype != null && cell.datatype.value == cellvalues.sharedstring)     {         return stringtablepart.sharedstringtable.childelements[int32.parse(value)].innertext;     }     else     {         return value;     } } 

the "cellvalue" don't exist. in case "null", have error. read blank cell :

if don't want format result depending cell contain, try

private static string getcellvalue(cell cell) {     return cell.innertext; } 

if want format cell before returning value of it

private static string getcellvalue(spreadsheetdocument doc, cell cell) {     // if no datatype, return value of innertext of cell     if (cell.datatype == null) return cell.innertext;      // depending type of cell     switch (cell.datatype.value)     {         // string => search cellvalue         case cellvalues.string:             return cell.cellvalue != null ? cell.cellvalue.text : string.empty;          // inlinestring => search of inlinestring         case cellvalues.inlinestring:             return cell.inlinestring != null ? cell.inlinestring.text.text : string.empty;          // sharedstring => search sharedstring         case cellvalues.sharedstring:             // sharedpart exist ?             if (doc.workbookpart.sharedstringtablepart == null) doc.workbookpart.sharedstringtablepart = new sharedstringtablepart();             // text exist ?             foreach (sharedstringitem item in doc.workbookpart.sharedstringtablepart.sharedstringtable.elements<sharedstringitem>())             {                 // text exist, return sharedstringtable                 if (item.innertext == cell.innertext) return cell.innertext;             }             // no text in sharedstringtable, create , return             doc.workbookpart.sharedstringtablepart.sharedstringtable.append(new sharedstringitem(new documentformat.openxml.spreadsheet.text(cell.innertext)));             doc.workbookpart.sharedstringtablepart.sharedstringtable.save();             return cell.innertext;          // default case : bool / number / date         // return value of cell in plain text         // can parse types depending needs         default:             return cell.innertext;     } } 

two usefull documentations:


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -