1、生成图片

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class ImageGenera {
/**
  * 生成图片
  * @param cellsValue 以二维数组形式存放 表格里面的值
  * @param path 文件保存路径
  */
public void myGraphicsGeneration(String cellsValue[][],String path){
    // 字体大小
    int fontTitileSize =15;
    // 横线的行数
    int totalrow = cellsValue.length+1;
    // 竖线的行数
    int totalcol = 0;
    if (cellsValue[0] != null){
        totalcol = cellsValue[0].length;
    }
    // 图片宽度
    int imageWidth =2048;
    // 行高
    int rowheight= 40;
    // 图片高度
    int imageHeight = totalrow*rowheight+50;
    // 起始高度
    int startHeight = 10;
    // 起始宽度
    int startWidth = 10;
    // 单元格宽度
    int colwidth = (int)((imageWidth-20)/totalcol);
    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(220,240,240));

    //画横线
    for(int j=0;j<totalrow;j++){
        graphics.setColor(Color.black);
        graphics.drawLine(startWidth,startHeight+(j+1)*rowheight,startWidth+colwidth*totalcol,startHeight+(j+1)*rowheight);
    }
    //画竖线
    for(int k=0;k<totalcol+1;k++){
        graphics.setColor(Color.black);
        graphics.drawLine(startWidth+k*colwidth,startHeight+rowheight,startWidth+k*colwidth,startHeight+rowheight*totalrow);
    }
    //设置字体
    Font font  = new Font("微软雅黑",Font.BOLD,fontTitileSize);
    graphics.setFont(font);

    //写入内容
    for(int n=0;n<cellsValue.length;n++){
        for(int l=0;l<cellsValue[n].length;l++){
            if (n == 0){
                font = new Font("微软雅黑",Font.BOLD,fontTitileSize);
                graphics.setFont(font);
            }else if (n > 0 && l > 0){
                font = new Font("微软雅黑",Font.PLAIN,fontTitileSize);
                graphics.setFont(font);
                graphics.setColor(Color.RED);
            }else{
                font = new Font("微软雅黑",Font.PLAIN,fontTitileSize);
                graphics.setFont(font);
                graphics.setColor(Color.BLACK);
            }
            graphics.drawString(cellsValue[n][l].toString(),startWidth+colwidth*l+5,startHeight+rowheight*(n+2)-10);
        }
    }
    // 保存图片
    createImage(image,path);
}





/**
  * 将图片保存到指定位置
  * @param image 缓冲文件类
* @param fileLocation 文件位置
*/
public void createImage(BufferedImage image, String fileLocation){
    try{
        FileOutputStream fos = new FileOutputStream(fileLocation);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(image);
        bos.close();
    } catch (Exception e){
        e.printStackTrace();
    }
}
}

2、数据处理,生成上面需要的二维数组

import java.util.*;

public class DataProce {
/**
 * @Description: 数据处理,把list集合处理成2维数组,注意set集合无序
 * @Param: ArrayList集合,jdbc的工具类查询出来
 * @return: 二维数组
 * @Author: 
 * @Date: 2020/7/9
 */
public String[][] dateCreate(List<Map<String, Object>> date) {

    //在list集合中首位添加一个元素(所有的key)
    List<Map<String, Object>> maps = new ArrayList<>();

    Map<String, Object> stringObjectMap = date.get(0);
    Set<String> keys = stringObjectMap.keySet();
    Map<String,Object> map1 = new HashMap<String,Object>();
    map1.put("0","库");
    map1.put("1","表");
    map1.put("2","大集群数量");
    map1.put("3","hive数量");
    map1.put("4","前置数量");
    map1.put("5","oracle数量");
    map1.put("6","前置差异");
    map1.put("7","大集群差异");
    map1.put("8","hive差异");
    map1.put("9","查询时间");

    //这样排序才对,要改成汉字,这里注意set是无序的
//        Integer a = 0;
//        for (String key : keys) {
//            String s = a.toString();
//            map1.put(s,key);
//            a++;
//        }
    maps.add(map1);


//        JSONArray objects = JSONArray.parseArray(JSON.toJSONString(maps));
//        String s1 = objects.toString();
//        System.out.println(s1);


    //将date集合添加到新集合
    maps.addAll(date);


    //二维数组的第二个数组长度,行数 查询的条数+1(列头)4
    int size = maps.size();
//        System.out.println(size);
    String[][] z = new String[size][];
    for (int i = 0; i < z.length; i++) {
        Map m = (Map) maps.get(i);
        Set set = m.keySet();
        z[i] = new String[m.size()];
        Iterator it = set.iterator();
        for (int j = 0; it.hasNext(); j++) {
            String s = (String) it.next();
            if (m.get(s) != null) {
                z[i][j] = m.get(s).toString();
            }
        }
    }
    return z;
}
}

借鉴:
https://blog.csdn.net/weixin_34137799/article/details/91749037
和https://blog.csdn.net/jishoujiang/article/details/78566632

https://my.oschina.net/beanGo/blog/315920

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐