欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java生成表格圖片的實例代碼

 更新時間:2020年09月08日 11:33:50   作者:崔笑顏  
這篇文章主要介紹了Java生成表格圖片的實例代碼,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下

主要代碼:

/**
 * 生成圖片
			 * @param cellsValue 以二維數(shù)組形式存放 表格里面的值
			 * @param path 文件保存路徑
			 */
			public void myGraphicsGeneration(String cellsValue[][], String path) {
				// 字體大小
				int fontTitileSize = 15;
				// 橫線的行數(shù)
				int totalrow = cellsValue.length+1;
				// 豎線的行數(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));
				
				//畫橫線
				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);
				}
				//畫豎線
				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);
				}
				//設置字體
				Font font = new Font("微軟雅黑",Font.BOLD,fontTitileSize);
				graphics.setFont(font);
				//寫標題
				String title = "【指標完成進度】";
				graphics.drawString(title, startWidth, startHeight+rowheight-10);
				//寫入內(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 緩沖文件類
			 * @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();
					}
			}

測試代碼:

public static void main(String[] args) {
				DrawTableImg cg = new DrawTableImg();
				try {
					String tableData1[][] = {{"8月31日","累計用戶數(shù)","目標值","完成進度","時間進度", "進度差異"}, {"掌廳客戶端(戶)","469281","1500000","31.2%","33.6%", "-2.4%"}};
					String[][] tableData2 = {{"8月31日(戶)","新增用戶數(shù)","日訪問量","累計用戶數(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生成表格圖片的實例代碼的詳細內(nèi)容,更多關于Java生成表格圖片的資料請關注腳本之家其它相關文章!

相關文章

  • 利用Java編寫一個出敬業(yè)福的小程序

    利用Java編寫一個出敬業(yè)福的小程序

    新年將至,又開始掃福活動,每年的敬業(yè)福成了大家難過的坎。所以本文將介紹一個通過Java編寫的一款福字生成器,感興趣的小伙伴可以試一試
    2022-01-01
  • CentOS?7.9服務器Java部署環(huán)境配置的過程詳解

    CentOS?7.9服務器Java部署環(huán)境配置的過程詳解

    這篇文章主要介紹了CentOS?7.9服務器Java部署環(huán)境配置,主要包括ftp服務器搭建過程、jdk安裝方法以及mysql安裝過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • mybatis-plus更新字段為null的處理方式

    mybatis-plus更新字段為null的處理方式

    這篇文章主要介紹了mybatis-plus更新字段為null的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java并發(fā)中的ABA問題學習與解決方案

    Java并發(fā)中的ABA問題學習與解決方案

    這篇文章主要介紹了Java并發(fā)中的ABA問題學習與解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 基于Spring Security的Oauth2授權實現(xiàn)方法

    基于Spring Security的Oauth2授權實現(xiàn)方法

    這篇文章主要介紹了基于Spring Security的Oauth2授權實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Spring中集成Groovy的四種方式(小結)

    Spring中集成Groovy的四種方式(小結)

    這篇文章主要介紹了Spring中集成Groovy的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 基于request.getAttribute與request.getParameter的區(qū)別詳解

    基于request.getAttribute與request.getParameter的區(qū)別詳解

    本篇文章小編為大家介紹,基于request.getAttribute與request.getParameter的區(qū)別詳解。需要的朋友參考下
    2013-04-04
  • Spring?Cloud中Sentinel的兩種限流模式介紹

    Spring?Cloud中Sentinel的兩種限流模式介紹

    如何使用Sentinel做流量控制呢?這篇文章就來為大家詳細介紹了Spring?Cloud中Sentinel的兩種限流模式,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05
  • Java HashMap原理及實例解析

    Java HashMap原理及實例解析

    這篇文章主要介紹了Java HashMap原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Mybatis通用Mapper(tk.mybatis)的使用

    Mybatis通用Mapper(tk.mybatis)的使用

    本文主要介紹了Mybatis通用Mapper(tk.mybatis)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07

最新評論