java基于POI批量插入图片到word

您所在的位置:网站首页 word中如何批量导入图片 java基于POI批量插入图片到word

java基于POI批量插入图片到word

#java基于POI批量插入图片到word| 来源: 网络整理| 查看: 265

java使用POI批量添加图片到word 前言生成效果图第一步 添加依赖第二步 创建要封装的表单数据第三步 将数据设置到word注意:文字换行赋值如下批量设置多张图、文字及描述 授人以鱼不如授人以渔(参考该API)

前言

我们的需求:可以批量的向word中添加图片和文本。在网上找了很久,找到的都是只能向word添加一张图片的,而且都是使用的freemark,一个是找到的需求不满足,另外一个是我也不想使用freemark,因此,我决定用java结合着poi自己手动写一个。

生成效果图

该报告为代码生成,达到的效果是影像和处理建议都可以动态插入多张图片和文字 在这里插入图片描述

第一步 添加依赖

该依赖为对poi封装后的依赖,还需自己添加相关poi依赖

com.deepoove poi-tl 1.6.0

如果你还没有poi依赖,那么可以使用下面的依赖(如果有请忽略下面这些依赖)

org.apache.poi poi 4.0.0 org.apache.poi poi-ooxml 4.0.0 org.apache.poi poi-excelant 4.0.0 org.apache.poi poi-ooxml-schemas 4.0.0 org.apache.poi poi-scratchpad 4.0.0 第二步 创建要封装的表单数据

CommonReportVo将这个类换成自己的实体类数据,这个方法主要目的是给表格里面的数据赋值并返回(此步骤根据自身实际情况赋值),这里表格是两列,因此我使用二维数组,如果你的word表格数据是多列,那么你可以使用一个实体类进行封装后返回。注:此部分代码仅做表格数据赋值参考

private String[][] setSatelliteReportTemplate(CommonReportVo commonReportVo) { String[] names = {"测试001","测试002,"测试0003","测试0004","测试0005","测试0006"}; String lon = GCSUtils.changeToDFM(Double.parseDouble(commonReportVo.getLon())); String lat = GCSUtils.changeToDFM(Double.parseDouble(commonReportVo.getLat())); String status = commonReportVo.getDisaterSituatuin(); String taskNo = commonReportVo.getTaskNo(); String taskRecord = commonReportVo.getTaskRecord(); //多张图片地址 对应测试0004 String analysisResult = commonReportVo.getDataAnalysisResult(); String suggestion = commonReportVo.getEmergencySuggestion(); String otherMaterial = "详见附件文件"; String[] values = {status,taskNo,taskRecord,analysisResult,suggestion,otherMaterial}; String[][] arrs = new String[names.length][2]; //设置行数据 for (int i = 0;i < names.length;i ++){ arrs[i][0] = names[i]; } //设置列数据 for (int i = 0;i < values.length;i ++){ arrs[i][1] = values[i]; } return arrs; } 第三步 将数据设置到word

在上面我们只赋值了表格数据,现在我们将设置标题、时间等其他字段并赋值。

//参数1:响应体 //参数2:上面的二维数据组,即表格中的数据 //参数3:设置标题、时间等字段的封装类 private File setInfo2CommonWord(HttpServletResponse response, String[][] reports,CommonReportVo commonReportVo) { //创建一个word XWPFDocument doc = new XWPFDocument(); XWPFParagraph title = doc.createParagraph(); XWPFRun xwpfRun = title.createRun(); //设置标题 测试0001报告 xwpfRun.setText(commonReportVo.getReportName() + "报告"); xwpfRun.setFontSize(16); //设置标题加粗 xwpfRun.setBold(true); //设置标题居中 title.setAlignment(ParagraphAlignment.CENTER); XWPFParagraph content1 = doc.createParagraph(); XWPFRun xwpfRun1 = content1.createRun(); xwpfRun1.setText("报告单位:测试一下"); //设置体字体大小 xwpfRun1.setFontSize(12); XWPFParagraph content2 = doc.createParagraph(); XWPFRun xwpfRun2 = content2.createRun(); xwpfRun2.setText("灾害报告时间:" + DateUtil.formatDate1(commonReportVo.getReportTime())); xwpfRun2.setFontSize(12); String imgResult = commonReportVo.getSatelliteImgResult(); try { //注意 这是创建表格 创建的是几行几列 这儿需要根据自己实际的表格行列数自己设置 //参数1:表示行 参数2:表示列 XWPFTable table = doc.createTable(reports.length, 2); //下面的两个for循环就是设置表格数据 这里仅提供样例的设置思路 for (int i = 0;i < reports.length; i ++){ //行 //得到列数据对象 XWPFTableCell cell = table.getRow(i).getCell(0); cell.setWidth("1500"); for (int j = 0; j < reports[0].length; j ++){ //列 //设置表格每个单元格的样式 XWPFParagraph paragraph = table.getRow(i).getCell(j).getParagraphs().get(0); //对齐方式 左边 paragraph.setAlignment(ParagraphAlignment.LEFT); XWPFRun run = paragraph.createRun(); //字体9号 run.setFontSize(9); //以下setPics方法为给表格设置多张图片和文字 看明白后根据自己的业务进行设置 //注意 修改此处,然后替换为自己的多张图片 if(i == 3 && j == 1){ //给对应的单元格设置多张图片 setPics(reports[i][j], table.getRow(i), j,imgResult); }else { //给单元格设置文本 String[] runTexts=reports[i][j].split("\r\n"); for (String s:runTexts){ XWPFRun tempRun=paragraph.createRun(); tempRun.setFontSize(9); tempRun.setText(s); tempRun.addBreak(); } } } } //设置表格为A4纸张 //注意 参数2:表格列数 需要改为自己的列数 TableTools.widthTable(table, MiniTableRenderData.WIDTH_A4_FULL, 2); //以下方式自行选择 //直接下载 方式一 String fileName =commonReportVo.getReportName() + "测试报告"; // OutputStream out = response.getOutputStream(); // response.setHeader("Content-Type", "application/ms-winword"); // response.addHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + ".docx\""); // response.setContentType("application/octet-stream"); // response.setCharacterEncoding("UTF-8"); // doc.write(out); //返回文件的方式下载 方式二 File file = new File(filePath+File.separator+fileName + ".docx"); FileOutputStream fos = new FileOutputStream(file); doc.write(fos); doc.close(); return file; } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } return null; } 注意:文字换行赋值如下

文字描述如果需要换行时要在设置赋值时的文本中添加\r\n,如:imgResult这个字段中的值是这样的, imgResult:这是一段测试文字\r\n这是一段测试文字\r\n这是一段测试文字 那么取值后给对应的单元格赋值时这样的

String[] runTexts=imgResult.split("\r\n"); for (String s:runTexts){ run=p1.createRun(); run.setFontSize(9); run.setText(s); run.addBreak(); } 批量设置多张图、文字及描述 //参数1:对应样例中测试0004的多张图片的路径 //参数2:行对象 //参数3:图片上方的文字描述 private void setPics(String analysisResult, XWPFTableRow row, int j,String imgResult) throws InvalidFormatException, IOException { XWPFParagraph p1 = row.getCell(j).getParagraphs().get(0); //通过分隔符获取多张图片路径 String[] filePaths = analysisResult.split(","); if(filePaths.length > 0){ XWPFRun run = null; //设置图片上的文字描述 String[] runTexts=imgResult.split("\r\n"); for (String s:runTexts){ run=p1.createRun(); run.setFontSize(9); run.setText(s); run.addBreak(); } //run.addBreak(); //换行 //一个单元格中每行设置几张图片 我这里设置的是每行2张图片 然后换行 /注意: 每行几张图片根据自己业务修改 for (int k = 0;k < filePaths.length; k +=2){ if(filePaths[k].toUpperCase().contains(".JPG")||filePaths[k].toUpperCase().contains(".PNG")||filePaths[k].toUpperCase().contains(".SVG")){ run.addTab(); //添加一个tab int index = filePaths[k].lastIndexOf("\\"); String newStr = filePaths[k].substring(index+1); int indexOf = newStr.lastIndexOf("."); String pic = newStr.substring(indexOf); int i = newStr.indexOf("_"); //获取文件名 String name = newStr.substring(0,i) + pic; //获取文件流 InputStream stream = new FileInputStream(filePaths[k]); //设置第一张图片 //参数1:图片流数据 参数2:图片类型 参数3 图片名称 参数4:图片宽度 参数5:图片高度 run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated"+k, Units.toEMU(128), Units.toEMU(128)); run.addTab(); run.addTab(); int lastK = k + 1; String name2 = ""; if( (lastK)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3