excel - POI-XSSF: user defined data formats -


i reading excel , seems have user defined data formats. example has data format : "yyyy"e , displays date in yyyy format followed letter e.

now, format not part of built in ,available formats.

so, seems when try set dataformat of cellstyle, doesn't work.

first read workbook , create dataformat workbook.

xssfworkbook wb = new xssfworkbook(exceltoread); xssfdataformat df = wb.createdataformat(); 

the df object has user defined data formats (checked printing till 200). now, try create new cell in same workbook, new style this:

xssfcell celltemp = row.createcell(0); xssfcellstyle styletemp = wb.createcellstyle(); styletemp.setdataformat(df.getformat(cell.getcellstyle().getdataformatstring())); celltemp.setcellstyle(styletemp); celltemp.setcellvalue(cell.getstringcellvalue()); system.out.print(formatter.formatcellvalue(celltemp)+" "); 

but, formatter not give me correct string value, instead gives me underlying integer value of date (which guess default if format not recognized).

what correct way use user defined formats in xssf?

update: seems able use other user defined formats yyyy, 0.0% not yyyy"e" or yyyy"a"

it not clear me trying achieve. if dataformatter not formats, 1 use class derived cellformatter.

example:

import java.io.*;  import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.format.*; import org.apache.poi.openxml4j.exceptions.invalidformatexception;  import org.apache.poi.xssf.usermodel.xssfworkbook;  import java.awt.desktop; import java.util.date;  class customdateformattest {   public static void main(string[] args) {   try {     workbook wb = new xssfworkbook();    sheet sheet = wb.createsheet("sheet1");    row row = sheet.createrow(0);    cell cell = row.createcell(0);     cell.setcellvalue(new date());     dataformat format = wb.createdataformat();    cellstyle cellstyle = wb.createcellstyle();    cellstyle.setdataformat(format.getformat("yyyy\"e\""));     cell.setcellstyle(cellstyle);     outputstream out = new fileoutputstream("customdateformattest.xlsx");    wb.write(out);    wb.close();     system.out.println("done");    file outputfile = new file("customdateformattest.xlsx");    desktop.getdesktop().open(outputfile);     inputstream inp = new fileinputstream("customdateformattest.xlsx");    wb = workbookfactory.create(inp);     sheet = wb.getsheetat(0);    row = sheet.getrow(0);    cell = row.getcell(0);     //this not format    dataformatter dataformatter = new dataformatter();    system.out.println(dataformatter.formatcellvalue(cell));     //this format    string formatstring = cell.getcellstyle().getdataformatstring();    system.out.println(formatstring);    celldateformatter celldateformatter = new celldateformatter(formatstring);    //for using celldateformatter need know cell value date.    system.out.println(celldateformatter.format(cell.getdatecellvalue()));     } catch (invalidformatexception ifex) {   } catch (filenotfoundexception fnfex) {   } catch (ioexception ioex) {   }  } } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -