java绘制数据表格并导出为图片格式

摘要:
好久没写了,其实不是不写,都记在笔记里了,感觉在这里写有点慢而已。。话不多说直接上代码!!!

好久没写了,其实不是不写,都记在笔记里了,感觉在这里写有点慢而已。。

话不多说直接上代码!!!

本文参考连接:https://blog.csdn.net/weixin_34137799/article/details/91749037,感谢这位老哥,如有侵权,请联系我

1.前端发送请求,后端进行处理下载

    /*** @Description : 导出图片<br>
     * @param:</b> <br>
     *                 <br>
     * @return:</b> <br>
     *              2020-04-23
     */
    public void actionExportReport(HttpServletRequest request, HttpServletResponse response) throwsException {

        //1. 获取请求参数
        String SID = request.getParameter("SID");
        if (SID == null) {
            SID = "";
        }
        String reportSn = request.getParameter("reportSn");
        if (reportSn == null) {
            reportSn = "";
        }
        //2. 根据请求参数,取后台数据
        Res_inst_checkLineObject lineObject =checkMgr.getObjectById(reportSn);
        Res_inst_scheduresultObject object =resultMgr.getObjectById(lineObject.getMissionsn());
        lineObject.setSchedulNum(object.getSchedulenumber());
        ArrayList<Res_inst_checkDetailsObject> arrayList = null;
        if (lineObject.getVersion().equals("1")) {
            arrayList =cDetailMgr.queryListBySn(reportSn);
        } else{
            arrayList =cDetailMgr.queryListByParentSn(reportSn);
        }
        /**  3. 对取出来的数据进行处理
         *     ArrayList<ArrayList<String>> 我用这个代替的二位数组,我设计的表是一共8列,所以在每个子List中都放入了8条数据
         *     getDataList()方法就是处理数据的,代码就不展示了
         */ArrayList<ArrayList<String>> list =getDataList(lineObject, arrayList);
        /**  4. 封装了画图类  ImageUtil,用于画图初始化,设置图标题、表格行列数
         */ImageUtil util = newImageUtil();
        BufferedImage bufferedImage =util.drawImage(lineObject.getName(), list.size() );

        Graphics2D graphics2D =(Graphics2D) bufferedImage.getGraphics();
        /**  5. 继续向初始化后的graphics2D,存储刚处理的数据内容
         */Font font = null;
        int colwidth = (int) ((util.imageWidth - 20) /util.totalcol);
        for(int n=0;n< list.size();n++){
            for(int l=0;l<list.get(n).size();l++){
                font = new Font("微软雅黑",Font.PLAIN, 14);
                graphics2D.setFont(font);
                graphics2D.setColor(Color.BLACK);
                if(n == 3){
                    graphics2D.setFont( new Font("微软雅黑",Font.BOLD, 16));
                    graphics2D.setColor(Color.RED);
                    graphics2D.setBackground(new Color(135,206,235));
                }
                graphics2D.drawString(list.get(n).get(l), util.startWidth+ colwidth*l+5, util.startHeight+util.rowheight*(n+2)-10);
            }
       }
        /**  6. 此处定义,用户可自定义保存位置
         */response.setContentType("image/jpeg;charset=gbk");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String((new Date().getTime() + ".jpg").getBytes(), "iso-8859-1"));
        //2.将图片写到流里,如果指定保存位置,更改response.getOutputStream() 为 自己 的位置就可以
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
    }

2.ImageUtil

/*** @Date : 2020年4月23日
 * @author:HuGY
 * @Description: 
 */
public classImageUtil {
    //竖线的行数
    public static final int totalcol = 8;
    //图片宽度
    public static final int imageWidth = 1850;
    //行高
    public static final int rowheight = 40;
    //起始高度
    public static final int startHeight = 10;
    //起始宽度
    public static final int startWidth = 10;
    /**     向表格中画内容
     */
    public BufferedImage drawImage(String title, int totalrow) throwsSQLException{
        
        //图片高度
        int imageHeight = totalrow * rowheight + 50;
        //单元格宽度
        int colwidth = (int) ((imageWidth - 20) /totalcol);

        //1.获取数据
        BufferedImage bufferedImage = newBufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D graphics2D =(Graphics2D) bufferedImage.getGraphics();
        
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0,0, imageWidth, imageHeight);
        graphics2D.setColor(new Color(220,240,240));
        for (int j = 0; j < totalrow; j++) {
            graphics2D.setColor(Color.black);
            graphics2D.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + colwidth *totalcol,
                    startHeight + (j + 1) *rowheight);
        }
        //画竖线
        for (int k = 0; k < totalcol + 1; k++) {
            graphics2D.setColor(Color.black);
            graphics2D.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k *colwidth,
                    startHeight + rowheight *totalrow);
        }
        //设置字体
        Font font = new Font("微软雅黑", Font.BOLD, 16);
        graphics2D.setFont(font);
        graphics2D.setColor(Color.RED);
        //写标题
        graphics2D.drawString(title, startWidth, startHeight + rowheight - 10);
        returnbufferedImage;
    }
}

3. 之前看到一篇博客说,ImageIO不适用于Ajax请求,我没有尝试,我是前端直接访问的这个路径进行下载的。看下效果
java绘制数据表格并导出为图片格式第1张

由于隐私问题,对数据打马了

免责声明:文章转载自《java绘制数据表格并导出为图片格式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇pandas dataframe.apply() 实现对某一行/列进行处理获得一个新行/新列awk getline命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Docker中使用Dockerfile定制化jar启动时:at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)

场景 CentOS7中使用Dockerfile部署后台jar包: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119818808 在上面使用Dockfile部署带后台jar包的镜像时提示: at sun.awt.FontConfiguration.getVersion(FontConf...

html基值 仿淘宝

$(function(){ var scale = 1 / devicePixelRatio; document.querySelector('meta[name="viewport"]').setAttribute('content','initial-scale=' + scale + ', maximu...

C# winform 打印事例

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.D...

vw和rem的根字号大小设置CSS代码

html { font-size: 16px; } @media screen and (min-375px) { html { /*iPhone6的375px尺寸作为16px基准,414px正好18px大小, 600 20px */font-size: calc(100% + 2 * (100vw - 375px) /...

第二章 JSP页面制作基础

第二章  JSP页面制作基础 [本章导读] JSP页面将Java代码嵌入到HTML脚本中,掌握HTML语言是学习JSP的基础。HTML是网页制作的一种规范,一种标准,它通过标记符来标记网页的各个部分。本章首先介绍用HTML制作网页的各种标记符的设置方法,接着介绍了CSS的基本概念和使用方法。此外,为了增强读者对网页制作的了解,介绍了Dreamweaver的...

h5红包雨

现在很多活动中都会有一些小游戏,比如抽奖轮盘,红包雨等等,今天心血来潮写了一个粗糙的红包雨 <div class="content"> <div class="bg_box"> <img src="./image/bg.jpg"/> </div> </div>...