java生成表_java生成表格图表

这篇博客介绍了一个在Java中生成表格图表的需求,由于Java POI不支持服务器端将Excel导出为图片,作者选择了使用Java 2D API来实现。通过`graphics.fillRect()`、`graphics.drawLine()`和`graphics.drawString()`等方法,分别处理标题、表头、表中和表尾四个部分。代码示例展示了如何计算坐标和设置字体、颜色,以绘制出包含多个数据行的表格,并将其保存为图片。
摘要由CSDN通过智能技术生成

066d6653c53c7469e3750af1307c8477.png

项目有个需求是生成上图的表格图表,本来excel很容易生成上边的表格图,但是java poi不支持在服务器端把excel表格导出成图片,在没有找到合适的工具库下,用java 2d实现同样图表。

这个表格图分成标题、表头、表中、表尾4个部分,

背景填充用:graphics.fillRect()

画线条用:graphics.drawLine()

画文字用:graphics.drawString()

主要用上边三个java 2d方法实现,剩下的就是各种坐标位置的计算。

实现代码:

package com.penngo.test;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.util.Arrays;

import java.util.List;

public class ExcelChart {

private Font titleFont = new Font("宋体",Font.BOLD,20);

private Font headFont = new Font("宋体",Font.BOLD,16);

private Font rowFont = new Font("宋体",Font.PLAIN,14);

private Font bottomFont = new Font("宋体",Font.PLAIN,14);

private Color titleBackgroup = new Color(237, 125, 49); // 标题背景色

private Color headBackgroup = new Color(252, 228, 214); // 表头背景色

private Color lineColor = new Color(237, 125,49); // 线条颜色

private int titleMargin = 8;

private int headMargin = 8;

private int rowMargin = 5;

private int bottomMargin = 5;

private int width;

private int height;

private int widthMargin = 10; // x横轴边距

private int heightMargin = 10; // y轴边框

/**

* 将图片保存到指定位置

* @param image 缓冲文件类

* @param fileLocation 文件位置

*/

public void createImage(BufferedImage image, File fileLocation) {

try {

ImageIO.write(image, "png", fileLocation);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 字符串总宽度

* @param g

* @param str

* @return

*/

private int getStringWidth(Graphics g,String str) {

char[] strcha=str.toCharArray();

int strWidth = g.getFontMetrics().charsWidth(strcha, 0, str.length());

return strWidth;

}

//字符高度

private int getStringHeight(Graphics g) {

int height = g.getFontMetrics().getHeight();

return height;

}

private int calculateImageHeight(int rowCount){

BufferedImage image = new BufferedImage(100, 100,BufferedImage.TYPE_INT_RGB);

Graphics graphics = image.getGraphics();

graphics.setFont(titleFont);

int titleRowHeight = getStringHeight(graphics) + titleMargin *

你可以使用 iText 或者 Apache PDFBox 等 Java 的 PDF 库来生成包含图表表格的 PDF 文件。 iText 是一个广泛使用的 Java PDF 库,支持文本、表格、图像、图表等多种元素。你可以使用 iText 中的 PdfPTable 类来生成表格,使用 ChartFactory.createXXXChart() 等方法生成图表,然后将它们添加到 PDF 中。 以下是使用 iText 生成包含表格图表的 PDF 的示例代码: ```java Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 添加表格 PdfPTable table = new PdfPTable(3); table.addCell("Header 1"); table.addCell("Header 2"); table.addCell("Header 3"); table.addCell("1.1"); table.addCell("1.2"); table.addCell("1.3"); table.addCell("2.1"); table.addCell("2.2"); table.addCell("2.3"); document.add(table); // 添加图表 JFreeChart chart = ChartFactory.createBarChart( "Chart Title", "X Axis", "Y Axis", dataset, PlotOrientation.VERTICAL, true, true, false ); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(400, 300); Graphics2D graphics2D = template.createGraphics(400, 300, new DefaultFontMapper()); Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, 400, 300); chart.draw(graphics2D, rectangle2D); graphics2D.dispose(); contentByte.addTemplate(template, 0, 0); document.newPage(); document.close(); ``` Apache PDFBox 是另一个 Java PDF 库,它也支持多种元素的生成。你可以使用 PDFBox 中的 PDPageContentStream 类来添加表格图表。 以下是使用 PDFBox 生成包含表格图表的 PDF 的示例代码: ```java PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); // 添加表格 PDPageContentStream contentStream = new PDPageContentStream(document, page); PDTable table = new PDTable(); PDPageContentStreamTableDrawer drawer = new PDPageContentStreamTableDrawer(contentStream, table); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("Header 3"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("1.3"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.1"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.2"))); table.addCell(new PDCell().addParagraph(new PDParagraph("2.3"))); drawer.drawTable(100, 700, 400, 0); // 添加图表 JFreeChart chart = ChartFactory.createBarChart( "Chart Title", "X Axis", "Y Axis", dataset, PlotOrientation.VERTICAL, true, true, false ); BufferedImage image = chart.createBufferedImage(400, 300); PDImageXObject ximage = LosslessFactory.createFromImage(document, image); contentStream.drawImage(ximage, 100, 400, 400, 300); contentStream.close(); document.save("output.pdf"); document.close(); ``` 这些示例代码仅供参考,具体实现方式还需要根据你的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值