钉钉机器人推送表格图片

需求:要做一个钉钉机器人推送表格信息,10分钟一次

难点:1钉钉机器人不支持推送excel,2钉钉推送的图片不是动态的,需要自己写一个方法,变成获取新生成的图片

技术方案

第一种:可以用图片服务器lfs,底层用netty写的,

第二种:生成的图片放到这台服务器上。然后再取

第二种简单,以下是第二种:

技术点:java 生成表格图片,钉钉api格式,服务器开通外网,linux字体上传。

生成表格图的类,表格的竖线和横线是写死的,你自己估算好数据的长度,自己定,竖线调整,这里边有一个华文行楷,linux如果没有就下载一个.ttf格式的

public class GridPictureUtil {
	public static String graphicsGeneration(List<List<List<String>>> allValue,List<String> titles,List<String[]> headers ,String receiver,int totalcol) throws Exception {
        int rows = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                rows += (2+typeV.size());
            }
        }
        // 实际数据行数+标题+备注
        int totalrow = 1+rows;
        //图片宽
        int imageWidth = 1600;
        //图片高
        int imageHeight = totalrow * 30 + 20;
        //行高
        int rowheight = 30;
        int startHeight = 10;
        int startWidth = 10;
        int colwidth = 200;
 
        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
 
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        //画背景
        graphics.setColor(new Color(0, 112, 192));
        int startH = 1;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.fillRect(startWidth+1, startHeight+startH*rowheight+1, imageWidth - startWidth-5-1,rowheight-1);
                startH+=2+typeV.size();
            }
        }
 
        graphics.setColor(new Color(220, 240, 240));
        // 画横线
 
        for (int j = 0; j < totalrow - 1; j++) {
            graphics.setColor(Color.black);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
                    startHeight + (j + 1) * rowheight);
        }
 
        // 画竖线
        graphics.setColor(Color.black);
        startH = 1;
        int rightLine = 0 ;
        for (List<List<String>> typeV : allValue) {
 
            if (typeV != null && typeV.size() > 0) {
                for (int k = 0; k < totalcol+1; k++) {
                    rightLine = getRightMargin(k,startWidth, colwidth,imageWidth);
                    graphics.drawLine(rightLine, startHeight + startH*rowheight, rightLine,
                            startHeight + (typeV.size()+1+startH)*rowheight);
                }
                startH+=2+typeV.size();
            }
        }
 
        // 设置字体
        Font font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);
 
        // 写标题
        startH = 1;
        int i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.drawString(titles.get(i), imageWidth / 3 + startWidth+30, startHeight + startH*rowheight - 10);
                startH+=2+typeV.size();
            }
            i++;
        }
 
 
        // 写入表头
        graphics.setColor(Color.WHITE);
        font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);
        startH = 2;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
 
                String[] headCells = headers.get(i);
                for (int m = 0; m < headCells.length; m++) {
                    rightLine = getRightMargin(m,startWidth, colwidth,imageWidth)+5;
                    graphics.drawString(headCells[m].toString(), rightLine,
                            startHeight + rowheight * startH - 10);
                }
                startH+=2+typeV.size();
            }
            i++;
        }
 
 
        // 写入内容
        graphics.setColor(Color.black);
        font = new Font("华文楷体", Font.PLAIN, 20);
        graphics.setFont(font);
        startH = 3;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                for (int n = 0; n < typeV.size(); n++) {
                    List<String> arr = typeV.get(n);
                    if(null!=arr&&arr.size()>0) {
                    	for (int l = 0; l < arr.size(); l++) {
                            rightLine = getRightMargin(l,startWidth, colwidth,imageWidth)+5;
                           if( null!=arr.get(l)) {
                        	   graphics.drawString(arr.get(l).toString(), rightLine,
                                       startHeight + rowheight * (n + startH) - 10);
                           }else {
                        	   graphics.drawString("0.000万", rightLine,
                                       startHeight + rowheight * (n + startH) - 10);
                           }
                        }
                    }
                }
                startH+=2+typeV.size();
            }
            i++;
        }
 
        String path = "/Users/quwending/Desktop/a/a.jpg";
        ImageIO.write(image, "jpg", new File(path));
        return path;
    }
 
    /**
     * 获取竖线和文字的水平位置
     * @param k
     * @param startWidth
     * @param colwidth
     * @param imageWidth
     * @return
     */
    private static int getRightMargin(int k, int startWidth, int colwidth, int imageWidth) {
        int rightLine = 0;
        if (k == 0) {
            rightLine = startWidth;
        } else if (k == 1) {
            rightLine = startWidth + colwidth ;
        } else if (k == 2) {
            rightLine = startWidth + 2 * colwidth;
        } else if (k == 3) {
            rightLine = startWidth + 3* colwidth;
        } else if (k == 4) {
            rightLine = startWidth + 4*colwidth;
        }else if (k == 5) {
        	rightLine = startWidth + 5*colwidth;
        }else if (k == 6) {
        	rightLine = startWidth + 6*colwidth;
        }else if (k == 7) {
        	rightLine = startWidth + 7*colwidth;
        }else if (k == 8) {
        	rightLine = imageWidth -10;
        }
        return rightLine;
    }

public static void initChartData() {
        List<List<List<String>>> allValue = new ArrayList<>();                                                                                
        List<String> content1 =Arrays.asList(new String[]{"南","230000000000","23.789","23","23","23","23","23"});  
         List<String> content2 = Arrays.asList(new String[]{"北","23","23","23","23","23","23","23"});  
         
         List<List<String>> contentArray1 = new ArrayList<>();
         contentArray1.add(content1);
         contentArray1.add(content2);

         
         allValue.add(contentArray1);
         List<String[]> headTitles = new ArrayList<>();
         String[] h1 = new String[]{"列标题1","列标题2","列标题3"  };
         headTitles.add(h1);
         List<String> titles = new ArrayList<>();
         titles.add("总标题");
         try {
			graphicsGeneration(allValue,titles,headTitles ,"",8);
		} catch (Exception e) {
			e.printStackTrace();
		}
 }
}

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值