java poi 填充word(合并单元格,添加图片,设置字号)

/**
* 查询Word报表
 *
 * @param
 * @return
 */
@RequestMapping(value = "/wordQuery")
public BaseResponse wordQuery() throws Exception {
    //报表地址
    String modelDir = ExcelDir + "gwydjb.doc";
    //输出地址
    String importDir = ImportDir + "gwydjb.doc";
    //结果集
    List<Map<String, Object>> mapData = reportMapper.selectGwydjbData();
    Map<String, Object> map = mapData.get(0);

    Document doc = new Document(modelDir);
    DocumentBuilder builder = new DocumentBuilder(doc);
    //doc文档添加域名
    //1.插入--文档部件--域
    //2.域名:邮件合并
    //3.域代码:MERGEFIELD "A0101"
    //人员姓名
    builder.moveToMergeField("A0101");
    builder.write(map.get("a0101").toString());

    Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); //第1个表格
    //定义合并的开始列
    Cell cellStart = null;
    //定义合并的结束列
    Cell cellEnd =  null;

    //全日制学历学位
    if(!map.get("a6170").toString().equals("") && map.get("a6171").toString().equals("")){
        builder.moveToMergeField("A6171");
        builder.moveToMergeField("A6170");
        builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);//水平居左
        builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
        //合并的开始列
        cellStart = table.getRows().get(4).getCells().get(2); //第5行第3列
        //合并的结束列
        cellEnd = table.getRows().get(5).getCells().get(2);  //第6行第3列
        mergeCells(cellStart, cellEnd);
        builder.insertHtml("<font style='font-family:宋体;font-size:8pt;'>"+ map.get("a6170").toString() +"</font>");
    }
    //合并全日制毕业院校
    //合并的开始列
    cellStart = table.getRows().get(4).getCells().get(4); //第5行第5列
    //合并的结束列
    cellEnd = table.getRows().get(5).getCells().get(4);  //第6行第5列
    mergeCells(cellStart, cellEnd);

    // 图片填充使用 BufferedImage 接受图片
    BufferedImage image = ImageIO.read(new FileInputStream("/home/liangningning/图片/234.jpeg"));
    BufferedImage bufferedImage = resizeBufferedImage(image, 135, 330, true);
    builder.moveToMergeField("pic");
    builder.insertImage((BufferedImage) bufferedImage);

    doc.save(importDir);
    return setResultSuccess(importDir);
}
/**
  *      @Author:  lnn
  *  @Description:  合并单元格
  *  @CreateDate:  2021/11/26 下午5:54
*/
public static void mergeCells(Cell startCell, Cell endCell) {
    Table parentTable = startCell.getParentRow().getParentTable();
    //查找起始单元格和结束单元格的行和单元格索引
    Point startCellPos = new Point(startCell.getParentRow().indexOf(startCell), parentTable.indexOf(startCell.getParentRow()));
    Point endCellPos = new Point(endCell.getParentRow().indexOf(endCell), parentTable.indexOf(endCell.getParentRow()));
    //根据这些索引创建要合并的单元格范围。如果结束单元格在开始单元格之前,则反转每个索引。
    Rectangle mergeRange = new Rectangle(Math.min(startCellPos.x, endCellPos.x), Math.min(startCellPos.y, endCellPos.y), Math.abs(endCellPos.x-startCellPos.x) + 1,
            Math.abs(endCellPos.y-startCellPos.y) + 1);
    for (Row row : parentTable.getRows()) {
        for (Cell cell : row.getCells()) {
            Point currentPos = new Point(row.indexOf(cell), parentTable.indexOf(row));
            //检查当前单元格是否在合并范围内,然后将其合并。
            if (mergeRange.contains(currentPos)) {
                if (currentPos.x==mergeRange.x)
                    cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                else
                    cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

                if (currentPos.y == mergeRange.y)
                    cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                else
                    cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
            }
        }
    }
}

/**
 * Word调整bufferedimage大小
 * @param source BufferedImage 原始image
 * @param targetW int  目标宽
 * @param targetH int  目标高
 * @param flag boolean 是否同比例调整
 * @return BufferedImage  返回新image
 */
public static BufferedImage resizeBufferedImage(BufferedImage source, int targetW, int targetH, boolean flag) {
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    if (flag && sx > sy) {
        sx = sy;
        targetW = (int) (sx * source.getWidth());
    } else if(flag && sx <= sy){
        sy = sx;
        targetH = (int) (sy * source.getHeight());
    }
    if (type == BufferedImage.TYPE_CUSTOM) {
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else {
        target = new BufferedImage(targetW, targetH, type);
    }
    Graphics2D g = target.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}
上一篇:PDF转图片,拼接图片


下一篇:将pdf文件转长jpg图片