Java生成表格圖片的實(shí)例代碼
主要代碼:
/**
* 生成圖片
* @param cellsValue 以二維數(shù)組形式存放 表格里面的值
* @param path 文件保存路徑
*/
public void myGraphicsGeneration(String cellsValue[][], String path) {
// 字體大小
int fontTitileSize = 15;
// 橫線(xiàn)的行數(shù)
int totalrow = cellsValue.length+1;
// 豎線(xiàn)的行數(shù)
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));
//畫(huà)橫線(xiàn)
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);
}
//畫(huà)豎線(xiàn)
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);
}
//設(shè)置字體
Font font = new Font("微軟雅黑",Font.BOLD,fontTitileSize);
graphics.setFont(font);
//寫(xiě)標(biāo)題
String title = "【指標(biāo)完成進(jìn)度】";
graphics.drawString(title, startWidth, startHeight+rowheight-10);
//寫(xiě)入內(nèi)容
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 緩沖文件類(lèi)
* @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();
}
}
測(cè)試代碼:
public static void main(String[] args) {
DrawTableImg cg = new DrawTableImg();
try {
String tableData1[][] = {{"8月31日","累計(jì)用戶(hù)數(shù)","目標(biāo)值","完成進(jìn)度","時(shí)間進(jìn)度", "進(jìn)度差異"}, {"掌廳客戶(hù)端(戶(hù))","469281","1500000","31.2%","33.6%", "-2.4%"}};
String[][] tableData2 = {{"8月31日(戶(hù))","新增用戶(hù)數(shù)","日訪(fǎng)問(wèn)量","累計(jì)用戶(hù)數(shù)","環(huán)比上月"},
{"合肥和巢湖","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();
}
}
效果圖

以上就是Java生成表格圖片的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java生成表格圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java迭代器移除元素出現(xiàn)并發(fā)修改異常的原因及解決
這篇文章主要介紹了java迭代器移除元素出現(xiàn)并發(fā)修改異常的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
SpringBoot Actuator潛在的OOM問(wèn)題的解決
本文主要介紹了SpringBoot Actuator潛在的OOM問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
運(yùn)行SpringBoot項(xiàng)目請(qǐng)求響應(yīng)流程分析以及404和500報(bào)錯(cuò)的解決辦法
這篇文章主要介紹了運(yùn)行Spring Boot項(xiàng)目請(qǐng)求響應(yīng)流程分析以及404和500報(bào)錯(cuò)的解決辦法,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12
Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解
在這篇文章中,小編將帶大家了解一下Java數(shù)據(jù)結(jié)構(gòu)中鏈表的增刪查改(以下結(jié)果均在IDEA中編譯)希望在方便自己復(fù)習(xí)的同時(shí)也能幫助到大家2022-09-09
java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑
這篇文章主要為大家詳細(xì)介紹了java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Elasticsearch8.1中的Script使用實(shí)例深入解讀
這篇文章主要為大家介紹了Elasticsearch8.1中的Script使用實(shí)例深入解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Java中多線(xiàn)程Reactor模式的實(shí)現(xiàn)
多線(xiàn)程Reactor模式旨在分配多個(gè)reactor每一個(gè)reactor獨(dú)立擁有一個(gè)selector,本文就詳細(xì)的來(lái)介紹一下Java中多線(xiàn)程Reactor模式的實(shí)現(xiàn),需要的朋友可以參考下2021-12-12

