excel - NPOI set Explicit Column Type Not working properly -


i'm using npoi excel library generate excel file, in excel file i'm explicitly define column type columns date,string etc.

im using following code achive this.

  var row = sheet.createrow(currentnpoirowindex++);                     (var colindex = 0; colindex < exportdata.columns.count; colindex++)                     {                         icell cell = null;                          cell = row.createcell(colindex);                         if (exportdata.columns[colindex].datatype == typeof(datetime))                         {                             if (exportdata.rows[rowindex][colindex].tostring() != "")                             {                                 cell.setcellvalue((datetime)exportdata.rows[rowindex][colindex]);                                 cell.cellstyle = (npoi.hssf.usermodel.hssfcellstyle)book.createcellstyle();                                 cell.cellstyle.dataformat = book.createdataformat().getformat("yyyymmdd hh:mm:ss");                                 cell = null;                             }                             else                                 cell.setcellvalue(exportdata.rows[rowindex][colindex].tostring());                         }                         else                             cell.setcellvalue(exportdata.rows[rowindex][colindex].tostring());                     }                 } 

the above code works fine 42 rows i.e. correctly set column type,but after 42 rows column type doesn't apply.

any highly appreciated.

you'll required set default column style if want set column format cells of column. please see below example xssf format. syntax may differ hssf format give idea missing.

i providing working code. using npoi version 2.2.1.0. can comment line //cell = null;

xssfworkbook workbook = new xssfworkbook();   xssfsheet sheet = (xssfsheet)workbook.createsheet("template");    xssffont defaultfont = (xssffont)workbook.createfont();   defaultfont.fontheightinpoints = (short)10;    xssfcellstyle headerstyle = (xssfcellstyle)workbook.createcellstyle();   headerstyle.wraptext = true;    xssfcellstyle defaultstyle = (xssfcellstyle)workbook.createcellstyle();   xssfdataformat defaultdataformat = (xssfdataformat)workbook.createdataformat();   defaultstyle.setdataformat(defaultdataformat.getformat("000-000-0000"));   defaultstyle.fillbackgroundcolor = indexedcolors.lightyellow.index;                   defaultstyle.fillforegroundcolor = indexedcolors.lightturquoise.index;   defaultstyle.setfont(defaultfont);    var row = sheet.createrow(0);   (int headercount = 0; headercount < headers.count(); headercount++)   {       row.createcell(headercount).setcellvalue(headers[headercount]);       row.cells[headercount].cellstyle = headerstyle;       sheet.setdefaultcolumnstyle(headercount, defaultstyle);             } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -