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