Java生成表格圖片

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());
    }
    }
    // 實(shí)際數(shù)據(jù)行數(shù)+標(biāo)題+備注
    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();
         }
     }
    
     // 設(shè)置字體
     Font font = new Font("華文楷體", Font.BOLD, 20);
     graphics.setFont(font);
    
     // 寫標(biāo)題
     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++;
     }
    
    
     // 寫入內(nè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("上傳結(jié)果:{}", 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", "調(diào)用量", "所屬系統(tǒng)"};
     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[]{"接口名稱", "調(diào)用量", "失敗數(shù)量", "95Line", "所屬系統(tǒng)"};
     headTitles.add(h1);
     List<String> titles = new ArrayList<>();
     titles.add("接口調(diào)用量TOP"+top+"(按接口調(diào)用量倒序)");
     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", "所屬系統(tǒng)"};
      // 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 "";
      }

    /**

    • 初始化學(xué)習(xí)核心接口圖片
    • @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","失敗數(shù)量", "所屬系統(tǒng)"};
      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[]{"接口名稱", "報錯數(shù)量", "所屬系統(tǒng)"};
     headTitles.add(h1);
     List<String> titles = new ArrayList<>();
     titles.add("報錯數(shù)量TOP"+top+"(倒排序)");
     try {
         String url = graphicsGeneration(allValue, titles, headTitles, "/static/img/failnum.jpg", h1.length-1);
         return url;
     } catch (Exception e) {
         e.printStackTrace();
     }
     return "";
    

    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摄咆,一起剝皮案震驚了整個濱河市嘶居,隨后出現(xiàn)的幾起案子英上,更是在濱河造成了極大的恐慌断国,老刑警劉巖谊路,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異厢呵,居然都是意外死亡纳鼎,警方通過查閱死者的電腦和手機(jī)建车,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門扩借,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人缤至,你說我怎么就攤上這事潮罪。” “怎么了凄杯?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵错洁,是天一觀的道長。 經(jīng)常有香客問我戒突,道長,這世上最難降的妖魔是什么描睦? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任膊存,我火速辦了婚禮,結(jié)果婚禮上忱叭,老公的妹妹穿的比我還像新娘隔崎。我一直安慰自己,他們只是感情好韵丑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布爵卒。 她就那樣靜靜地躺著,像睡著了一般撵彻。 火紅的嫁衣襯著肌膚如雪钓株。 梳的紋絲不亂的頭發(fā)上实牡,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機(jī)與錄音轴合,去河邊找鬼创坞。 笑死,一個胖子當(dāng)著我的面吹牛受葛,可吹牛的內(nèi)容都是我干的题涨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼总滩,長吁一口氣:“原來是場噩夢啊……” “哼纲堵!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起闰渔,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤婉支,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后澜建,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體向挖,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年炕舵,在試婚紗的時候發(fā)現(xiàn)自己被綠了何之。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡咽筋,死狀恐怖溶推,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奸攻,我是刑警寧澤蒜危,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站睹耐,受9級特大地震影響辐赞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜硝训,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一响委、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窖梁,春花似錦赘风、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至假哎,卻和暖如春瞬捕,著一層夾襖步出監(jiān)牢的瞬間鞍历,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工山析, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留堰燎,地道東北人。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓笋轨,卻偏偏與公主長得像秆剪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子爵政,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容