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) throws Exception {

        // 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 = new ImageUtil();
        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 class ImageUtil {
    // 豎線的行數
    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) throws SQLException{
        
        // 圖片高度
        int imageHeight = totalrow * rowheight + 50;
        // 單元格寬度
        int colwidth = (int) ((imageWidth - 20) / totalcol);

        // 1.獲取數據
        BufferedImage bufferedImage = new BufferedImage(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);
        return bufferedImage;
    }
}

3. 之前看到一篇博客說,ImageIO不適用於Ajax請求,我沒有嘗試,我是前端直接訪問的這個路徑進行下載的。看下效果

 

 由於隱私問題,對數據打馬了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM