java drawing data table and exporting to picture format

I haven't written it for a long time. Actually, it's not that I don't write it. It's all in my notes. I feel it's a little slow here..

Don't say more directly on the code!!!

This article refers to the connection: https://blog.csdn.net/weixin_34137799/article/details/91749037 Thank you for your infringement. Please contact me

1. The front end sends the request and the back end processes the download

    /**
     * @Description : Export picture < br >
     * @param:</b> <br>
     *                 <br>
     * @return:</b> <br>
     *              2020-04-23
     */
    public void actionExportReport(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 1. Get request parameters
        String SID = request.getParameter("SID");
        if (SID == null) {
            SID = "";
        }
        String reportSn = request.getParameter("reportSn");
        if (reportSn == null) {
            reportSn = "";
        }
        // 2. According to the request parameters, take the background data
        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. Process the extracted data
         *     ArrayList<ArrayList<String>> The table I designed is a total of 8 columns, so I put 8 pieces of data in each sublist
         *     getDataList()The method is to process the data, and the code will not show it
         */
        ArrayList<ArrayList<String>> list = getDataList(lineObject, arrayList);
        /*
         *  4. It encapsulates the drawing class ImageUtil, which is used for drawing initialization, setting the drawing title and the number of table rows and columns
         */
        ImageUtil util = new ImageUtil();
        BufferedImage bufferedImage = util.drawImage(lineObject.getName(), list.size() );

        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
        /*
         *  5. Continue to store the data content just processed to the initialized 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("Microsoft YaHei ",Font.PLAIN, 14);
                graphics2D.setFont(font);
                graphics2D.setColor(Color.BLACK);
                if(n == 3){
                    graphics2D.setFont( new Font("Microsoft YaHei ",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. Defined here, user can customize the save location
         */
        response.setContentType("image/jpeg;charset=gbk");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String((new Date().getTime() + ".jpg").getBytes(), "iso-8859-1"));
        // 2.Write the picture to the stream. If you specify a save location, change response.getOutputStream() Just for your position
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
    }

2.ImageUtil

/** 
 * @Date : 2020 April 23, 2006
 * @author:HuGY
 * @Description: 
 */
public class ImageUtil {
    // Number of vertical lines
    public static final int totalcol = 8;
    // image width
    public static final int imageWidth = 1850;
    // Row height
    public static final int rowheight = 40;
    // Starting height
    public static final int startHeight = 10;
    // Start width
    public static final int startWidth = 10;
    /*
     *     Draw content to table
     */
    public BufferedImage drawImage(String title, int totalrow) throws SQLException{
        
        // Picture height
        int imageHeight = totalrow * rowheight + 50;
        // Cell width
        int colwidth = (int) ((imageWidth - 20) / totalcol);

        // 1.get data
        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);
        }
        // Draw a vertical line
        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);
        }
        // Set font
        Font font = new Font("Microsoft YaHei ", Font.BOLD, 16);
        graphics2D.setFont(font);
        graphics2D.setColor(Color.RED);
        // Write title
        graphics2D.drawString(title, startWidth, startHeight + rowheight - 10);
        return bufferedImage;
    }
}

3. I saw a blog saying that ImageIO is not suitable for Ajax requests. I didn't try. I downloaded it from the path directly accessed by the front end. See the effect

 

Because of privacy issues, we've made a big hit on the data

Tags: Java

Posted on Thu, 23 Apr 2020 07:16:28 -0700 by SQHell