如果是图表,比如柱状图、折线图、饼图之类的,用jfreechart就可以做到了,但是有些图是纯文字的,这就需要做到将文字放到一张图片中。
https://www.dandelioncloud.cn/article/details/1533801141372743682
https://blog.csdn.net/u014641168/article/details/125338018
先上图:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
package test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.dom4j.DocumentException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Demo{
// 生成Word
public static void createXWPFDocument(BufferedImage image)
throws IOException, DocumentException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "gif", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, "图片1", 200, 140);
OutputStream os = new FileOutputStream("D:\picture\" + UUID.randomUUID().toString().substring(28) + ".docx");
// 把doc输出到输出流
doc.write(os);
}
// 生成图片
private static void createImage(String fileLocation, BufferedImage image) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
int imageWidth = 330;// 图片的宽度
int imageHeight = 200;// 图片的高度
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(Color.BLACK);
int high = 50;
int wigth = 20;
graphics.setFont(new Font("宋体", Font.BOLD, 20));
graphics.drawString("正在连接主机台数", wigth, high);
high += 10;
graphics.drawLine(20, high, 310, high);
high += 30;
graphics.setColor(Color.GRAY);
graphics.setFont(new Font("宋体", Font.BOLD, 15));
graphics.drawString("数据刷新于:2022-10-12 20:52:41", wigth, high);
high += 30;
graphics.drawString("统计", wigth, high);
wigth += 50;
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("宋体", Font.BOLD, 30));
graphics.drawString("7", wigth, high);
wigth += 20;
graphics.setColor(Color.GRAY);
graphics.setFont(new Font("宋体", Font.BOLD, 15));
graphics.drawString("人", wigth, high);
wigth = 20;
high += 20;
graphics.drawString("同比:", wigth, high);
wigth += 50;
graphics.setColor(Color.RED);
graphics.drawString("99.99%", wigth, high);
graphics.setColor(Color.GRAY);
wigth = 20;
high += 20;
graphics.drawString("环比:", wigth, high);
wigth += 50;
graphics.setColor(Color.GREEN);
graphics.drawString("99.99%", wigth, high);
// createImage("E:\picture\"+UUID.randomUUID().toString().substring(25)+".jpg",
// image);
createXWPFDocument(image);
}
}
原文地址:https://blog.csdn.net/qq_38667622/article/details/127294521
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_29052.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。