【根据excel模板写入数据并分页多sheet导出】 您所在的位置:网站首页 vba将数据导入模版并生成新的工作薄 【根据excel模板写入数据并分页多sheet导出】

【根据excel模板写入数据并分页多sheet导出】

2024-06-30 06:19| 来源: 网络整理| 查看: 265

根据excel模板写入数据并分页多sheet导出

这是前段时间遇到的一个需求,当时看了一些大佬们文档,花了一些时间才搞定的。功能到是实现了,就是不知道怎样写可以更优雅一些,现在贴出来记录一下:

主要思路大概分为以下几步:

导入相关poi依赖根据路径读取模板,建立workbook对象,获取模板sheet页将数据列表遍历并复制模板页建立新的sheet,填充数据填充完写入到本地文件中

poi依赖,需要注意一下版本必须一样:

org.apache.poi poi 3.12 org.apache.poi poi-scratchpad 3.12 org.apache.poi poi-ooxml 3.12

主要实现代码:

...... //定义文件对象,输入,输出流对象 File file = new File(machinePurchaseExcel); FileInputStream fis; FileOutputStream fos; //获取采购单模板 fis = new FileInputStream(file); //根据模板创建Excel工作簿 HSSFWorkbook workbook = new HSSFWorkbook(fis); //定义模板页 HSSFSheet sheet = workbook.getSheetAt(0); //定义新页面 HSSFSheet newSheet; //定义map封装简单数据 Map tmp =new HashMap(0); //设置年月日 Calendar calendar = Calendar.getInstance(); tmp.put("year", calendar.get(Calendar.YEAR) + ""); String month = (calendar.get(Calendar.MONTH) + 1) + ""; tmp.put("month", month); tmp.put("day", day); //遍历输出多个sheet页 for (int i = 0; i public static void ExcelByModel(String ExcelName, String ModelURl, List dataSource,HttpServletResponse response, String[] sheetNames, String[] keyNames, int rowNum) throws Exception { // 设置导出Excel报表的导出形式 response.setContentType("application/vnd.ms-excel"); // 设置导出Excel报表的响应文件名 String fileName = new String(ExcelName.getBytes("utf-8"), "ISO-8859-1"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls"); // 创建一个输出流 OutputStream fileOut = response.getOutputStream(); // 读取模板文件路径 File file = new File(ModelURl); FileInputStream fins = new FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(fins); // 读取Excel模板 HSSFWorkbook wb = new HSSFWorkbook(fs); // 设置边框样式 HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置边框样式的颜色 style.setBottomBorderColor(HSSFColor.BLACK.index); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setRightBorderColor(HSSFColor.BLACK.index); style.setTopBorderColor(HSSFColor.BLACK.index); // 模板页 HSSFSheet sheetModel = null; // 新建的Sheet页 HSSFSheet newSheet = null; // 创建行 HSSFRow row = null; // 创建列 HSSFCell cell = null; // 循环建立Sheet页 for (int i = 0; i // 循环遍历,新建行 row = newSheet.createRow((short) num); //判断有多少列数据 for (int j = 0; j // 复制一个单元格样式到新建单元格 if ((firstrow == -1) || (lasttrow == -1) || lasttrow region = fromsheet.getMergedRegionAt(i); if ((region.getRowFrom() >= firstrow) && (region.getRowTo() fromRow = fromsheet.getRow(i); if (fromRow != null) { for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) { int colnum = fromsheet.getColumnWidth((short) j); if (colnum > 100) { newSheet.setColumnWidth((short) j, (short) colnum); } if (colnum == 0) { newSheet.setColumnHidden((short) j, true); } else { newSheet.setColumnHidden((short) j, false); } } break; } } // 复制行并填充数据 for (int i = 0; i continue; } newRow = newSheet.createRow(i - firstrow); newRow.setHeight(fromRow.getHeight()); //fromRow.getPhysicalNumberOfCells()是获取不为空的列个数 for (int j = fromRow.getFirstCellNum(); j continue; } newCell = newRow.createCell((short) j); newCell.setCellStyle(fromCell.getCellStyle()); int cType = fromCell.getCellType(); newCell.setCellType(cType); switch (cType) { case HSSFCell.CELL_TYPE_STRING: newCell.setCellValue(fromCell.getRichStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: newCell.setCellValue(fromCell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: newCell.setCellValue(fromCell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BOOLEAN: newCell.setCellValue(fromCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: newCell.setCellValue(fromCell.getErrorCellValue()); break; default: newCell.setCellValue(fromCell.getRichStringCellValue()); break; } } } } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有