14. Java Implementation POI operation Excel, including read and write date format, and set font style

Source: Internet
Author: User
Tags dateformat

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.