Java uses POI to read Excel content method instances _java

Source: Internet
Author: User

Copy Code code as follows:

Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.nio.channels.FileChannel;
Import Java.text.DecimalFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFDateUtil;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Fileoperator {
public static void Main (string[] args) throws Exception {
String Path = "F:\\1.xls";
print (path);
}

public static void print (String path) throws ioexception{
Bufferedinputstream in = new Bufferedinputstream (
New FileInputStream (New File (path));
Poifsfilesystem fs = new Poifsfilesystem (in);
Hssfworkbook wb = new Hssfworkbook (FS);
Hssfcell cell = null;
Hssfsheet st = wb.getsheetat (0);
for (int rowIndex = 0; RowIndex <= st.getlastrownum (); rowindex++) {
Hssfrow row = St.getrow (RowIndex);
if (row = = null) {
Continue
}
for (short columnindex = 0, size = Row.getlastcellnum (); columnindex <= size; columnindex++) {
Cell = Row.getcell (columnindex);
String value = "";
if (cell!= null) {
Note: Be sure to set this, otherwise there may be garbled
Cell.setencoding (HSSFCELL.ENCODING_UTF_16);
Switch (Cell.getcelltype ()) {
Case hssfcell.cell_type_string:
Value = Cell.getstringcellvalue ();
Break
Case Hssfcell.cell_type_numeric:
if (hssfdateutil.iscelldateformatted (cell)) {
Date date = Cell.getdatecellvalue ();
if (date!= null) {
Value = new SimpleDateFormat ("Yyyy-mm-dd")
. Format (date);
} else {
Value = "";
}
} else {
Value = new DecimalFormat ("0"). Format (cell
. Getnumericcellvalue ());
}
Break
Case Hssfcell.cell_type_formula:
When importing, the data generated for the formula is not worth
if (!cell.getstringcellvalue (). Equals ("")) {
Value = Cell.getstringcellvalue ();
} else {
Value = Cell.getnumericcellvalue () + "";
}
Break
Case Hssfcell.cell_type_blank:
Break
Case HSSFCELL.CELL_TYPE_ERROR:
Value = "";
Break
Case Hssfcell.cell_type_boolean:
Value = (Cell.getbooleancellvalue () = = true? Y
: "N");
Break
Default
Value = "";
}
System.out.println (RowIndex + "," + ColumnIndex + ":"
+ value);
}
if (ColumnIndex = = 0 && value.trim (). Equals ("")) {
Break
}
}
}
}

}

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.