POI讀取Excel到數(shù)據(jù)庫(kù)
? ?新入職后,接手的第一個(gè)任務(wù)是將excel中的數(shù)據(jù)錄入數(shù)據(jù)庫(kù),這里總結(jié)一下。
public?String?excelInput()?throws?Exception?{ ????????InputStream?is?=?new?FileInputStream("D://model.xlsx"); ????????XSSFWorkbook?hssfWorkbook?=?new?XSSFWorkbook(is); ????????DeviceInfo?deviceInfo; ????????Listlist?=?new?ArrayList(); ????????//?循環(huán)工作表Sheet ????????for?(int?numSheet?=?0;?numSheet?<?hssfWorkbook.getNumberOfSheets();?numSheet++)?{ ????????????XSSFSheet?xssfSheet?=?hssfWorkbook.getSheetAt(numSheet); ????????????if?(xssfSheet?==?null)?{ ????????????????continue; ????????????} ????????????//?循環(huán)行Row ????????????for?(int?rowNum?=?1;?rowNum?<=?xssfSheet.getLastRowNum();?rowNum++)?{ ????????????????XSSFRow?xssfRow?=?xssfSheet.getRow(rowNum); ????????????????if?(xssfRow?!=?null)?{ ????????????????????deviceInfo?=?new?DeviceInfo(); ????????????????????XSSFCell?did?=?xssfRow.getCell(0); ????????????????????XSSFCell?didSn?=?xssfRow.getCell(1); ???????????????????? ????????????????????deviceInfo.setDid(String.valueOf(did)); ????????????????????deviceInfo.setDidSn(String.valueOf(didSn)); ????????????????????//將數(shù)據(jù)以對(duì)象形式插入數(shù)據(jù)庫(kù) ????????????????????deviceDao.insertDeviceSelective(deviceInfo); ??????} ???} ??} }
1, 對(duì)于類似電話號(hào)碼或手機(jī)一類的大數(shù)值讀取問(wèn)題?
//?取值后會(huì)帶一個(gè)E的問(wèn)題??? double?cellValue?=?row.getCell(k).getNumericCellValue();??? fieldValues[k]?=?new?DecimalFormat("#").format(cellValue);
2, 對(duì)于數(shù)值型單元的純數(shù)值和日期型的處理?
case?HSSFCell.CELL_TYPE_NUMERIC:?//?數(shù)值型????? ????if?(HSSFDateUtil.isCellDateFormatted(cell))?{????? ????????//??如果是date類型則?,獲取該cell的date值????? ????????value?=?HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();????? ????}?else?{?//?純數(shù)字????? ????????value?=?String.valueOf(cell.getNumericCellValue());????? }