當前位置:
首頁 >
java生成表格图片(请假条为例)
發(fā)布時間:2024/3/12
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
java生成表格图片(请假条为例)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java生成表格圖片(請假條)
package table;import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;public class TestTable {BufferedImage image;void createImage(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 void graphicsGeneration() {//實際數據行數+標題+備注int totalrow = 4;int totalcol = 6;int imageWidth = 2024;int imageHeight = totalrow * 40 + 20;int rowheight = 40;int startHeight = 10;int startWidth = 10;int colwidth = (int) ((imageWidth - 20) / totalcol);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, imageWidth - 5, startHeight + (j + 1) * rowheight);}//末行graphics.setColor(Color.black);graphics.drawLine(startWidth, imageHeight - 90, imageWidth - 5, imageHeight - 90);//畫豎線for (int k = 0; k < totalcol; k++) {graphics.setColor(Color.black);graphics.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth, imageHeight - 50);}//末列graphics.setColor(Color.black);graphics.drawLine(imageWidth - 5, startHeight + rowheight, imageWidth - 5, imageHeight - 50);//設置字體Font font = new Font("華文楷體", Font.BOLD, 20);graphics.setFont(font);//寫標題String title = "警員請假單";graphics.drawString(title, imageWidth / 3 + startWidth, startHeight + rowheight - 10);font = new Font("華文楷體", Font.BOLD, 18);graphics.setFont(font);//寫入表頭String[] headCells = {"賬號", "姓名", "請假內容", "開始時間", "結束時間","審核意見"};for (int m = 0; m < headCells.length; m++) {graphics.drawString(headCells[m].toString(), startWidth + colwidth * m + 5, startHeight + rowheight * 2 - 10);}//設置字體font = new Font("華文楷體", Font.PLAIN, 16);graphics.setFont(font);String[][] cellsValue = {{"20182012", "陳雅麗", "2012-11-12", "2012-11-13", "頭疼,前往中心醫(yī)院檢查,請假一天","同意!注意身體"}};//寫入內容for (int n = 0; n < cellsValue.length; n++) {String[] arr = cellsValue[n];for (int l = 0; l < arr.length; l++) {graphics.drawString(cellsValue[n][l].toString(), startWidth + colwidth * l + 5, startHeight + rowheight * (n + 3) - 10);}}font = new Font("華文楷體", Font.BOLD, 18);graphics.setFont(font);graphics.setColor(Color.RED);//寫備注String remark = "此圖片可作為請假憑據,請保存至本地!";graphics.drawString(remark, startWidth, imageHeight - 30);createImage("d:\\test.jpg");}}效果圖:
ok了,至于如何應用到web項目中,應該大家都會了,如果有想了解的,可以評論,我會立刻出一個web請假系統(tǒng)生成請假單的例子。最后,別忘了點贊關注6666.
總結
以上是生活随笔為你收集整理的java生成表格图片(请假条为例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python predictabel_统
- 下一篇: 现代诗如蚯蚓