Java uses POI to parse more than 2007 Excel tables

Source: Internet
Author: User

From http://hao0610.iteye.com/blog/1160678

Use POI to parse the XLS and xlsx of Excel.

Resolve XLS:

Java code
  1. Package xls;
  2. Import Java.io.FileInputStream;
  3. Import java.io.IOException;
  4. Import Java.io.InputStream;
  5. Import Org.apache.poi.hssf.usermodel.HSSFCell;
  6. Import Org.apache.poi.hssf.usermodel.HSSFRow;
  7. Import Org.apache.poi.hssf.usermodel.HSSFSheet;
  8. Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. Public class Xlsmain {
  10. public static void Main (string[] args) throws IOException {
  11. Xlsmain Xlsmain = new Xlsmain ();
  12. Xlsmain.readxls ();
  13. }
  14. private void Readxls () throws ioexception{
  15. InputStream is = new FileInputStream ( "D:\\excel\\xls_test2.xls");
  16. Hssfworkbook Hssfworkbook = new Hssfworkbook (IS);
  17. //Cycle sheet sheet
  18. For (int numsheet = 0; numsheet < hssfworkbook.getnumberofsheets (); numsheet++) {
  19. Hssfsheet Hssfsheet = Hssfworkbook.getsheetat (Numsheet);
  20. if (Hssfsheet = = null) {
  21. continue;
  22. }
  23. //Cycle rows row
  24. For (int rowNum = 0; rowNum <= hssfsheet.getlastrownum (); rownum++) {
  25. Hssfrow Hssfrow = Hssfsheet.getrow (RowNum);
  26. if (Hssfrow = = null) {
  27. continue;
  28. }
  29. //Cyclic column cell
  30. For (int cellnum = 0; cellnum <= hssfrow.getlastcellnum (); cellnum++) {
  31. Hssfcell Hssfcell = Hssfrow.getcell (cellnum);
  32. if (Hssfcell = = null) {
  33. continue;
  34. }
  35. System.out.print ("" + GetValue (Hssfcell));
  36. }
  37. System.out.println ();
  38. }
  39. }
  40. }
  41. @SuppressWarnings ("static-access")
  42. private String GetValue (Hssfcell hssfcell) {
  43. if (hssfcell.getcelltype () = = Hssfcell.cell_type_boolean) {
  44. return string.valueof (Hssfcell.getbooleancellvalue ());
  45. }Else if (hssfcell.getcelltype () = = hssfcell.cell_type_numeric) {
  46. return string.valueof (Hssfcell.getnumericcellvalue ());
  47. }else{
  48. return string.valueof (Hssfcell.getstringcellvalue ());
  49. }
  50. }
  51. }



Resolve xlsx:

Java code
    1. Package xlsx;
    2. Import java.io.IOException;
    3. Import Org.apache.poi.xssf.usermodel.XSSFCell;
    4. Import Org.apache.poi.xssf.usermodel.XSSFRow;
    5. Import Org.apache.poi.xssf.usermodel.XSSFSheet;
    6. Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
    7. Public class Xlsxmain {
    8. public static void Main (string[] args) throws IOException {
    9. Xlsxmain Xlsxmain = new Xlsxmain ();
    10. Xlsxmain.readxlsx ();
    11. }
    12. private void readxlsx () throws ioexception{
    13. String fileName = "D:\\excel\\xlsx_test.xlsx";
    14. Xssfworkbook Xssfworkbook = new Xssfworkbook (FileName);
    15. //Cycle sheet sheet
    16. For (int numsheet = 0; numsheet < xssfworkbook.getnumberofsheets (); numsheet++) {
    17. Xssfsheet Xssfsheet = Xssfworkbook.getsheetat (Numsheet);
    18. if (Xssfsheet = = null) {
    19. continue;
    20. }
    21. //Cycle rows row
    22. For (int rowNum = 0; rowNum <= xssfsheet.getlastrownum (); rownum++) {
    23. Xssfrow Xssfrow = Xssfsheet.getrow (RowNum);
    24. if (Xssfrow = = null) {
    25. continue;
    26. }
    27. //Cyclic column cell
    28. For (int cellnum = 0; cellnum <= xssfrow.getlastcellnum (); cellnum++) {
    29. Xssfcell Xssfcell = Xssfrow.getcell (cellnum);
    30. if (Xssfcell = = null) {
    31. continue;
    32. }
    33. System.out.print ("+getvalue (Xssfcell)");
    34. }
    35. System.out.println ();
    36. }
    37. }
    38. }
    39. @SuppressWarnings ("static-access")
    40. private String GetValue (Xssfcell xssfcell) {
    41. if (xssfcell.getcelltype () = = Xssfcell.cell_type_boolean) {
    42. return string.valueof (Xssfcell.getbooleancellvalue ());
    43. }Else if (xssfcell.getcelltype () = = xssfcell.cell_type_numeric) {
    44. return string.valueof (Xssfcell.getnumericcellvalue ());
    45. }else{
    46. return string.valueof (Xssfcell.getstringcellvalue ());
    47. }
    48. }
    49. }

Java uses POI to parse more than 2007 Excel tables

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.