简体   繁体   English

使用自定义TableCellRenderer导出JTable

[英]Export JTable with custom TableCellRenderer

I have a JTable that uses a TableCellRenderer to colour the background of some cells according to their values. 我有一个JTable ,它使用TableCellRenderer根据其值为某些单元格的背景着色。

I need to export this table in an excel file with the same cell formatting. 我需要将此表格导出为具有相同单元格格式的excel文件。 Here is an extract of what I currently see in the JTable . 这是我目前在JTable看到的内容的一部分。

在此处输入图片说明

Do I have to replicate all the logic behind the renderer in Apache POI to get the same result, or there is a quicker way? 是否必须在Apache POI中复制渲染器后面的所有逻辑才能获得相同的结果,还是有一种更快的方法?

You still need to create a workbook, a worksheet, rows and then cells. 您仍然需要创建工作簿,工作表,行,然后创建单元格。

Iterate through your model for each row, and create row and cells ( based on number of columns ). 遍历模型中的每一行,并创建行和单元格(基于列数)。

Set cell's value based on your table's data. 根据表格数据设置单元格的值。

For color : 对于颜色:

It will help if your JTable's model has not just the values for each cell, but also its back ground color. 如果JTable的模型不仅具有每个单元格的值,还具有其背景色,则将有帮助。 If not, you could get background color from the JTable given the values for row, column. 如果没有,您可以从JTable获得给定行,列的值的背景色。 You need to apply a cellstyle for each cell ( cell styles can be created independent of number of cells ) and set background color using setFillBackgroundColor() . 您需要为每个单元格应用一个单元格样式(可以独立于单元格数量创建单元格样式),并使用setFillBackgroundColor()设置背景色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM