版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Liutt55/article/details/79030489
/** * 创建表格 * @param dataMap 数据 * @return */ public XSSFWorkbook createExcelCheck(Map<String, List<List<String>>> dataMap, boolean range){ XSSFWorkbook excel = new XSSFWorkbook(); for (String key: dataMap.keySet()) { List<List<String>> sheetList = dataMap.get(key); XSSFSheet sheet = excel.createSheet(key); //多个sheet/切换 if (range) createCellRange(sheet,sheetList); //如果true,设置合并对应单元格 XSSFRow row0 = sheet.createRow(0); List<String> headList = sheetList.get(0); for (int j = 0; j < headList.size(); j++) { //表头设置 sheet.setColumnWidth(j,20*256); String cellString = headList.get(j); XSSFCell cell = row0.createCell(j); XSSFCellStyle cellStyle = getStyle(excel,(short)12); cell.setCellStyle(cellStyle); cell.setCellValue(cellString); } for (int i = 1; i < sheetList.size(); i++) { List<String> rowList = sheetList.get(i); XSSFRow row = sheet.createRow(i); XSSFCellStyle cellStyle = getStyle(excel,(short)10); int rowSize = rowList.size(); for (int j = 0; j < rowSize; j++) { //表主体数据设置 if(j == (rowSize-1)) { if(StrKit.notBlank(rowList.get(j))) { // 将图片写入excel try { FileOutputStream fileOut = null; BufferedImage bufferImg = null; ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File("D:/talos"+rowList.get(j))); ImageIO.write(bufferImg, "jpg", byteArrayOut); //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点) XSSFDrawing patriarch = sheet.createDrawingPatriarch(); //anchor主要用于设置图片的属性 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255,(short) j, i, (short) j+1, i+1); anchor.setAnchorType(3); patriarch.createPicture(anchor, excel.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG)); } catch(Exception e) { e.printStackTrace(); continue; } } continue; } String cellString = rowList.get(j); XSSFCell cell = row.createCell(j); cell.setCellStyle(cellStyle); cell.setCellValue(cellString); } } } return excel; }
/** * 创建表格 * @param dataMap 数据 * @return */ public XSSFWorkbook createExcel(Map<String, List<List<String>>> dataMap, boolean range){ XSSFWorkbook excel = new XSSFWorkbook(); for (String key: dataMap.keySet()) { List<List<String>> sheetList = dataMap.get(key); XSSFSheet sheet = excel.createSheet(key); //多个sheet/切换 if (range) createCellRange(sheet,sheetList); //如果true,设置合并对应单元格 XSSFRow row0 = sheet.createRow(0); List<String> headList = sheetList.get(0); for (int j = 0; j < headList.size(); j++) { //表头设置 sheet.setColumnWidth(j,20*256); String cellString = headList.get(j); XSSFCell cell = row0.createCell(j); XSSFCellStyle cellStyle = getStyle(excel,(short)12); cell.setCellStyle(cellStyle); cell.setCellValue(cellString); } for (int i = 1; i < sheetList.size(); i++) { List<String> rowList = sheetList.get(i); XSSFRow row = sheet.createRow(i); XSSFCellStyle cellStyle = getStyle(excel,(short)10); for (int j = 0; j < rowList.size(); j++) { //表主体数据设置 String cellString = rowList.get(j); XSSFCell cell = row.createCell(j); cell.setCellStyle(cellStyle); cell.setCellValue(cellString); } } } return excel; }