(1): Celltype type value cell_type_numeric numeric 0 cell_type_string String Type 1 Cell_type_formula Formula 2 Cell_type_blank NULL 3 cell_ty Pe_boolean Boolean 4 cell_type_error Error 5
(2) SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm"); //12小时制(3) SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //24小时制(4) Java package
Package Com.common;import Java.io.file;import Java.io.fileinputstream;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.text.decimalformat;import Java.text.NumberFormat; Import Java.text.simpledateformat;import java.util.arraylist;import java.util.date;import java.util.Iterator; Import Java.util.list;import Java.util.map;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfcellstyle;import Org.apache.poi.hssf.usermodel.hssfdataformat;import Org.apache.poi.hssf.usermodel.hssfdateutil;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.hssf.util.hssfcolor;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.sheet;import Org.apache.poi.ss.usermodel.workbook;import Org.apache.poi.xssf.usermodel.xssfworkbook;public class UploadExcel {public static void main (string[] args) throws IOException {Uploadexcel.readexcel ();//uploadexcel.writeexcel ();} Format public static String Getformatkey for date format//numeric type (short formatnumber, date date) {string format = "; SimpleDateFormat SDF = null;if (FormatNumber = = Hssfdataformat.getbuiltinformat ("h:mm")) {SDF = new SimpleDateFormat ("HH : MM ");} else if (FormatNumber = = Hssfdataformat.getbuiltinformat ("M/d/yy")) {SDF = new SimpleDateFormat ("Yyyy-mm-dd");} else if (FormatNumber = = Hssfdataformat.getbuiltinformat ("M/d/yy h:mm")) {SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm: SS ");} ELSE{SDF = new SimpleDateFormat ("Mm-dd");} Format = Sdf.format (date); return format;} Read excelpublic static void Readexcel () throws ioexception{String Excelfilepath = "f://a.xlsx"; FileInputStream InputStream = new FileInputStream (new File (Excelfilepath)); When reading and writing XLS and xlsx formats, Hssfworkbook for xlsx Workbook Workbook = null for Xls,xssfworkbook; int index = Excelfilepath.lastindexof ("."); String suffix = excelfilepath.substring (index + 1). toLowerCase (); if ("xls". Equals (suffix)) {workbook = new Hssfworkbook (InputStream); } else if ("xlsx". Equals (suffix)) {workbook = new Xssfworkbook (InputStream); } else {System.err.println ("error"); } Sheet firstsheet = Workbook.getsheetat (0); iterator<row> Iterator = Firstsheet.iterator (); int i = 0; while (Iterator.hasnext ()) {System.out.print ("Serial Number:" + (i++) + ""); Row NextRow = Iterator.next (); iterator<cell> celliterator = Nextrow.celliterator (); while (Celliterator.hasnext ()) {Cell cell = Celliterator.next (); Switch (Cell.getcelltype ()) {case Cell.cell_type_boolean: { System.out.print (Cell.getbooleancellvalue ()); Break } case cell.cell_type_numeric: {Short format = Cell.getcellstyl E (). getDataFormat (); Gets the type value String printstring = ""; if (format = = | |) format = = | | format = = | | format = = | | | format = = | | Rmat = =) {printstring = Uploadexcel.getformatkey (format, Cell.getdatecellval UE ()); } else {DecimalFormat df = new DecimalFormat ("#.#"); printstring = Df.format (Cell.getnumericcellvalue ()); } System.out.print (printstring); Break } Case cell.cell_type_string: {System.out.print (Cell.getstringcellvalue ()); Break } default: {System.out.print (Cell.getstringcellvalue ()); }} System.out.print ("|"); } System.out.println ("# # #"); } workbook.close (); Inputstream.close ();} Write excelpublic static void Writeexcel () {try {fileoutputstream fileout = new FileOutputStream ("F://poi-test.xls"); Hssfworkbook workbook = new Hssfworkbook (); hssfsheet worksheet = workbook.createsheet ("POI worksheet");//index from 0,0 ... Cell A1 is cell (0,0) Hssfrow row1 = Worksheet.createrow ((short) 0); Hssfcell cellA1 = Row1.createcell ((short) 0); Cella1.setcellvalue ("Hello"); Hssfcellstyle CellStyle = Workbook.createcellstyle (); Cellstyle.setfillforegroundcolor (HSSFColor.GOLD.index); Cellstyle.setfillpattern (hssfcellstyle.soLid_foreground); Cella1.setcellstyle (CellStyle); Hssfcell cellB1 = Row1.createcell ((short) 1); Cellb1.setcellvalue ("Goodbye"); CellStyle = Workbook.createcellstyle (); Cellstyle.setfillforegroundcolor (HSSFColor.LIGHT_CORNFLOWER_BLUE.index); Cellstyle.setfillpattern ( Hssfcellstyle.solid_foreground); Cellb1.setcellstyle (CellStyle); Hssfcell cellC1 = Row1.createcell ((short) 2); Cellc1.setcellvalue (true); Hssfcell cellD1 = Row1.createcell ((short) 3); Celld1.setcellvalue (new Date ()); CellStyle = Workbook.createcellstyle (); Cellstyle.setdataformat (Hssfdataformat.getbuiltinformat ("M/d/yy h:mm")); Celld1.setcellstyle (CellStyle); Workbook.write (fileout); Fileout.flush (); Fileout.close (); SYSTEM.OUT.PRINTLN ("read End");} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}
Java POI Import cell type for Excel