c# - How to get the openxml.spreadsheet.cell screen display value -


i have c# application reads uploaded excel file (.xslx) created in excel 2013

i read each cell putting each cell collection of cells per collection of rows using c# openxml library.

the code works correctly except doesn't read formatted excel cell value correctly.

here how cell shows in excel:

enter image description here

and fromatting: enter image description here

i have examined documentformat.openxml.spreadsheet.cell cell properties cell when c# application reads none of cell properties equals value of may-15 want.

i noticed cell.datatype , cell.cellformula both null cannot used determine format.

the cell.innertext value "42125". if change cells format in excel general displays 42125.

i using code this article

here code snippet using

for (var = 0; < rows.count; i++) {     var datarow = new list<string>();     data.datarows.add(datarow);     var row = rows[i];     var cellenumerator = getexcelcellenumerator(row);     while (cellenumerator.movenext())     {         var cell = cellenumerator.current; //return current documentformat.openxml.spreadsheet.cell         var text = readexcelcell(cell, workbookpart).trim();         datarow.add(text);     } } 

.

private string readexcelcell(cell cell, workbookpart workbookpart) {     var cellvalue = cell.cellvalue;     var text = (cellvalue == null) ? cell.innertext : cellvalue.text;     if ((cell.datatype != null) && (cell.datatype == cellvalues.sharedstring))     {         text = workbookpart.sharedstringtablepart.sharedstringtable             .elements<sharedstringitem>().elementat(                 convert.toint32(cell.cellvalue.text)).innertext;     }      return (text ?? string.empty).trim(); } 

question:

  1. how can read display value of cell.innertext of may-15?

  2. is possible convert "may-15" "42125" or "42125" "may-15" using standard c# string/date formatting if given format mmm-y?

the answer 2nd question:

how convert 42125 may-15

var myval = string.format("{0:mmm-yy}", datetime.fromoadate(42125)); 

convert may-15 42125

var myval =  datetime.parseexact("may-15", "mmm-yy", system.globalization.cultureinfo.invariantculture).tooadate(); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -