情境:今天在项目中需要将excel中的数据导入到数据库中,使用poi来完成excel
中数据的导入!
步骤如下:(我的导入有些特殊,各位可以按照需求截取代码)
1、导入依赖
<dependency> <groupId>org.apache.poigroupId> <artifactId>poi-ooxmlartifactId> <version>3.14version> dependency>
2、前端十分简单,我就不班门弄斧了
这里要注意的是,我在controller层使用的是springmvc的MultipartFile来接收Excel文件,
所以在前台提交的form表单中要加上 :enctype="multipart/form-data"
<form id="FmImport_Excel" method="post" enctype="multipart/form-data"> <table class="editForm"> <tr class="editForm"> <td class="efLb"><label>月份:label>td> <td class="efEditRight"><input class="easyui-monthbox" data-options="required:true" id="month" name="month" style="width:350px;"/>td> tr> <tr class="editForm"> <td class="efLb"><label>单位:label>td> <td class="efEditRight"><input class="easyui-combotree" data-options="required:true,panelHeight:'auto',url:'${pageContext.request.contextPath}/basorg/queryOrgTreeRealNew'" id="orgName" name="orgName" style="width:350px;"/>td> tr> <tr class="editForm"> <td class="efLb"><label>附件:label>td> <td class="efEditRight"><input class="easyui-filebox" id="file" name="file" style="width:350px;"/>td> tr> <tr class="editForm"> <td class="efLb"><label> label>td> <td class="efEditRight">请上传.xls格式的附件td> tr> table> form>
3)接下来在controller层接收整个表单,然后进行数据的处理
/**
* @Description: 从 excel 导入数据到 资产负债,利润导入
* @param
* @author houChen
* @date
*/
@RequestMapping(value = "/importFmImportExcel")
@ResponseBody
public Object importFmImportExcel(@RequestParam("file")MultipartFile file,FmImport fmImport) throws IOException {
fmimportService.importFmImportExcel(file,fmImport);
return new JsonMsg();
}
4)service层对数据进行逻辑处理
/** * @Description: * @param file * @author houChen * @date 2020/7/2 18:27 */ public void importFmImportExcel(MultipartFile file,FmImport fmImport) throws IOException { // 1、校验file文件 if(file==null){ logger.error("文件不存在!"); throw new FileNotFoundException("文件不存在!"); } String fileName = file.getOriginalFilename(); //校验文件类型 if(!fileName.endsWith("xls") && !fileName.endsWith("xlsx")){ logger.error(fileName + "不是excel文件"); throw new IOException(fileName + "不是excel文件"); } //2、保存主表内容 fmImport.setModifyAccount(SysUtils.getLoginAccountId()); fmImport.setModifyTime(new Date()); fmImport.setIsUse(SysContent.IS_USE_Y); if(fmImport.getId()==null){ fmImport.setCreateAccount(SysUtils.getLoginAccountId()); fmImport.setCreateTime(new Date()); } saveFmImport(fmImport); //3、保存子表 资产负债和盈利 Workbook workbook = null; //获取excel文件的io流 InputStream is = file.getInputStream(); //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象 if(fileName.endsWith("xls")){ //2003 workbook = new HSSFWorkbook(is); }else { //2007 workbook = new XSSFWorkbook(is); } int sheetNum = workbook.getNumberOfSheets(); //这里有三次导入,针对不同的子表 for(int i=0;i) { Sheet sheet=workbook.getSheetAt(i); if(sheet!=null && i==0 && sheet.getSheetName().contains("负债") && !workbook.isSheetHidden(i)) { // 从 excel中导入资产负债的数据 ImportFmBalanceSheet(sheet,fmImport.getId()); } if(sheet!=null && i==1 && sheet.getSheetName().contains("利润") && !workbook.isSheetHidden(i)) { // 从 excel中导入资产负债的数据 ImportFmIncomeStatement(sheet,fmImport.getId()); } if(sheet!=null && i==2 && sheet.getSheetName().contains("现金") && !workbook.isSheetHidden(i)) { // 从 excel中导入资产负债的数据 ImportFmCashStatement(sheet,fmImport.getId()); } } }
/** * @Description: 从 excel中导入资产负债的数据 * @param mainId fm_import的主键 id * @author houChen * @date 2020/7/3 9:51 */ public void ImportFmBalanceSheet(Sheet sheet,Long mainId){ //获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); //获得当前sheet的结束行 int lastRowNum = sheet.getLastRowNum(); //循环所有行 for(int rowNum = firstRowNum+4;rowNum <= lastRowNum;rowNum++){ //获得当前行 Row row = sheet.getRow(rowNum); if(row == null){ continue; } //获得当前行的开始列 int firstCellNum = row.getFirstCellNum(); //获得当前行的列数 int lastCellNum = row.getPhysicalNumberOfCells(); FmBalanceSheet balanceSheet =new FmBalanceSheet(); FmBalanceSheet balanceSheetReplicate =new FmBalanceSheet(); for(int i = firstCellNum; i < lastCellNum;i++){ Cell cell = row.getCell(i); String cellValue = getCellValue(cell); if(StringUtils.isNotBlank(cellValue)&&!"非法字符".equals(cellValue)&&!"未知类型".equals(cellValue)){ switch (i){ //设置 case 0 : balanceSheet.setItemName(cellValue); break; case 1 : balanceSheet.setDisplayNo(Long.parseLong(cellValue)); break; case 2 : balanceSheet.setEndingBalance(Double.parseDouble(cellValue)); break; case 3 : balanceSheet.setBeginningBalance(Double.parseDouble(cellValue)); break; case 4 : balanceSheetReplicate.setItemName(cellValue); break; case 5 : balanceSheetReplicate.setDisplayNo(Long.parseLong(cellValue)); break; case 6 : balanceSheetReplicate.setEndingBalance(Double.parseDouble(cellValue)); break; case 7 : balanceSheetReplicate.setBeginningBalance(Double.parseDouble(cellValue)); break; default: break; } } } balanceSheet.setOrgId(SysUtils.getLoginUser().getOrgid()); balanceSheet.setModifyAccount(SysUtils.getLoginAccountId()); balanceSheet.setModifyTime(new Date()); balanceSheet.setIsUse(SysContent.IS_USE_Y); if(balanceSheet.getId()==null){ balanceSheet.setCreateAccount(SysUtils.getLoginAccountId()); balanceSheet.setCreateTime(new Date()); } balanceSheet.setCol(1L); //第一列 balanceSheet.setMainId(mainId); if(StringUtils.isNotBlank(balanceSheet.getItemName())){ fmBalanceSheetService.saveFmBalanceSheet(balanceSheet); } balanceSheetReplicate.setOrgId(SysUtils.getLoginUser().getOrgid()); balanceSheetReplicate.setModifyAccount(SysUtils.getLoginAccountId()); balanceSheetReplicate.setModifyTime(new Date()); balanceSheetReplicate.setIsUse(SysContent.IS_USE_Y); if(balanceSheetReplicate.getId()==null){ balanceSheetReplicate.setCreateAccount(SysUtils.getLoginAccountId()); balanceSheetReplicate.setCreateTime(new Date()); } balanceSheetReplicate.setCol(2L); //第一列 balanceSheetReplicate.setMainId(mainId); if(StringUtils.isNotBlank(balanceSheetReplicate.getItemName())){ fmBalanceSheetService.saveFmBalanceSheet(balanceSheetReplicate); } } }
用到的工具类
public static String getCellValue(Cell cell){ String cellValue = ""; if(cell == null){ return cellValue; } //把数字当成String来读,避免出现1读成1.0的情况 if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ cell.setCellType(Cell.CELL_TYPE_STRING); } //判断数据的类型 switch (cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC: //数字 cellValue = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: //字符串 cellValue = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_ERROR: //故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } return cellValue; }
5)结果
参考博客啊:
https://www.cnblogs.com/guanghe/p/10002446.html