用于将表格数据转换为PNG图像文件的Java API或工具 [英] Java API or Tool to convert tabular data into PNG image file

查看:211
本文介绍了用于将表格数据转换为PNG图像文件的Java API或工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java API或应用程序是否有办法将CSV表格数据转换为文本并将其转换为带有行和网格和标题的PNG文件?

Is there a way a Java API or application to convert CSV tabular data as text and convert that into a PNG file with lines and a grid and header?

对现在,我在考虑将HTML数据转换为图像的xhtmlrenderer。

Right now, I am thinking xhtmlrenderer which converts HTML data into an image.

更新:安德鲁给出了一个很好的回复,我将其设为答案。另外,我使用xhtmlrenderer / fly saucer来获得相同的结果,将html文档转换为图像。它花了与他的例子相同的努力。

Updated: Andrew gave a good response, I set that as the answer. Also, I used xhtmlrenderer/flying saucer with the same result to convert a html document to an image. It took the same amount of effort as his example.

http://code.google.com/p/flying-saucer/

现在,在github上:

Now, on github:

https://github.com/berlinbrown/XHTMLRendererForHtmlDataToImage

推荐答案

您能阅读表格数据并将其放入 JTable 吗?

Can you read the tabular data and put it into a JTable?

如果是这样,请调用 table.paintComponent(Graphics)是受保护的方法 - 请参阅 table.paint(Graphics),其中 Graphics 对象是从一个首选大小的图像中获取的表。

If so, call table.paintComponent(Graphics) is a protected method - see instead table.paint(Graphics), where the Graphics object is obtained from an image that is the preferred size of the table.


您能提供更完整的示例吗?

Could you provide a more complete example?

这个例子使用Nimbus PLAF作为'备用行着色',我认为每个表都应该有。

This example uses the Nimbus PLAF for the 'alternate row shading' which I think every table should have.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.table.JTableHeader;

import javax.imageio.ImageIO;
import java.io.File;

class TableImage {

    public static void main(String[] args) throws Exception {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception useDefault) {
        }

        Object[][] data = {
            {"Hari", new Integer(23), new Double(78.23), new Boolean(true)},
            {"James", new Integer(23), new Double(47.64), new Boolean(false)},
            {"Sally", new Integer(22), new Double(84.81), new Boolean(true)}
        };

        String[] columns = {"Name", "Age", "GPA", "Pass"};

        JTable table = new JTable(data, columns);
        JScrollPane scroll = new JScrollPane(table);

        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll,BorderLayout.CENTER);

        // JTable must have been added to a TLC in order to render
        // correctly - go figure.
        JFrame f = new JFrame("Never shown");
        f.setContentPane(scroll);
        f.pack();

        JTableHeader h = table.getTableHeader();
        Dimension dH = h.getSize();
        Dimension dT = table.getSize();
        int x = (int)dH.getWidth();
        int y = (int)dH.getHeight() + (int)dT.getHeight();

        scroll.setDoubleBuffered(false);

        BufferedImage bi = new BufferedImage(
            (int)x,
            (int)y,
            BufferedImage.TYPE_INT_RGB
            );

        Graphics g = bi.createGraphics();
        h.paint(g);
        g.translate(0,h.getHeight());
        table.paint(g);
        g.dispose();

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
        ImageIO.write(bi,"png",new File("table.png"));

        // our TLC forces us to explicitly exit the VM
        System.exit(0);
    }
}

这篇关于用于将表格数据转换为PNG图像文件的Java API或工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆