简体   繁体   English

使用HTML标签将Swing JTable导出到Excel

[英]Export Swing JTable to Excel with HTML Tags

How to export JTable with colors to excel? 如何导出带有颜色的JTable来表现出色? Since HTML tags are included excel does not render table correctly, only HTML tag gets viewed. 由于包含了HTML标签,因此excel无法正确呈现表格,因此只能查看HTML标签。

Well I am not sure exactly what you mean however I would guess however if you break it down various tasks then firstly you will need to iterate of the rows and columns of the TableModel associated with your table and build an HTML String 好吧,我不确定您的意思到底是什么,但是我猜想,如果将其分解为各种任务,那么首先您需要迭代与表关联的TableModel的行和列,并构建一个HTML字符串

TableModel model = table.getModel();
StringBuilder html = new StringBuilder("<html><table>");
//for each row append <tr>
//for each column append<td> + model value for row,col
//close </td>
//close</tr>
builder.append("</table></html>";

If you saved this out to a .html file then Excel should open it. 如果您将其保存为.html文件,则Excel应该将其打开。

I also have a project here that will convert HTML to Excel spreadsheets with support for styling via css: 我这里还有一个项目,该项目将HTML转换为Excel电子表格,并支持通过CSS进行样式设置:

https://github.com/alanhay/html-exporter https://github.com/alanhay/html-exporter

Maybe you could then try that to transform your generated HTML to proper .xlsx file or maybe the source will give you some further ideas. 也许您可以尝试将生成的HTML转换为适当的.xlsx文件,或者源代码可以为您提供更多的建议。

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

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