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", "头疼,前往中心医院检查,请假一天","同意!注意身体"}};
        //写入内容
        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请假系统生成请假单的例子。最后,别忘了点赞关注6666.

猜你喜欢

转载自blog.csdn.net/weixin_43005654/article/details/123325013
今日推荐