java 將保單數據 生成圖片


主要代碼:
----------------------------------------------------------------

/**
             * 生成圖片
             * @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 = 1024;
                // 行高
                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);
                //寫標題
                String title = "【指標完成進度】";
                graphics.drawString(title, startWidth, startHeight+rowheight-10);
                //寫入內容
                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();
                    }
            }
 
         
         
        

 






測試代碼
-------------------------------------------------------

public static void main(String[] args) {
                DrawTableImg cg = new DrawTableImg();
                try {
                    String tableData1[][] = {{"8月31日","累計用戶數","目標值","完成進度","時間進度", "進度差異"}, {"掌廳客戶端(戶)","469281","1500000","31.2%","33.6%", "-2.4%"}};
                    String[][] tableData2 = {{"8月31日(戶)","新增用戶數","日訪問量","累計用戶數","環比上月"},
                    {"合肥和巢湖","469281","1500000","31.2%","33.6%"},
                    {"蕪湖","469281","1500000","31.2%","33.6%"},
                    {"蚌埠","469281","1500000","31.2%","33.6%"},
                    {"淮南","469281","1500000","31.2%","33.6%"},
                    {"馬鞍山","469281","1500000","31.2%","33.6%"},
                    {"淮北","469281","1500000","31.2%","33.6%"}};
                    cg.myGraphicsGeneration(tableData2, "c:\\myPic.jpg");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

 




免責聲明!

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



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