1. 程式人生 > >java後臺程式碼建立表格並填充相應的內容

java後臺程式碼建立表格並填充相應的內容

       java後臺程式碼建立表格(生成圖片)                                          

  /**

     * 動態建立5行兩列表格,並填充相應的內容

     *

     * @param 

     * @return String

     */

public static void creatTabeAndIntoData(String[][] cellsValue){

   // 實際資料行數+標題+備註

     int totalrow = 5*(cellsValue.length);

     int totalcol = 3;

     int imageWidth = 1024;          //圖片背景寬度

     int imageHeight = totalrow * 40 + 20;//圖片背景高度

     int rowheight = 20;             //行高

     int startHeight = 10;

     int startWidth = 10;

     int colwidth = ((imageWidth - 360) / 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 n = 1;n<=cellsValue.length; n++){

         /*建立表格*/

         //表格

    for (int j = 0; j<= totalrow ; j++) {

    graphics.setColor(Color.black);

             graphics.drawLine(startWidth, startHeight + (j + 1)* rowheight+50,

                     imageWidth-3, startHeight + (j + 1)* rowheight+50);

         }

         // 末行

         graphics.setColor(Color.black);

         graphics.drawLine(startWidth, 180 +(n-1)*100, imageWidth,

                 180 +(n-1)*100);

         // 畫豎線

         for (int k = 0; k <= totalcol; k++) {

             graphics.setColor(Color.black);

             graphics.drawLine(startWidth + colwidth, startHeight

                     + rowheight+70+100*(n-1), startWidth + colwidth, startHeight + rowheight*5 +70+100*(n-1));

         }

         //寫入表頭內容

         Font font = new Font("華文楷體", Font.BOLD, 16);

         graphics.setFont(font);

         graphics.drawString("轉讓背書", startWidth + colwidth+200

                      , startHeight + rowheight +65+100*(n-1));

         // 首列

         graphics.setColor(Color.black);

         graphics.drawLine(10, startHeight + rowheight+50+100*(n-1),

                 10 , startHeight + rowheight*5 +70+100*(n-1));

         // 末列

         graphics.setColor(Color.black);

         graphics.drawLine(imageWidth -2, startHeight + rowheight+50+100*(n-1),

                 imageWidth -2, startHeight + rowheight*5 +70+100*(n-1));

         // 設定字型

         font = new Font("華文楷體", Font.PLAIN, 16);

         graphics.setFont(font);

         // 寫入行標題內容

         String[] headCells = { "背書人名稱", "被背書人名稱", "不得轉讓標記", "背書日期"};

         for (int m = 0; m < headCells.length; m++) {

             graphics.drawString(headCells[m].toString(), startWidth + colwidth/2

                      , startHeight + rowheight * m +105+100*(n-1));

         }

         //將資料動態寫入表格

       String[] arr = cellsValue[n-1];

       for (int m = 0; m < arr.length; m++) {

               graphics.drawString(arr[m].toString(), startWidth + colwidth/2+200

                        , startHeight + rowheight * m +105+100*(n-1));

           }

       }

         //顯示日期

         Date date = new Date();

         String time = BusUtils.dateToString2(date);

         graphics.drawString("顯示日期:"+time, 16, 15);

         graphics.drawString("票據號碼:1907646464646464646464464644545", 16, 62);

         // 設定字型

         Font  font = new Font("華文楷體", Font.BOLD, 20);

         graphics.setFont(font);

         // 寫標題

         String title = "電 子 銀 行 承 兌 匯 票";

         graphics.drawString(title, imageWidth / 3 + startWidth, startHeight

                 + rowheight - 3);

         font = new Font("華文楷體", Font.BOLD, 16);

         graphics.setFont(font);

         // 寫備註

         String remark = "備註:備註寫在這裡。";

         graphics.drawString(remark, startWidth, imageHeight - 30);

         try {

ImageIO.write(image, "jpg", new File("D:\\table.jpg"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}                       

public class PictureUtilTest2  {


    public static void main(String[] args) throws Exception{  
         graphicsGeneration();
     }  
   
 public static void graphicsGeneration() throws Exception { 
String[][] cellsValue = { { "上汽通用汽車銷售有限公司1", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司2", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司3", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司4", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司5", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司6", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司7", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" },
    { "上汽通用汽車銷售有限公司8", "上海通用汽車有限公司", "可再轉讓", "2015-11-26" }
    };
BusUtils.creatTabeAndIntoData(cellsValue);
     }

}