使用POI 讀取Excel文件數據
/*POI常用類說明
類名 說明
HSSFWorkbook Excel的文檔對象
HSSFSheet Excel的表單
HSSFRow Excel的行
HSSFCell Excel的格子單元
HSSFFont Excel字體
HSSFDataFormat 格子單元的日期格式
HSSFHeader Excel文檔Sheet的頁眉
HSSFFooter Excel文檔Sheet的頁腳
HSSFCellStyle 格子單元樣式
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 錯誤信息表*/
/**
* 讀取Excel文件數據插入數據庫
* @param filePath
* @return
*
*/
public static String readExcel(String filePath){
InputStream in = null;//讀取劉文件
HSSFWorkbook workbook = null;//Excel文件對象
HSSFSheet sheet = null;//表單對象
HSSFRow row = null;//行
HSSFCell cell = null;//列
try {
filePath = System.getProperty("user.dir")+File.separator+"excelDownload"+File.separator+"test.xls";
in = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
workbook = new HSSFWorkbook(in);
} catch (IOException e) {
e.printStackTrace();
}
sheet = workbook.getSheetAt(0);//獲取第一個表單對象
row = sheet.getRow(0);//獲取第一個表單的第一行
int rows = sheet.getPhysicalNumberOfRows();//獲取總行數
int columns = row.getPhysicalNumberOfCells();//獲取第一行的列數
//遍歷行列
for (int i=0;i<rows;i++){
row = sheet.getRow(i);
for (int j=0;j<columns;j++){
cell = row.getCell(j);
String cellValue = cell.getStringCellValue();
System.out.println("cellValue"+cellValue);
}
}
return "讀取完成";
}