废话不多说,直接上代码,上效果图
@RestController
@Api(tags = “报表”)
@RequestMapping("/export")
@CrossOrigin
public class exportContriller {
//图片上传地址
@Value("${queryurl}")
private String queryurl;
private int indexExport = 0;
private int indexExportCell = 0;
private Map<String, Object> mapWork;
//图片上传地址文件夹
private static final String pathUr=null;
private List<Map<String, Object>> listMap;

@ApiOperation("查询文件详情套用模板,返回base64字符")
@GetMapping("/officeModel")
@ApiImplicitParams({})
public void officeModel(HttpServletResponse response, String workId) {try {//查询隐蔽工程信息mapWork = ContUtil.getHiddenWorkById(workId, queryurl);//查询该隐蔽工程下面的图片信息listMap = ContUtil.getPictureByWorkId(workId, queryurl);//查询项目、合同Map<String, Object> mapProj = new HashMap<>();if (mapWork.get("contId").toString() != null && !StringUtil.isEmpty(mapWork.get("contId").toString())) {mapProj = ContUtil.getContrctNameBytId(mapWork.get("contId").toString(), queryurl);} else {return;}// 解析成excel结构体XSSFWorkbook workbook = new XSSFWorkbook();//生成一个表格,设置表格名称XSSFSheet sheet = workbook.createSheet("隐蔽工程及工序流程影像");//设置表格列宽度sheet.setDefaultColumnWidth(100);// 字体样式XSSFFont xssfFont = workbook.createFont();// 加粗xssfFont.setBold(true);// 字体名称xssfFont.setFontName("楷体");// 字体大小xssfFont.setFontHeight(12);// 表头样式XSSFCellStyle headStyle = workbook.createCellStyle();// 设置字体cssheadStyle.setFont(xssfFont);// 竖向居中headStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 横向居中headStyle.setAlignment(HorizontalAlignment.CENTER);// 边框headStyle.setBorderBottom(BorderStyle.THIN);headStyle.setBorderLeft(BorderStyle.THIN);headStyle.setBorderRight(BorderStyle.THIN);headStyle.setBorderTop(BorderStyle.THIN);// 内容字体样式XSSFFont contFont = workbook.createFont();// 加粗contFont.setBold(false);// 字体名称contFont.setFontName("楷体");// 字体大小contFont.setFontHeight(11);// 内容样式XSSFCellStyle contentStyle = workbook.createCellStyle();// 设置字体csscontentStyle.setFont(contFont);// 竖向居中contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 横向居中//contentStyle.setAlignment(HorizontalAlignment.CENTER);// 边框contentStyle.setBorderBottom(BorderStyle.THIN);contentStyle.setBorderLeft(BorderStyle.THIN);contentStyle.setBorderRight(BorderStyle.THIN);contentStyle.setBorderTop(BorderStyle.THIN);// 自动换行contentStyle.setWrapText(true);// 数字样式XSSFCellStyle numStyle = workbook.createCellStyle();// 设置字体cssnumStyle.setFont(contFont);// 竖向居中numStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 横向居中numStyle.setAlignment(HorizontalAlignment.CENTER);// 边框numStyle.setBorderBottom(BorderStyle.THIN);numStyle.setBorderLeft(BorderStyle.THIN);numStyle.setBorderRight(BorderStyle.THIN);numStyle.setBorderTop(BorderStyle.THIN);// 标题字体样式XSSFFont titleFont = workbook.createFont();// 加粗titleFont.setBold(false);// 字体名称titleFont.setFontName("宋体");// 字体大小titleFont.setFontHeight(20);// 标题样式XSSFCellStyle titleStyle = workbook.createCellStyle();titleStyle.setFont(titleFont);// 竖向居中titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 横向居中titleStyle.setAlignment(HorizontalAlignment.CENTER);// 边框titleStyle.setBorderBottom(BorderStyle.THIN);titleStyle.setBorderLeft(BorderStyle.THIN);titleStyle.setBorderRight(BorderStyle.THIN);titleStyle.setBorderTop(BorderStyle.THIN);showCell(workbook,sheet, listMap);// 创建第一行XSSFRow row1 = sheet.createRow(0);// 创建第一行第一列XSSFCell row1Cell1 = row1.createCell(0);row1Cell1.setCellValue("隐蔽工程及工序流程影像");row1Cell1.setCellStyle(titleStyle);XSSFCell row1Cell2 = row1.createCell(1);// 为了保证合并的单元格能有效追加外框、被合并的单元格、内容要设置为空row1Cell2.setCellValue("");row1Cell2.setCellStyle(titleStyle);XSSFCell row1Cell3 = row1.createCell(2);row1Cell3.setCellValue("");row1Cell3.setCellStyle(titleStyle);XSSFCell row1Cell4 = row1.createCell(3);row1Cell4.setCellValue("");row1Cell4.setCellStyle(titleStyle);XSSFCell row1Cell5 = row1.createCell(4);row1Cell5.setCellValue("");row1Cell5.setCellStyle(titleStyle);XSSFCell row1Cell6 = row1.createCell(5);row1Cell6.setCellValue("");row1Cell6.setCellStyle(titleStyle);// 创建第二行XSSFRow row2 = sheet.createRow(1);// 创建第二行第一列XSSFCell row2Cell1 = row2.createCell(0);row2Cell1.setCellValue(mapProj.get("projName") + "" + mapProj.get("contName") + "                 影像资料");row2Cell1.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell1.getColumnIndex(), 100 * 50);XSSFCell row2Cell2 = row2.createCell(1);// 为了保证合并的单元格能有效追加外框、被合并的单元格、内容要设置为空row2Cell2.setCellValue("0");row2Cell2.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell2.getColumnIndex(), 100 * 50);XSSFCell row2Cell3 = row2.createCell(2);row2Cell3.setCellValue("");row2Cell3.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell3.getColumnIndex(), 100 * 50);XSSFCell row2Cell4 = row2.createCell(3);row2Cell4.setCellValue("");row2Cell4.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell4.getColumnIndex(), 100 * 50);XSSFCell row2Cell5 = row2.createCell(4);row2Cell5.setCellValue("");row2Cell5.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell5.getColumnIndex(), 100 * 50);XSSFCell row2Cell6 = row2.createCell(5);row2Cell6.setCellValue("");row2Cell6.setCellStyle(headStyle);sheet.setColumnWidth(row2Cell6.getColumnIndex(), 100 * 50);generate(sheet, numStyle, listMap);//八进制输出流response.setContentType("application/octet-stream");//设置导出Excel的名称response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("测试.xls", "UTF-8"));//刷新缓冲response.flushBuffer();//workbook将Excel写入到response的输出流中,供页面下载该Excel文件workbook.write(response.getOutputStream());//关闭workbookworkbook.close();} catch (Exception e) {e.printStackTrace();}
}
//单元格合并后边框样式
public void merge(CellRangeAddress cell, XSSFSheet sheet) {RegionUtil.setBorderBottom(BorderStyle.THIN, cell, sheet);RegionUtil.setBorderTop(BorderStyle.THIN, cell, sheet);RegionUtil.setBorderLeft(BorderStyle.THIN, cell, sheet);RegionUtil.setBorderRight(BorderStyle.THIN, cell, sheet);
}
//合同单元格
public void showCell(XSSFWorkbook workbook,XSSFSheet sheet, List<Map<String, Object>> listMap) {listMap.forEach(list -> {// 合并单元格(第一行、标题)CellRangeAddress cAddress = new CellRangeAddress(indexExport, indexExport, indexExport, indexExport + 5);sheet.addMergedRegion(cAddress);merge(cAddress, sheet);// 合并单元格(第一个分类)CellRangeAddress cAddress2 = new CellRangeAddress(indexExport + 1, indexExport + 1, 0, 5);sheet.addMergedRegion(cAddress2);merge(cAddress2, sheet);// 合并单元格(第一个分类)CellRangeAddress cAddress3 = new CellRangeAddress(indexExport + 2, indexExport + 5, 4, 4);sheet.addMergedRegion(cAddress3);merge(cAddress3, sheet);//合并单元格CellRangeAddress cAddress4 = new CellRangeAddress(indexExport + 2, indexExport + 5, 5, 5);sheet.addMergedRegion(cAddress4);merge(cAddress4, sheet);CellRangeAddress cAddress5 = new CellRangeAddress(indexExport + 6, indexExport + 9, 4, 4);sheet.addMergedRegion(cAddress5);merge(cAddress5, sheet);CellRangeAddress cAddress6 = new CellRangeAddress(indexExport + 6, indexExport + 9, 5, 5);sheet.addMergedRegion(cAddress6);merge(cAddress6, sheet);CellRangeAddress cAddress7 = new CellRangeAddress(indexExport + 10, indexExport + 13, 4, 4);sheet.addMergedRegion(cAddress7);merge(cAddress7, sheet);CellRangeAddress cAddress8 = new CellRangeAddress(indexExport + 10, indexExport + 13, 5, 5);sheet.addMergedRegion(cAddress8);merge(cAddress8, sheet);CellRangeAddress cAddress9 = new CellRangeAddress(indexExport + 14, indexExport + 17, 4, 4);sheet.addMergedRegion(cAddress9);merge(cAddress9, sheet);CellRangeAddress cAddress10 = new CellRangeAddress(indexExport + 14, indexExport + 17, 5, 5);sheet.addMergedRegion(cAddress10);merge(cAddress10, sheet);CellRangeAddress cAddress11 = new CellRangeAddress(indexExport + 18, indexExport + 21, 4, 4);sheet.addMergedRegion(cAddress11);merge(cAddress11, sheet);CellRangeAddress cAddress12 = new CellRangeAddress(indexExport + 18, indexExport + 21, 5, 5);sheet.addMergedRegion(cAddress12);merge(cAddress12, sheet);CellRangeAddress cAddress14 = new CellRangeAddress(indexExport + 22, indexExport + 24, 2, 2);sheet.addMergedRegion(cAddress14);merge(cAddress14, sheet);CellRangeAddress cAddress15 = new CellRangeAddress(indexExport + 22, indexExport + 24, 3, 5);sheet.addMergedRegion(cAddress15);merge(cAddress15, sheet);CellRangeAddress cAddress13 = new CellRangeAddress(indexExport + 2, indexExport + 21, 0, 3);sheet.addMergedRegion(cAddress13);merge(cAddress13, sheet);String fileType = "";//将图片插入到指定的列if(list.get("fileName")!=null){String fileString = list.get("fileName").toString();fileType=fileString.substring(fileString.indexOf("."),fileString.length());}picture(workbook,sheet,list.get("fileUrl").toString(),list.get("fileName").toString(),fileType,indexExport + 2,indexExport + 21);indexExport = indexExport + 24;});
}
//将数据插入到指定的单元格中
public void generate(XSSFSheet sheet, XSSFCellStyle numStyle, List<Map<String, Object>> listMap) {listMap.forEach(list -> {// 创建第三行XSSFRow row3 = sheet.createRow(indexExportCell + 2);// 创建第三行第四列XSSFCell row3Cell4 = row3.createCell(4);row3Cell4.setCellValue("名称");row3Cell4.setCellStyle(numStyle);// 创建第三行第五列XSSFCell row3Cell5 = row3.createCell(5);row3Cell5.setCellValue(mapWork.get("checkPart").toString());row3Cell5.setCellStyle(numStyle);// 创建第四行XSSFRow row4 = sheet.createRow(indexExportCell + 6);// 创建第四行第四列XSSFCell row4Cell4 = row4.createCell(4);row4Cell4.setCellValue("图片编号");row4Cell4.setCellStyle(numStyle);// 创建第四行第四列XSSFCell row4Cell5 = row4.createCell(5);row4Cell5.setCellValue("");row4Cell5.setCellStyle(numStyle);// 创建第五行XSSFRow row5 = sheet.createRow(indexExportCell + 10);// 创建第五行第四列XSSFCell row5Cell4 = row5.createCell(4);row5Cell4.setCellValue("摄影者");row5Cell4.setCellStyle(numStyle);// 创建第五行第四列XSSFCell row5Cell5 = row5.createCell(5);row5Cell5.setCellValue("");row5Cell5.setCellStyle(numStyle);// 创建第五行XSSFRow row6 = sheet.createRow(indexExportCell + 14);// 创建第五行第四列XSSFCell row6Cell4 = row6.createCell(4);row6Cell4.setCellValue("日期");row6Cell4.setCellStyle(numStyle);// 创建第五行第四列XSSFCell row6Cell5 = row6.createCell(5);row6Cell5.setCellValue("");row6Cell5.setCellStyle(numStyle);// 创建第五行XSSFRow row7 = sheet.createRow(indexExportCell + 18);// 创建第五行第四列XSSFCell row7Cell4 = row7.createCell(4);row7Cell4.setCellValue("时间");row7Cell4.setCellStyle(numStyle);// 创建第五行第四列XSSFCell row7Cell5 = row7.createCell(5);row7Cell5.setCellValue("");row7Cell5.setCellStyle(numStyle);XSSFRow row8 = sheet.createRow(indexExportCell + 22);// 创建第6行第1列XSSFCell row8Cell1 = row8.createCell(0);row8Cell1.setCellValue("施工现场负责人");row8Cell1.setCellStyle(numStyle);// 创建第6行第2列XSSFCell row8Cell2 = row8.createCell(1);row8Cell2.setCellValue("胡必飞");row8Cell2.setCellStyle(numStyle);// 创建第6行第3列XSSFCell row8Cell3 = row8.createCell(2);row8Cell3.setCellValue("照片说明");row8Cell3.setCellStyle(numStyle);// 创建第6行第3列XSSFCell row8Cell4 = row8.createCell(3);row8Cell4.setCellValue(mapWork.get("remarke").toString());row8Cell4.setCellStyle(numStyle);XSSFRow row9 = sheet.createRow(indexExportCell + 23);// 创建第7行第1列XSSFCell row9Cell1 = row9.createCell(0);row9Cell1.setCellValue("质检负责人");row9Cell1.setCellStyle(numStyle);// 创建第7行第2列XSSFCell row9Cell2 = row9.createCell(1);row9Cell2.setCellValue("张力");row9Cell2.setCellStyle(numStyle);XSSFRow row10 = sheet.createRow(indexExportCell + 24);// 创建第7行第1列XSSFCell row10Cell1 = row10.createCell(0);row10Cell1.setCellValue("现场监理");row10Cell1.setCellStyle(numStyle);// 创建第7行第2列XSSFCell row10Cell2 = row10.createCell(1);row10Cell2.setCellValue("李犁");row10Cell2.setCellStyle(numStyle);indexExportCell = indexExportCell + 24;});
}
//将图片插入到指定的单元格中
public void picture(XSSFWorkbook workbook,XSSFSheet sheet, String fileUrl, String fileId, String fileType,int row, int col) {try {XSSFDrawing patriarch = sheet.createDrawingPatriarch();//生成一个表格,设置表格名称String urlFile = pathUrl + fileUrl + fileId;// 构造URLURL url = new URL(urlFile);// 打开连接URLConnection con = url.openConnection();//设置请求超时为5scon.setConnectTimeout(5 * 1000);int cols = col + 1;int rows = row - 1;// 输入流InputStream is = con.getInputStream();ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();BufferedImage bufferImg = ImageIO.read(is);if (fileType.equals(".jpg")) {ImageIO.write(bufferImg, "jpg", byteArrayOut);} else if (fileType.equals(".png")) {ImageIO.write(bufferImg, "png", byteArrayOut);}XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 200,100, 1, (short)row, 3, (short)col);patriarch.createPicture(anchor1, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));} catch (Exception e) {e.printStackTrace();}
}

}

该功能只是适应我的功能需求,没有做很多优化,要根据自己的实际情况调整自己的表格。

JAVA通过poi实现excel表格制作并且将图片放入到指定的单元格中(可以循环插入)相关推荐

  1. 利用VBA将excel中链接转换为图片形式存放在指定的单元格中

    利用VBA将excel中链接转换为图片形式存放在指定的单元格中 VBA代码如下 Private Declare Function URLDownloadToFile Lib "urlmon& ...

  2. java通过poi生成excel表格(自适应列宽、合并单元格后的边框添加)

    具体java通过POI读写Excel的基本使用方法可参考: POI读写Excel的基本使用 1.项目导入依赖: <!--xls--> <dependency><group ...

  3. Java 通过POI 操作excel表格:读取、写入

    Java操作excel表格是很常见的,在下面的例子中我将展示使用Java如何对excel进行读取和写入操作.(我是在ssm中实现的) 一.导入依赖 (如果是在普通Java项目中需要导入相应jar包) ...

  4. java使用poi读取excel表格

    前言 用户上传excel表格,后端读取excel中的数据并存入数据库. 这里只演示如何读取到excel表格中的内容,excel中包括图片.日期.字符串格式. 导入依赖 <dependency&g ...

  5. java 使用POI简单excel表格导出,通过浏览器直接下载

    pom.xl依赖 <!--POI Excel表格导出依赖--> <dependency><groupId>org.apache.poi</groupId> ...

  6. vue导出excel表格,自定义字体样式,字体颜色,合并单元格,单元格填充色-xlsx以及xlsx-style,JSZip为2.XX版本

    先装包 安装注意 import XLSX from "xlsx-style"报错:This relative module was not found: ./cptable in ...

  7. excel小写转大写公式_Excel办公技巧:快速将单元格中小写字母全部转换为大写字母...

    应用场景 将下图表格A列"项目编号"中所有的小写字母"a""b"全部变为大写字母. 操作步骤 1.在"项目编号"列和&q ...

  8. excel表格制作教程

    excel表格制作教程入门 Excel是常用的办公软件,可以大幅提升我们的工作效率:下面就为大家介绍:excel表格制作教程入门,及常用公式! 当前操作环境为电脑端,使用笔记本:戴尔灵越5000,对应 ...

  9. java性别分类汇总,excel表格数据男女分类汇总-在Excel中,对数据清单进行“按性别分类汇总出男女......

    在Excel中,对数据清单进行"按性别分类汇总出男女... 在Excel中,对数据清单进行性别分类汇总出男女生的英语平均分"可使用AVERAGEIF函数实现. 操骤如下: 1.打开 ...

最新文章

  1. git常用命令及配置
  2. java空心正方形代码_从Java中的用户输入绘制空心星号正方形/矩...
  3. cmb网站服务器失去响应代码,3.5.2 连接到服务器并响应各种连接事件(2)
  4. 开源无收入,全职开源仍是少数,企业开源成热门,开发者正在见证开源历史
  5. Python+OpenCV:高动态范围(High Dynamic Range, HDR)
  6. 这次国足想不出线都难
  7. OPNET常用编译器使用实例和相关问题
  8. 浅谈外网通过反向代理访问内网资源时的权限保护
  9. 用树莓派做一个语音机器人
  10. Mohican_4/6 C语言 移位运算 代码#FloatToInt
  11. testin云测操作
  12. Docker_03_彻底搞懂Dockerfile文件
  13. 早期RA获超声缓解的基线预测因素
  14. ESTIMATE包计算肿瘤纯度
  15. AMD GPU的断点指令
  16. 自然语言处理在eBay的技术实践
  17. 工业智能网关BL110应用之45:实现欧姆龙 PLC CJ/CS/CP 接入华为云平台
  18. 北航计算机技术硕士含金量,中科院软件工程的工程硕士含金量怎么样?对比清华,北大,北邮和北航?...
  19. 初识 OpenGL ES2.0
  20. 2021年低压电工考试及低压电工作业模拟考试

热门文章

  1. PE文件资源解析(十一)对话框资源的解析
  2. 使用 docker 对容器资源进行限制
  3. MTK android系统源码修改快速上手
  4. python自动生成统计表_python处理Excel自动统计表
  5. 软考证书如何评职称?
  6. ECB oracle——attack
  7. Word VBA自动排版(1)-新建窗口,并且设置并列分布
  8. Protocol handler start failed
  9. Dreamweaver CS6 新建站点
  10. 【Ubuntu安装后基本配置】