Java uses poi.3.10 to read more than version of Excel 2007 (xlsx format)

Source: Internet
Author: User

1, in the use of the process, has been error throw new ClassNotFoundException (name); Cause: Xmlbeans-2.6.0.jar package is not imported, it is recommended that all packages are imported into the project when using POI.

2. Case Source

ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.util.Iterator;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook;Importorg.junit.Test; Public classTestexcels { Public StaticString FilePath = Testexcel.class. GetResource ("/test.xlsx"). GetPath (); @Test Public Static voidMain (string[] args) {readXml (FilePath); System.out.println ("-------------"); }     Public Static voidreadXml (String fileName) {BooleanisE2007 =false;//determine if the format is excel2007        if(Filename.endswith ("xlsx")) isE2007=true; Try{InputStream input=NewFileInputStream (FileName);//Create an input streamWorkbook WB =NULL; //Initialize according to file format (2003 or 2007)            if(isE2007) WB=NewXssfworkbook (input); ElseWB=NewHssfworkbook (input); Sheet Sheet= Wb.getsheetat (0);//get the first formiterator<row> rows = Sheet.rowiterator ();//the iterator that gets the first form             while(Rows.hasnext ()) {row row= Rows.next ();//Get Row DataSystem.out.println ("Row #" + row.getrownum ());//get line number starting from 0Iterator<cell> cells = Row.celliterator ();//get the first line of Iterators                 while(Cells.hasnext ()) {cell cell=Cells.next (); System.out.println ("Cell #" +Cell.getcolumnindex ()); Switch(Cell.getcelltype ()) {//output data According to the type in the cell                     CaseHSSFCell.CELL_TYPE_NUMERIC:System.out.println (Cell.getnumericcellvalue ());  Break;  CaseHSSFCell.CELL_TYPE_STRING:System.out.println (Cell.getstringcellvalue ());  Break;  CaseHSSFCell.CELL_TYPE_BOOLEAN:System.out.println (Cell.getbooleancellvalue ());  Break;  CaseHSSFCell.CELL_TYPE_FORMULA:System.out.println (Cell.getcellformula ());  Break; default: System.out.println ("Unsuported Sell Type");  Break; }                }            }        } Catch(IOException ex) {ex.printstacktrace (); }    }}

Related code comments can refer to Java using POI to read Excel2003

Java uses poi.3.10 to read more than version of Excel 2007 (xlsx 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.