Java用POI解析excel并獲取所有單元格數(shù)據(jù)的實例
更新時間:2017年10月07日 10:13:10 作者:小曉峰
下面小編就為大家?guī)硪黄狫ava用POI解析excel并獲取所有單元格數(shù)據(jù)的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
1.導入POI相關jar包
org.apache.poi jar
2.代碼示例
public List getAllExcel(File file, String tableName, String fname,
String enterpriseId, String reportId, String projectId)
throws FileNotFoundException, IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, ParseException {
List listt = new ArrayList();
try {
FileInputStream fis = new FileInputStream(file);
Workbook workbook = null;
if (fname.toLowerCase().endsWith("xlsx")) {
workbook = new XSSFWorkbook(fis);
} else if (fname.toLowerCase().endsWith("xls")) {
workbook = new HSSFWorkbook(new POIFSFileSystem(fis));
}
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = workbook.getSheetAt(i);
for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) { // 獲取每行
XSSFRow row = (XSSFRow) sheet.getRow(j);
if(row!=null){
List list = new ArrayList();
for (int k = 0; k < sheet.getRow(0).getPhysicalNumberOfCells(); k++) { // 獲取每個單元格
Cell cell = row.getCell(k);
if (cell == null) {
list.add("");
continue;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
list.add(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
list.add(cell.getDateCellValue());
} else {
list.add(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
list.add(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
list.add(cell.getCellFormula());
break;
default:
list.add("");
break;
}
}
listt.add(getByReflect(tableName, list, enterpriseId,reportId, projectId));
}
}
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return listt;
}
以上這篇Java用POI解析excel并獲取所有單元格數(shù)據(jù)的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12
SpringBoot無法識別bootstrap.yml小綠葉問題的解決辦法
一般單獨使用?Spring?Boot?時,bootstrap.yml?文件一般是不會生效的,也就是沒有小綠葉圖標,本文給大家介紹了SpringBoot無法識別bootstrap.yml小綠葉問題的解決辦法,文中給出了兩種解決方案,需要的朋友可以參考下2024-07-07

