Java文件導入導出
/**
* 導入
* @param path xls文件(路徑+文件名)
* @return
*/
public static void importExcel(String path) throws Exception{
FileInputStream inputStream = new FileInputStream(new File(path));
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet firstSheet = workbook.getSheetAt(0);//獲取第一個工作簿
int trLength = firstSheet.getLastRowNum();//獲取總行數
int tdLength = firstSheet.getRow(0).getPhysicalNumberOfCells();//獲取第一行總列數
for (int i=0; i<trLength; i++) {
HSSFRow row = firstSheet.getRow(i);
for (int j=0; j<tdLength; j++) {
HSSFCell cell = row.getCell(j);
/**
* 為了處理:Excel異常Cannot get a text value from a numeric cell
* 將所有列中的內容都設置成String類型格式
*/
if(cell != null){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
System.out.println(cell.getStringCellValue());
}
System.out.println("----------");
}
workbook.close();
inputStream.close();
}
最後更新:2017-07-14 09:02:55