美文网首页
Java生成表格图片

Java生成表格图片

作者: 逆风踏雷 | 来源:发表于2022-05-31 19:41 被阅读0次

    package com.koolean.demo.util;

    import com.alibaba.fastjson.JSON;
    import com.koolean.demo.model.CoreInterface;
    import com.koolean.demo.model.Line95;
    import com.koolearn.framework.sardine.client.Sardine;
    import com.koolearn.framework.sardine.client.SardineClientBuilder;
    import com.koolearn.framework.sardine.response.UploadResult;
    import lombok.extern.slf4j.Slf4j;
    import net.coobird.thumbnailator.Thumbnails;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;

    /**

    • @author koolearn

    • @date 2021-01-29 7:09 下午
      */
      @Slf4j
      public class GridPictureUtil {
      public static String graphicsGeneration(List<List<List<String>>> allValue, List<String> titles, List<String[]> headers, String imgPath, 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 * 90 + 20;
      //行高
      int rowheight = 90;
      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)) {
                               String cellContent = arr.get(l);
                               int strMaxLength = 70;
                               if (cellContent.length() > strMaxLength) {
                                   graphics.drawString(cellContent.substring(0, strMaxLength), rightLine,
                                           startHeight + rowheight * (n + startH) - 30);
                                   graphics.drawString(cellContent.substring(strMaxLength), rightLine,
                                           startHeight + rowheight * (n + startH) - 10);
                               } else {
                                   graphics.drawString(cellContent, rightLine,
                                           startHeight + rowheight * (n + startH) - 10);
                               }
      
                           } else {
                               graphics.drawString("0.000万", rightLine,
                                       startHeight + rowheight * (n + startH) - 10);
                           }
                       }
                   }
               }
               startH += 2 + typeV.size();
           }
           i++;
       }
       Resource resource = new ClassPathResource(imgPath);
       Thumbnails.of(image)
               .scale(1f)
               .outputQuality(0.1f).toFile(resource.getFile());
       String trunkEndpoint = "http://sardine.trunk.koolearn.com/api/v1/";
       String token = "eyJhbGciOiJIUzUxMiJ9.eyJidWNrZXQiOiJjcm0tcWQtZmlsZS10ZXN0Iiwic3ViIjoic2FyZGluZS1hY2Nlc3MtdG9rZW4iLCJhcHBOYW1lIjoia29vLWNybS1xZCIsImlhdCI6MTYwNTE3MzA2NH0.OFE0BKi8AMtB1-7oa5sVJNqefn8XPDVAcUvWfBK-A5KMYkyGmOzsQtt3oXI5IkIskdcujnzCIw02wxFVEPW2qQ";
       Sardine client = new SardineClientBuilder().build(trunkEndpoint, token);
       String uri = "/crm-qd-file-test/" + System.currentTimeMillis() + resource.getFilename();
       try {
           UploadResult result = client.upload(new FileInputStream(resource.getFile()), uri);
           log.info("上传结果:{}", JSON.toJSONString(result));
           return result.getUrls().get(0).getUrl();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       //ImageIO.write(image, "jpg", resource.getFile());
       return "http://chuantu.xyz/t6/741/1611997417x1700340463.jpg";
      
       //return resource.getURI().toString();
      

      }

      /**

      • 获取竖线和文字的水平位置
      • @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 + 4 * colwidth;
        } else if (k == 2) {
        rightLine = startWidth + 5 * colwidth;
        } else if (k == 3) {
        rightLine = startWidth + (6 * colwidth) - (colwidth / 2);
        } else if (k == 4) {
        rightLine = startWidth + 6 * colwidth;
        }
        // else if (k == 3) {
        // rightLine = startWidth + 3
        colwidth;
        // }
        // else if (k == 4) {
        // rightLine = startWidth + 4colwidth;
        // }else if (k == 5) {
        // rightLine = startWidth + 5
        colwidth;
        // }else if (k == 6) {
        // rightLine = startWidth + 6colwidth;
        // }else if (k == 7) {
        // rightLine = startWidth + 7
        colwidth;
        // }else if (k == 8) {
        // rightLine = imageWidth -10;
        // }
        return rightLine;
        }

      public static String init95Line(List<Line95> line95List) {
      int top=20;
      List<List<List<String>>> allValue = new ArrayList<>();
      List<List<String>> trList = new ArrayList<>(top);
      for (int i = 0; i < top; i++) {
      Line95 line95 = line95List.get(i);
      List<String> tdList = new ArrayList<>(4);
      tdList.add(line95.getName());
      tdList.add("" + line95.getLine95());
      tdList.add("" + line95.getTotal());
      tdList.add(line95.getProject());
      trList.add(tdList);
      }

       allValue.add(trList);
       List<String[]> headTitles = new ArrayList<>();
       String[] h1 = new String[]{"接口名称", "95Line", "调用量", "所属系统"};
       headTitles.add(h1);
       List<String> titles = new ArrayList<>();
       titles.add("接口性能TOP"+top+"(按95L倒序)");
       try {
           String url = graphicsGeneration(allValue, titles, headTitles, "/static/img/lin95.jpg", h1.length-1);
           return url;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return "";
      

      }

      public static String initTopCall(List<Line95> line95List) {
      int top=20;
      List<List<List<String>>> allValue = new ArrayList<>();
      List<List<String>> trList = new ArrayList<>(top);
      for (int i = 0; i < top; i++) {
      Line95 line95 = line95List.get(i);
      List<String> tdList = new ArrayList<>(5);
      tdList.add(line95.getName());
      tdList.add("" + line95.getTotal());
      tdList.add("" + line95.getFailNum());
      tdList.add("" + line95.getLine95());
      tdList.add(line95.getProject());
      trList.add(tdList);
      }

       allValue.add(trList);
       List<String[]> headTitles = new ArrayList<>();
       String[] h1 = new String[]{"接口名称", "调用量", "失败数量", "95Line", "所属系统"};
       headTitles.add(h1);
       List<String> titles = new ArrayList<>();
       titles.add("接口调用量TOP"+top+"(按接口调用量倒序)");
       try {
           return graphicsGeneration(allValue, titles, headTitles, "/static/img/topcall.jpg", h1.length-1);
       } catch (Exception e) {
           e.printStackTrace();
       }
       return "";
      

      }

      /**

      • 初始化售前核心接口图片
      • @param line95List
      • @return
        */
        public static String initCorelin95Item(List<Line95> line95List) {
        // List<List<List<String>>> allValue = new ArrayList<>();
        // List<List<String>> trList = new ArrayList<>(DingUtil.CORE_INTERFACE_LIST_ITEM.size());
        // for (Line95 line95 : line95List) {
        // for (CoreInterface coreInterface : DingUtil.CORE_INTERFACE_LIST_ITEM) {
        // if (line95.getName().endsWith(coreInterface.getName())) {
        // List<String> tdList = new ArrayList<>(3);
        // tdList.add("(" + coreInterface.getPage() + ")" + line95.getName());
        // tdList.add("" + line95.getLine95());
        // tdList.add(line95.getProject());
        // trList.add(tdList);
        // }
        // }
        // }
        // allValue.add(trList);
        // List<String[]> headTitles = new ArrayList<>();
        // String[] h1 = new String[]{"接口名称", "95Line", "所属系统"};
        // headTitles.add(h1);
        // List<String> titles = new ArrayList<>();
        // titles.add("核心接口性能TOP10(倒序)- 售前部分");
        // try {
        // return graphicsGeneration(allValue, titles, headTitles, "/static/img/corelin95Item.jpg", 2);
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
        return "";
        }

      /**

      • 初始化学习核心接口图片
      • @param line95List
      • @return
        */
        public static String initCoreInterface(List<Line95> line95List) {
        List<List<List<String>>> allValue = new ArrayList<>();
        List<List<String>> trList = new ArrayList<>(DingUtil.CORE_INTERFACE_LIST.size());
        for (Line95 line95 : line95List) {
        for (CoreInterface coreInterface : DingUtil.CORE_INTERFACE_LIST) {
        if (line95.getName().endsWith(coreInterface.getName())) {
        List<String> tdList = new ArrayList<>(4);
        tdList.add("(" + coreInterface.getPage() + ")" + line95.getName());
        tdList.add("" + line95.getLine95());
        tdList.add(""+line95.getFailNum());
        tdList.add(line95.getProject());
        trList.add(tdList);
        }
        }
        }
        allValue.add(trList);
        List<String[]> headTitles = new ArrayList<>();
        String[] h1 = new String[]{"接口名称", "95Line","失败数量", "所属系统"};
        headTitles.add(h1);
        List<String> titles = new ArrayList<>();
        titles.add("核心接口性能(倒序)");
        try {
        return graphicsGeneration(allValue, titles, headTitles, "/static/img/corelin95Study.jpg", h1.length-1);
        } catch (Exception e) {
        e.printStackTrace();
        }
        return "";
        }

      public static String failNum(List<Line95> line95List) {
      int top=20;
      List<List<List<String>>> allValue = new ArrayList<>();
      List<List<String>> trList = new ArrayList<>(top);
      for (int i = 0; i < top; i++) {
      Line95 line95 = line95List.get(i);
      List<String> tdList = new ArrayList<>(3);
      tdList.add(line95.getName());
      tdList.add("" + line95.getFailNum());
      tdList.add(line95.getProject());
      trList.add(tdList);
      }

       allValue.add(trList);
       List<String[]> headTitles = new ArrayList<>();
       String[] h1 = new String[]{"接口名称", "报错数量", "所属系统"};
       headTitles.add(h1);
       List<String> titles = new ArrayList<>();
       titles.add("报错数量TOP"+top+"(倒排序)");
       try {
           String url = graphicsGeneration(allValue, titles, headTitles, "/static/img/failnum.jpg", h1.length-1);
           return url;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return "";
      

      }

    }

    相关文章

      网友评论

          本文标题:Java生成表格图片

          本文链接:https://www.haomeiwen.com/subject/rzlytltx.html