Java read Excel2003, 2007, 2010 Tutorials

Source: Internet
Author: User

Import Java.io.File;
Import Java.io.FileInputStream;
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.ss.usermodel.WorkbookFactory;

public class Testexcel {
public static void Main (string[] args) {
try {
File File = new file ("C://users/administrator/desktop/test.xls");
FileInputStream is = new FileInputStream (file);

Workbook Workbook = Workbookfactory.create (IS);//This way Excel 2003/2007/2010 can handle
Sheet Sheet = workbook.getsheetat (0);//First Sheet
int rowCount = Sheet.getphysicalnumberofrows ();//Get Total rows
Row Row1 = sheet.getrow (0);//Get the first line to determine if Excel is canonical
if (!row1.getcell (0). toString (). Trim (). Equals ("ordinal") | |
!row1.getcell (1). toString (). Trim (). Equals ("Evaluation Year") | |
!row1.getcell (2). toString (). Trim (). Equals ("Taxpayer identification number") | |
!row1.getcell (3). toString (). Trim (). Equals ("Taxpayer name") | |
!row1.getcell (4). toString (). Trim (). Equals ("Evaluation result")) {
Return
}
for (int i=1;i<rowcount;i++) {
Row row = Sheet.getrow (i);
int cellcount = Row.getphysicalnumberofcells (); Get total number of columns
String XH = "";//serial number
String pjnd = "";//Evaluation Year
String NSRSBH = "";//Taxpayer Identification number
String nsrmc = "";//Taxpayer name
String PJJG = "";//Evaluation Results
for (int c=0;c<cellcount;c++) {
String cellstr = Row.getcell (c). toString (). Trim ();
if (!cellstr.equals ("") &&cellstr!=null) {
if (c==0) xh=cellstr;
if (c==1) pjnd=cellstr;
if (c==2) nsrsbh=cellstr;
if (c==3) nsrmc=cellstr;
if (c==4) pjjg=cellstr;
}
}
System.out.println (xh+ "|" +pjnd+ "|" +nsrsbh+ "|" +nsrmc+ "|" +PJJG);//Get Data for database operations
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}

Java read Excel2003, 2007, 2010 Tutorials

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.