POI read for Excel custom date format

Source: Internet
Author: User

Post Source: http://yl-fighting.iteye.com/blog/1726285

Reading Excel data with POI: (version number: POI3.7)

1. Read Excel

Java code
  1. Private list<string[]> Rosolvefile (InputStream is, String suffix,
  2. int StartRow) throws IOException, FileNotFoundException {
  3. Workbook Xssfworkbook = null;
  4. if ("xls". Equals (suffix)) {
  5. Xssfworkbook = New Hssfworkbook (IS);
  6. } Else if ("xlsx". Equals (suffix)) {
  7. Xssfworkbook = New Xssfworkbook (IS);
  8. }
  9. Sheet Xssfsheet = Xssfworkbook.getsheetat (0);
  10. if (Xssfsheet = = null) {
  11. return null;
  12. }
  13. arraylist<string[]> list = new arraylist<string[]> ();
  14. int lastrownum = Xssfsheet.getlastrownum ();
  15. For (int rowNum = startrow; rowNum <= lastrownum; rownum++) {
  16. if (xssfsheet.getrow (rowNum) = null) {
  17. Row Xssfrow = Xssfsheet.getrow (RowNum);
  18. Short firstcellnum = Xssfrow.getfirstcellnum ();
  19. Short lastcellnum = Xssfrow.getlastcellnum ();
  20. if (firstcellnum! = lastcellnum) {
  21. String[] values = new String[lastcellnum];
  22. For (int cellnum = firstcellnum; cellnum < lastcellnum; cellnum++) {
  23. Cell Xssfcell = Xssfrow.getcell (cellnum);
  24. if (Xssfcell = = null) {
  25. Values[cellnum] = "";
  26. } Else {
  27. Values[cellnum] = Parseexcel (Xssfcell);
  28. }
  29. }
  30. List.add (values);
  31. }
  32. }
  33. }
  34. return list;
  35. }

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
  1. Private String Parseexcel (cell cell) {
  2. string result = new string ();
  3. switch (Cell.getcelltype ()) {
  4. Case Hssfcell.cell_type_numeric://numeric type
  5. if (hssfdateutil.iscelldateformatted (cell)) {//Process date format, time format
  6. SimpleDateFormat SDF = null;
  7. if (Cell.getcellstyle (). getDataFormat () = = Hssfdataformat
  8. . Getbuiltinformat ("h:mm")) {
  9. SDF = new SimpleDateFormat ("hh:mm");
  10. } else {//date
  11. SDF = new SimpleDateFormat ("Yyyy-mm-dd");
  12. }
  13. Date date = Cell.getdatecellvalue ();
  14. result = Sdf.format (date);
  15. } Else if (Cell.getcellstyle (). getDataFormat () = = + ) {
  16. //Process Custom date format: M month D Day (resolved by judging the format ID of the cell, the value of the ID is ()
  17. SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
  18. Double value = Cell.getnumericcellvalue ();
  19. Date date = Org.apache.poi.ss.usermodel.DateUtil
  20. . getjavadate (value);
  21. result = Sdf.format (date);
  22. } Else {
  23. Double value = Cell.getnumericcellvalue ();
  24. CellStyle style = Cell.getcellstyle ();
  25. DecimalFormat format = new DecimalFormat ();
  26. String temp = style.getdataformatstring ();
  27. //cell set to normal
  28. if (Temp.equals ("General")) {
  29. Format.applypattern ("#");
  30. }
  31. result = Format.format (value);
  32. }
  33. Break ;
  34. Case hssfcell.cell_type_string://STRING type
  35. result = Cell.getrichstringcellvalue (). toString ();
  36. Break ;
  37. Case Hssfcell.cell_type_blank:
  38. result = "";
  39. Default:
  40. result = "";
  41. Break ;
  42. }
  43. return result;
  44. }

* 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. 1. Determine if it is a numeric format
  2. if (cell.getcelltype () = = Hssfcell.cell_type_numeric) {
  3. Short format = Cell.getcellstyle (). getDataFormat ();
  4. SimpleDateFormat SDF = null;
  5. if (format = = | | | format = = | | format = = | | | format = = ) {
  6. //Date
  7. SDF = new SimpleDateFormat ("Yyyy-mm-dd");
  8. }Else if (format = = | | format = = ) {
  9. //Time
  10. SDF = new SimpleDateFormat ("hh:mm");
  11. }
  12. Double value = Cell.getnumericcellvalue ();
  13. Date date = Org.apache.poi.ss.usermodel.DateUtil.getJavaDate (value);
  14. result = Sdf.format (date);
  15. }

POI read for Excel custom date format

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.