/*
Note: Read the Excel file Save as 2003 version of Excel, otherwise you may get an error
Don't forget to import third-party jar packages
*/
Package Com.zzp.ExcelParse;
Import JXL. Cell;
Import JXL. Sheet;
Import JXL. Workbook;
Import Jxl.read.biff.BiffException;
Import Java.io.File;
Import Java.io.IOException;
/**
* Read the Excel table
* Created by Hasee on 2016/11/29.
*/
public class Obtainexceldata {
Private WorkbookWorkbook =Null
Private SheetSheet =Null
/*
@see: Loading files
@parma:p ath File path
@return: Whether the execution was successful
*/
public BooleanGetFile (String path) {
if (path.length () = =0 | | Path = =NULL) {
return false;
}
try {
Workbook = workbook.Getworkbook (New File (PATH);
return true;
}catch (IOException e) {
E.printstacktrace ();
}catch (Biffexception e) {
E.printstacktrace ();
}
return false;
}
/*
@see: The table is obtained according to the table subscript
@parma: Subscript Sheetindex the underlying
@return: Whether the execution was successful
*/
public BooleanGetsheet (int Sheetindex) {
if (Sheetindex >=0) {
Sheet =Workbook.getsheet (Sheetindex);
return true;
}
return false;
}
/*
@see: Get table by table name
@parma: SheetName table name
@return: Whether the execution was successful
*/
public BooleanGetsheet (String sheetname) {
if (Sheetname.length () >0 | | SheetName! =NULL) {
Sheet =Workbook.getsheet (SheetName);
return true;
}
return false;
}
/*
@see: Reading data by column (without reading headers)
@parma: rowindex The number of columns queried
@parma: Rows in row Excel table
@parma: RowData stores a list of data retrieved
@return: Get to a column of data
*/
Public string[]Getcol (int Colindex) {
if (Colindex <0) {
return null;
}
int row =Sheet.getrows ();
string[] RowData =New String[row];
Cell cell =Null
for (int i =1; I < row; i++) {
Cell =Sheet.getcell (ColindexI;
String Strrow = cell.getcontents ();
Rowdata[i-1] = Strrow;
}
Return RowData;
}
/*
@see: Reading data by rows
@parma: Number of rows queried by RowIndex
@parma: Col eccel column number of tables
@parma: RowData Stores the fetched row of data
@return: Gets the row of data
*/
Public string[]GetRow (int RowIndex) {
if (RowIndex <0)return null;
int col =Sheet.getcolumns ();
string[] RowData = new string[col];
cell cell = null;
for (int i = 0; I < Col cell = sheet.getcell (I, RowIndex) String Strrow = cell.getcontents () Rowdata[i] = Strrow "
return rowdata;
}
}
/span>
About Java reading to Excel