Two ways to import Java Excel JXL and POI

Source: Internet
Author: User

There are two ways to import Excel: JXL and POI

1. JXL parsing Excel

public class Jxlreadexcel {

/**
* JXL parsing Excel
* @author David
* @param args
*/
public static void Main (string[] args) {

try {
Create Workbook
Workbook Workbook = Workbook.getworkbook (New File ("E:/students.xls"));
Gets the first sheet of sheet
Sheet Sheet = workbook.getsheet (0);
Get Data
for (int i = 0; i < sheet.getrows (); i++) {
for (int j = 0; J < Sheet.getcolumns (); j + +) {
Cell cell = Sheet.getcell (j,i);
System.out.print (cell.getcontents () + "");
}
System.out.println ();
}
Workbook.close ();
} catch (Exception e) {
E.printstacktrace ();
}



}

}

2. POI parsing Excel

public class Poireadexcel {

/**
* Poi Parse Excel file content
* @author David
* @param args
*/
public static void Main (string[] args) {

Excel files that need to be parsed
File File = new file ("E:/students.xls");
try {
Create Excel to read file contents
Hssfworkbook Workbook =
New Hssfworkbook (Fileutils.openinputstream (file));
Read Default first worksheet sheet
Hssfsheet sheet = workbook.getsheetat (0);
int firstrownum = 0;
Gets the last line number in the sheet
int lastrownum = Sheet.getlastrownum ();
for (int i = firstrownum; I <=lastRowNum; i++) {
Hssfrow row = Sheet.getrow (i);
Gets the last cell column number of the current line
int lastcellnum = Row.getlastcellnum ();
for (int j = 0; J < Lastcellnum; J + +) {
Hssfcell cell = Row.getcell (j);
if (Row.getcell (j)!=null) {
Row.getcell (j). Setcelltype (cell.cell_type_string);
}
String value = Cell.getstringcellvalue ();
System.out.print (Value + "");
}
System.out.println ();
}

} catch (IOException e) {
E.printstacktrace ();
}



}

}

Two ways to import Java Excel JXL and POI

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.