1, first of all to see the results of the export
This is the exported code.
protected voidTestexcel ()throwsioexception{String Path=getservletcontext (). Getrealpath ("/web-inf/template/timesequence.xlsx"); SYSTEM.OUT.PRINTLN (path); InputStream input=NewFileInputStream (path); Xssfworkbook WorkBook=NewXssfworkbook (input); Xssfsheet Hssfsheet= Workbook.getsheet ("Sheet1"); Xssfrow Hssfrow=NULL; Xssfcell Cell=NULL; Xssffont Font=Workbook.createfont (); Font.setfontname ("GE Inspira"); OutputStream out=NewFileOutputStream (path); Xssfsheet HssfSheet2= Workbook.createsheet ("Sheet2"); for(inti = 0; I < Hssfsheet.getlastrownum (); i++) {Hssfrow=Hssfsheet.getrow (i); Xssfrow Row=Hssfsheet2.createrow (i); for(intj = 0; J < Hssfrow.getlastcellnum (); J + +) {CellStyle style=Workbook.createcellstyle (); Style.setfont (font); Cell=Hssfrow.getcell (j); Xssfcell Cellwrite=Row.createcell (j); if(Cell.getcelltype () = =cell. cell_type_string) {//set value for stringsCellwrite.setcellvalue (Cell.getstringcellvalue ()); Cellwrite.setcellstyle (style); }Else if(Cell.getcelltype () = =cell. Cell_type_numeric) {//set Value for date if(hssfdateutil.iscelldateformatted (cell)) {DateFormat format=NewSimpleDateFormat (); ShortDf=workbook.createdataformat (). GetFormat ("Yyyy-mm-dd"); Style.setdataformat (DF); Cellwrite.setcellstyle (style); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy/mm/dd"); String Readdatevalue=Dateformat.format (Cell.getdatecellvalue ()); Cellwrite.setcellvalue (Readdatevalue); }Else{ //set value for numericCellwrite.setcellvalue (Cell.getnumericcellvalue ()); Cellwrite.setcellstyle (style); } }Else if(Cell.getcelltype () = =cell. Cell_type_blank) {//set value for blankCellwrite.setcellvalue (""); Cellwrite.setcellstyle (style); }Else{cellwrite.setcellvalue (Cell.getstringcellvalue ()); Cellwrite.setcellstyle (style); }}} workbook.write (out); }
Where Excel templates are stored
Thank you very much if you are not the author of the visit
14. Java Implementation POI operation Excel, including read and write date format, and set font style