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);
     }

}           

                                                                                                                                        

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纯洁的一笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值