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
}
}
}
}
}