Post Source: http://yl-fighting.iteye.com/blog/1726285
Reading Excel data with POI: (version number: POI3.7)
1. Read Excel
Java code
- Private list<string[]> Rosolvefile (InputStream is, String suffix,
- int StartRow) throws IOException, FileNotFoundException {
- Workbook Xssfworkbook = null;
- if ("xls". Equals (suffix)) {
- Xssfworkbook = New Hssfworkbook (IS);
- } Else if ("xlsx". Equals (suffix)) {
- Xssfworkbook = New Xssfworkbook (IS);
- }
- Sheet Xssfsheet = Xssfworkbook.getsheetat (0);
- if (Xssfsheet = = null) {
- return null;
- }
- arraylist<string[]> list = new arraylist<string[]> ();
- int lastrownum = Xssfsheet.getlastrownum ();
- For (int rowNum = startrow; rowNum <= lastrownum; rownum++) {
- if (xssfsheet.getrow (rowNum) = null) {
- Row Xssfrow = Xssfsheet.getrow (RowNum);
- Short firstcellnum = Xssfrow.getfirstcellnum ();
- Short lastcellnum = Xssfrow.getlastcellnum ();
- if (firstcellnum! = lastcellnum) {
- String[] values = new String[lastcellnum];
- For (int cellnum = firstcellnum; cellnum < lastcellnum; cellnum++) {
- Cell Xssfcell = Xssfrow.getcell (cellnum);
- if (Xssfcell = = null) {
- Values[cellnum] = "";
- } Else {
- Values[cellnum] = Parseexcel (Xssfcell);
- }
- }
- List.add (values);
- }
- }
- }
- return list;
- }
2. Excel Data Processing:
Excel stores date and time are stored as numeric types, and when read, POI first determines whether it is a numeric type and then makes a judgment conversion.
1. Numerical format (cell_type_numeric):
1. Pure Numeric format : Getnumericcellvalue () get data directly
2. date format : deal with Yyyy-mm-dd, d/m/yyyy h:mm, hh:mm , etc. no text-containing date format
1). Determine if it is a date format: hssfdateutil.iscelldateformatted (cell)
2). Judgment is date or time
Cell.getcellstyle (). getDataFormat () = = Hssfdataformat.getbuiltinformat ("h:mm")
OR:cell.getCellStyle (). getDataFormat () = = Hssfdataformat.getbuiltinformat ("Yyyy-mm-dd")
3. Custom Date format : processing yyyy year M D Day, h time mm, yyyy year m month and other date formats with text
Determine Cell.getcellstyle (). getDataFormat () value, parse numeric format
YYYY year M D Day----->31
M D Day---->58
H mm Sub--->32
2. Character format (cell_type_string): Get content directly
Java code
- Private String Parseexcel (cell cell) {
- string result = new string ();
- switch (Cell.getcelltype ()) {
- Case Hssfcell.cell_type_numeric://numeric type
- if (hssfdateutil.iscelldateformatted (cell)) {//Process date format, time format
- SimpleDateFormat SDF = null;
- if (Cell.getcellstyle (). getDataFormat () = = Hssfdataformat
- . Getbuiltinformat ("h:mm")) {
- SDF = new SimpleDateFormat ("hh:mm");
- } else {//date
- SDF = new SimpleDateFormat ("Yyyy-mm-dd");
- }
- Date date = Cell.getdatecellvalue ();
- result = Sdf.format (date);
- } Else if (Cell.getcellstyle (). getDataFormat () = = + ) {
- //Process Custom date format: M month D Day (resolved by judging the format ID of the cell, the value of the ID is ()
- SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
- Double value = Cell.getnumericcellvalue ();
- Date date = Org.apache.poi.ss.usermodel.DateUtil
- . getjavadate (value);
- result = Sdf.format (date);
- } Else {
- Double value = Cell.getnumericcellvalue ();
- CellStyle style = Cell.getcellstyle ();
- DecimalFormat format = new DecimalFormat ();
- String temp = style.getdataformatstring ();
- //cell set to normal
- if (Temp.equals ("General")) {
- Format.applypattern ("#");
- }
- result = Format.format (value);
- }
- Break ;
- Case hssfcell.cell_type_string://STRING type
- result = Cell.getrichstringcellvalue (). toString ();
- Break ;
- Case Hssfcell.cell_type_blank:
- result = "";
- Default:
- result = "";
- Break ;
- }
- return result;
- }
* Universal Treatment Solution :
All date formats can be judged by the getDataFormat () value.
YYYY-MM-DD-----14
YYYY year M D Day---31
yyyy m month-------57
M-D Day----------58
hh:mm-----------20
h mm min-------32
Java code
- 1. Determine if it is a numeric format
- if (cell.getcelltype () = = Hssfcell.cell_type_numeric) {
- Short format = Cell.getcellstyle (). getDataFormat ();
- SimpleDateFormat SDF = null;
- if (format = = | | | format = = | | format = = | | | format = = ) {
- //Date
- SDF = new SimpleDateFormat ("Yyyy-mm-dd");
- }Else if (format = = | | format = = ) {
- //Time
- SDF = new SimpleDateFormat ("hh:mm");
- }
- Double value = Cell.getnumericcellvalue ();
- Date date = Org.apache.poi.ss.usermodel.DateUtil.getJavaDate (value);
- result = Sdf.format (date);
- }
POI read for Excel custom date format