Java reads Excel files

Source: Internet
Author: User

During this period of time, due to work reasons, thousands of data records need to be processed, so the data is poured out from the database and an Excel file is generated.

For a large number of repeated operations, manual processing is time-consuming and error-prone, So I thought of a program to process

Data, so I wrote a program to parse excel2007 documents. Note: If the previous version is low, you cannot report the exception in excel2007.

And then change to poi-3.8 to support 2007. If you encounter a similar problem, you can upgrade the version.

Some other experience is that the numeric data of a column in a single Excel file is queried in the scientific notation form, which can be used as needed, such as the integer type,

The retrieved value can be thrown into the integer constructor to convert the scientific Count value to a common value. If a certain type is null (not null ),

That is, if a column does not have a value, no error will be reported during poi reading, but the read will be stuck here. A solution is to assign a null value to the column.

Poi can continue to read the entire Excel Data.

The following is a simple example I have written. I hope you can use it for reference:

Package test;

Import java. Io. ioexception;
Import java. util. date;

Import org. Apache. Poi. xssf. usermodel. xssfrow;
Import org. Apache. Poi. xssf. usermodel. xssfsheet;
Import org. Apache. Poi. xssf. usermodel. xssfworkbook;

/**
* Excel worksheet operations
*
* @ Author: hnhychlorophyll
* @ Version 1.0
*/
Public class excelreader {

Private Static xssfworkbook xwb;

Private Static xssfsheet sheet;

Private Static xssfrow row;

Public static void readexcle2007 (){
Try {
Xwb = new xssfworkbook ("C :\\ lpk.xlsx ");
Sheet = xwb. getsheetat (0 );
String cell;
// System. Out. println ("physical rows =" + sheet. getphysicalnumberofrows ());
// System. Out. println ("physical Cols =" + sheet. getrow (1). getphysicalnumberofcells ());
// The list starts from 0.
// System. Out. println ("first Cols =" + sheet. getrow (1). getfirstcellnum ());
For (INT I = sheet. getfirstrownum () + 1; I <sheet. getlastrownum (); I ++ ){
Row = sheet. getrow (I );
System. Out. println ("ROW =" + row. getrownum ());

/**
* Read all data
*/
// For (Int J = row. getfirstcellnum (); j <row. getphysicalnumberofcells (); j ++ ){
// Row. getcell (J, row. return_null_and_blank). setcelltype (cell. cell_type_string );
// Cell = row. getcell (j). getstringcellvalue ();
// System. Out. Print (cell + "\ t ");
//}

Date begindate = row. getcell (2). getdatecellvalue ();
Date enddate = row. getcell (3). getdatecellvalue ();
Date activitydate = row. getcell (4). getdatecellvalue ();

// Determine whether the gift card has exceeded or expired. if it exceeded or expired, the wrong order number is output.
If (row. getcell (6). getnumericcellvalue ()> row. getcell (7). getnumericcellvalue () | begindate. After (enddate) | null = activitydate)
System. Out. println (row. getcell (0). getstringcellvalue ());
}
} Catch (ioexception e ){
E. printstacktrace ();
}
}

Public static void main (string [] ARGs ){
// Check code
Try {
// Read excel2007
Readexcle2007 ();
} Catch (exception ex ){
}
Long endtime = system. currenttimemillis ();

}
}

The above program depends on the plug-in poi-3.8. You can download it from my resources for free and copy the code to run it. Added support:

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.