Java生成表格图片的实例代码
主要代码:
/**
*生成图片
*@paramcellsValue以二维数组形式存放表格里面的值
*@parampath文件保存路径
*/
publicvoidmyGraphicsGeneration(StringcellsValue[][],Stringpath){
//字体大小
intfontTitileSize=15;
//横线的行数
inttotalrow=cellsValue.length+1;
//竖线的行数
inttotalcol=0;
if(cellsValue[0]!=null){
totalcol=cellsValue[0].length;
}
//图片宽度
intimageWidth=1024;
//行高
introwheight=40;
//图片高度
intimageHeight=totalrow*rowheight+50;
//起始高度
intstartHeight=10;
//起始宽度
intstartWidth=10;
//单元格宽度
intcolwidth=(int)((imageWidth-20)/totalcol);
BufferedImageimage=newBufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
Graphicsgraphics=image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0,imageWidth,imageHeight);
graphics.setColor(newColor(220,240,240));
//画横线
for(intj=0;j0&&l>0){
font=newFont("微软雅黑",Font.PLAIN,fontTitileSize);
graphics.setFont(font);
graphics.setColor(Color.RED);
}else{
font=newFont("微软雅黑",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);
}
/**
*将图片保存到指定位置
*@paramimage缓冲文件类
*@paramfileLocation文件位置
*/
publicvoidcreateImage(BufferedImageimage,StringfileLocation){
try{
FileOutputStreamfos=newFileOutputStream(fileLocation);
BufferedOutputStreambos=newBufferedOutputStream(fos);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}catch(Exceptione){
e.printStackTrace();
}
}
测试代码:
publicstaticvoidmain(String[]args){
DrawTableImgcg=newDrawTableImg();
try{
StringtableData1[][]={{"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(Exceptione){
e.printStackTrace();
}
}
效果图
以上就是Java生成表格图片的实例代码的详细内容,更多关于Java生成表格图片的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。