package
org.ian.webutil;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
jxl.Cell;
import
jxl.Sheet;
import
jxl.Workbook;
import
jxl.read.biff.BiffException;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.poi.xssf.usermodel.XSSFCell;
import
org.apache.poi.xssf.usermodel.XSSFRow;
import
org.apache.poi.xssf.usermodel.XSSFSheet;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook;
public
class
ReadExcel {
private
static
final
Log log = LogFactory.getLog(ReadExcel.
class
);
/**
* 判断后缀分批入
*/
private
static
void
parseSUCCEXX(String realPath,String fileName){
String [] pfix= fileName.split(
"\\."
);
String suffix = pfix[pfix.length -
1
];
if
( suffix!=
null
&&!suffix.equals(
""
)&&suffix.equals(
"xls"
)){
System.out.println(
"xls"
);
jlxExcel(realPath,fileName);
}
else
if
(suffix .equals(
"xlsx"
)){
System.out.println(
"xlsx"
);
poiExcel(realPath,fileName);
}
}
/**
* 读取 xls JXL
* @param realPath
* @param fileName
*/
private
static
void
jlxExcel(String realPath,String fileName){
try
{
File fileDes =
new
File(realPath);
InputStream str =
new
FileInputStream(fileDes);
Workbook rwb=Workbook.getWorkbook(str);
Sheet rs=rwb.getSheet(
0
);
int
rsRows=rs.getRows();
int
rsCols=rs.getColumns();
log.info(
"========行========"
+rsRows+
"=====列========"
+rsCols);
for
(
int
i=
1
;i<rsRows;i++){
log.info(
"========执行第========"
+i+
"行"
);
for
(
int
j=
0
;j<rsCols;j++){
log.info(
"========执行第========"
+j+
"列"
);
Cell coo=rs.getCell(j, i);
log.info(
"========coo========"
+coo);
String strc=coo.getContents();
log.info(
"========读取内容strc========"
+strc);
System.out.println(
"文件"
+fileName+
"的内容为:"
+strc);
}
}
rwb.close();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(BiffException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
/**
* POI读取 xlsx
* @param realPath
* @param fileName
*/
private
static
void
poiExcel(String realPath,String fileName){
try
{
File fileDes =
new
File(realPath);
InputStream str =
new
FileInputStream(fileDes);
XSSFWorkbook xwb =
new
XSSFWorkbook(str);
XSSFSheet st = xwb.getSheetAt(
0
);
int
rows=st.getLastRowNum();
int
cols;
log.info(
"========行========"
+rows);
for
(
int
i=
0
;i<rows;i++){
XSSFRow row=st.getRow(i);
if
(row!=
null
){
cols=row.getLastCellNum();
log.info(
"========行========"
+rows+
"=====列========"
+cols);
for
(
int
j=
0
;j<cols;j++){
XSSFCell cell=row.getCell(j);
if
(cell==
null
){
System.out.print(
" "
);
}
else
{
switch
(cell.getCellType()) {
case
XSSFCell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() +
" "
);
break
;
case
XSSFCell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() +
" "
);
break
;
case
XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue() +
" "
);
break
;
case
XSSFCell.CELL_TYPE_FORMULA:
System.out.print(cell.getCellFormula() +
" "
);
break
;
case
XSSFCell.CELL_TYPE_BLANK:
System.out.println(
""
);
break
;
case
XSSFCell.CELL_TYPE_ERROR:
System.out.println(
"故障"
);
break
;
default
:
System.out.print(
"未知类型 "
);
break
;
}
}
}
}
}
}
catch
(IOException e){
e.printStackTrace();
}
}
/**
* test
* @param args
*/
public
static
void
main(String[] args) {
String fileName =
"banShot.xlsx"
;
String realPath =
"d:/"
+fileName;
parseSUCCEXX(realPath, fileName);
}
}