Reprint Java reads Excel 2010 using poi.3.10

Source: Internet
Author: User

Package poi;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.Iterator;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 ReadExcel001 {
public static void Main (string[] args) {
READXML ("d:/test.xlsx");
System.out.println ("-------------");
READXML ("D:/test2.xls");
}
public static void ReadXml (String fileName) {
Boolean isE2007 = false; Determine if the format is excel2007
if (Filename.endswith ("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream (fileName); Create an input stream
Workbook wb = null;
Class according to the file format (2003 or 2007)
if (isE2007)
WB = new Xssfworkbook (input);
Else
WB = new Hssfworkbook (input);
Sheet Sheet = wb.getsheetat (0); Get the first form
iterator<row> rows = Sheet.rowiterator (); The iterator that gets the first form
while (Rows.hasnext ()) {
Row row = Rows.next (); Get Row data
System.out.println ("Row #" + row.getrownum ()); Get line number starting from 0
Iterator<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
Case Hssfcell.cell_type_numeric:
System.out.println (Cell.getnumericcellvalue ());
Break
Case hssfcell.cell_type_string:
System.out.println (Cell.getstringcellvalue ());
Break
Case Hssfcell.cell_type_boolean:
System.out.println (Cell.getbooleancellvalue ());
Break
Case Hssfcell.cell_type_formula:
System.out.println (Cell.getcellformula ());
Break
Default
SYSTEM.OUT.PRINTLN ("unsuported sell type");
Break
}
}
}
} catch (IOException ex) {
Ex.printstacktrace ();
}
}
}

Reprint Java reads Excel 2010 using poi.3.10

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.