jxl解析多个excel工作表-java代码
Posted 无奇刻
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jxl解析多个excel工作表-java代码相关的知识,希望对你有一定的参考价值。
@Override public ResultBean txImportDqKpi(String filePath) { ResultBean rb = new ResultBean(); int success = 0, fail = 0; StringBuffer sb = new StringBuffer(); try { File file = new File(fsConfig.getFsRoot() + filePath); Workbook wb = Workbook.getWorkbook(new FileInputStream(file)); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); List<SchDqKpiHead> dqKpiHeadList = new ArrayList<SchDqKpiHead>(); SchDqKpiHead dqKpiHead = null; for (int row = 2; row < rows; row++) { if (!TopUtil.isValidValue(sheet.getCell(0, row).getContents())) { continue; } try { dqKpiHead = new SchDqKpiHead(); dqKpiHead.setHeadId(new Long(this.checkIfNullToZero(sheet.getCell(0, row).getContents()))); dqKpiHead.setDqKpiCode(sheet.getCell(1, row).getContents()); dqKpiHead.setDqKpiName(sheet.getCell(2, row).getContents()); dqKpiHead.setDqKpiDesc(sheet.getCell(3, row).getContents()); dqKpiHead.setCalculateSql(sheet.getCell(4, row).getContents()); dqKpiHead.setAlterCondition(sheet.getCell(5, row).getContents()); dqKpiHead.setAlterMsg(sheet.getCell(6, row).getContents()); dqKpiHead.setObjectType(sheet.getCell(7, row).getContents()); dqKpiHead.setDqKpiType(sheet.getCell(8, row).getContents()); dqKpiHead.setKpiStatus(sheet.getCell(9, row).getContents()); dqKpiHead.setObjectCode(sheet.getCell(10, row).getContents()); dqKpiHead.setRunPosition(sheet.getCell(11, row).getContents()); dqKpiHead.setRunOrder(sheet.getCell(12, row).getContents()); dqKpiHead.setPonderance(sheet.getCell(13, row).getContents()); dqKpiHead.setEnabledFlag(sheet.getCell(14, row).getContents()); dqKpiHead.setCreatedBy("import"); dqKpiHead.setCreatedDate(new Date()); dqKpiHead.setLastUpdatedBy("import"); dqKpiHead.setLastUpdatedDate(new Date()); dqKpiHeadList.add(dqKpiHead); success++; } catch (Exception e) { log.error(e); e.printStackTrace(); fail++; continue; } } sb.append("表:SCH_DQ_KPI_HEAD 成功:" + success + "条,失败:" + fail + "条</br>"); success = fail = 0; coreDao.saveOrUpdate(dqKpiHeadList); rb.setData(dqKpiHeadList); sheet = wb.getSheet(1); rows = sheet.getRows(); List<SchDqKpiLine> dqKpiLineList = new ArrayList<SchDqKpiLine>(); SchDqKpiLine dqKpiLine = null; for (int row = 2; row < rows; row++) { if (!TopUtil.isValidValue(sheet.getCell(0, row).getContents())) { continue; } try { dqKpiLine = new SchDqKpiLine(); dqKpiLine.setLineId(new Long(this.checkIfNullToZero(sheet.getCell(0, row).getContents()))); dqKpiLine.setHeadId(new Long(this.checkIfNullToZero(sheet.getCell(1, row).getContents()))); dqKpiLine.setDtlCode(sheet.getCell(2, row).getContents()); dqKpiLine.setDtlName(sheet.getCell(3, row).getContents()); dqKpiLine.setExecSql(sheet.getCell(4, row).getContents()); dqKpiLine.setAttribute1(sheet.getCell(5, row).getContents()); dqKpiLine.setAttribute2(sheet.getCell(6, row).getContents()); dqKpiLine.setAttribute3(sheet.getCell(7, row).getContents()); dqKpiLine.setAttribute4(sheet.getCell(8, row).getContents()); dqKpiLine.setAttribute5(sheet.getCell(9, row).getContents()); dqKpiLine.setAttribute6(sheet.getCell(10, row).getContents()); dqKpiLine.setAttribute7(sheet.getCell(11, row).getContents()); dqKpiLine.setAttribute8(sheet.getCell(12, row).getContents()); dqKpiLine.setAttribute9(sheet.getCell(13, row).getContents()); dqKpiLine.setAttribute10(sheet.getCell(14, row).getContents()); dqKpiLine.setEnabledFlag(sheet.getCell(15, row).getContents()); dqKpiLine.setCreatedBy("import"); dqKpiLine.setCreatedDate(new Date()); dqKpiLine.setLastUpdatedBy("import"); dqKpiLine.setLastUpdatedDate(new Date()); dqKpiLineList.add(dqKpiLine); success++; } catch (Exception e) { log.error(e); e.printStackTrace(); fail++; continue; } } sb.append("表:SCH_DQ_KPI_LINE 成功:" + success + "条,失败:" + fail + "条</br>"); success = fail = 0; coreDao.saveOrUpdate(dqKpiLineList); rb.setMessage(sb.toString()); } catch (Exception e) { log.error(e); e.printStackTrace(); rb.setSuccess(false); rb.setMessage(e.getMessage()); } return rb; }
这是jxl解析excel(多个子表)的代码,权当记录,怕忘记。
以上是关于jxl解析多个excel工作表-java代码的主要内容,如果未能解决你的问题,请参考以下文章
JXL包大解析;Java程序生成excel文件和解析excel文件内容
JXL包大解析;Java程序生成excel文件和解析excel文件内容
如何通过java代码将access数据库表中的数据导出生成excel格式的文件,谢谢各位。