Package com.sun.test;
Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.text.DecimalFormat;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.Date;
Import java.util.List;
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 Exceloperate {
public static void Main (string[] args) throws Exception {
File File = new file ("C:\\users\\xsp034\\desktop\\ce.xls");
String[][] result = GetData (file, 1);
int rowlength = Result.length;
for (int i=0;i<rowlength;i++) {
for (int j=0;j<result[i].length;j++) {
System.out.print (result[i][j]+ "\t\t");
}
System.out.println ();
}
}
/**
* Read the contents of Excel, the first-dimensional array stores the values of the columns in a row, and how many rows are stored in a two-dimensional array
* Source Excel for reading data @param file
* @param the number of rows ignored by the ignorerows reading data, the number of rows ignored by the figurative costume does not need to be read into 1
* @return read out the contents of the data in Excel
* @throws FileNotFoundException
* @throws IOException
*/
public static string[][] GetData (file file, int ignorerows)
Throws FileNotFoundException, IOException {
list<string[]> result = new arraylist<string[]> ();
int rowsize = 0;
Bufferedinputstream in = new Bufferedinputstream (New FileInputStream (
file));
Open Hssfworkbook
Poifsfilesystem fs = new Poifsfilesystem (in);
Hssfworkbook wb = new Hssfworkbook (FS);
Hssfcell cell = null;
for (int sheetindex = 0; Sheetindex < wb.getnumberofsheets (); sheetindex++) {
Hssfsheet st = Wb.getsheetat (Sheetindex);
First act title, not take
for (int rowIndex = ignorerows; RowIndex <= st.getlastrownum (); rowindex++) {
Hssfrow row = St.getrow (RowIndex);
if (row = = null) {
Continue
}
int temprowsize = Row.getlastcellnum () + 1;
if (Temprowsize > Rowsize) {
Rowsize = temprowsize;
}
String[] values = new String[rowsize];
Arrays.fill (Values, "");
Boolean hasValue = false;
for (short columnindex = 0; columnindex <= row.getlastcellnum (); columnindex++) {
String value = "";
Cell = Row.getcell (columnindex);
if (cell! = null) {
Note: Must be set to this, or it may appear 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:
No value when data generated for a formula is imported
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 = "";
}
}
if (ColumnIndex = = 0 && value.trim (). Equals ("")) {
Break
}
Values[columnindex] = Righttrim (value);
HasValue = true;
}
if (HasValue) {
Result.add (values);
}
}
}
In.close ();
string[][] ReturnArray = new String[result.size ()][rowsize];
for (int i = 0; i < returnarray.length; i++) {
Returnarray[i] = (string[]) result.get (i);
}
return returnarray;
}
/**
* Remove the space to the right of the string
* @param str to process the string
* @return The processed string
*/
public static string Righttrim (String str) {
if (str = = null) {
Return "";
}
int length = Str.length ();
for (int i = length-1; I >= 0; i--) {
if (Str.charat (i)! = 0x20) {
Break
}
length--;
}
Return str.substring (0, length);
}
}
Java reads Excel normal XLS