**
jxl read excel file using java
**
jxl use java library to read excel file, this article excel file name suffix .xls, are not supported .xlsx.
code show as below:
public class ExcelReader {
Workbook rwb;
public String[][] readExcel(String filePath) throws IOException {
if (filePath.endsWith(".xls")) {
return this.reader(filePath);
} else {
return null;
}
}
public String[][] reader(String filePath) throws IOException {
try{
InputStream is = new FileInputStream(filePath);
rwb = jxl.Workbook.getWorkbook(is);
Sheet readsheet = rwb.getSheet(0);
int rsColumns = readsheet.getColumns();
int rsRows = readsheet.getRows();
String[][] data = new String[rsRows][rsColumns];
for(int i = 0; i < rsRows; i++)
{
for(int j = 0; j < rsColumns; j++)
{
data[i][j] = readsheet.getCell(j, i).getContents();
}
}
return data;
} catch(Exception e){
e.printStackTrace();
return null;
} finally {
rwb.close();
}
}
If numeric, Cell. the getContents () will automatically rounded three decimal places, if more precision, use the following code reads:
Cell cell = readsheet.getCell(j, i);
if(cell.getType() == CellType.NUMBER)
{
NumberCell numberCell = (NumberCell) cell;
data[i][j] = String.valueOf(numberCell.getValue());
}