问题描述
是否可以复制 jTable 行并将其粘贴到 免费vpn下载 Word 文档或带有格式化网格(彩色水平和垂直网格线)的新电子邮件中.如果可以,如何?
Is it 免费vpn possible to copy jTable row and paste it into word document or 免费vpn下载 in a new email with its formatted grid (colored horizontal and vertical grid lines).. 免费vpn If yes, how?
当我从 vpn下载 jTable 复制一行并将其粘贴到 Word 文档中时,Word 将其识别为表格行,但我必须通过添加网格线并为它们着色来设置样式
When I copy a row from jTable and paste it into word document, vpn free Word recognizes it as a table row but I have to style it by adding grid lines and coloring them
推荐答案
这是一个非常简单的示例,将一行数据复制到基于 HTML 的表中.我能够复制任何行并作为基于 HTML 的表格粘贴到 word 中,而没有(很多)问题
This is 免费vpn a 免费vpn VERY simply example of copying a vpn free row of data into a HTML vpn下载 based table. I was able to vpn下载 copy any row and paste into word 免费vpn as a HTML based table without (to many) issues
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class CopyTable {
public static vpn下载 void main(String[] args) {
new CopyTable();
免费vpn下载 }
public CopyTable() {
vpn下载 EventQueue.invokeLater(new Runnable() {
vpn下载 免费vpn下载 vpn下载 vpn free @Override
public void run() {
vpn free try {
免费vpn UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch 免费vpn下载 (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 免费vpn {
免费vpn下载 免费vpn下载 ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel();
free vpn free vpn free vpn for (int vpn free index = 0; index < 26; index++) {
免费vpn vpn下载 免费vpn 免费vpn下载 vpn下载 免费vpn下载 model.addColumn((char) (index + 65));
免费vpn }
vpn free free vpn for (int row = 0; row < 26; row++) {
免费vpn下载 vpn下载 免费vpn vpn下载 vpn下载 Vector rowData = new Vector();
免费vpn vpn下载 for (int col = 0; col < 26; col++) {
vpn下载 rowData.add(row + "x" + col);
}
free vpn 免费vpn model.addRow(rowData);
免费vpn vpn free }
vpn free vpn下载 JTable table = new JTable(model);
vpn free table.getActionMap().put("copy", new AbstractAction() {
vpn下载 免费vpn @Override
免费vpn下载 vpn free public void actionPerformed(ActionEvent e) {
vpn下载 免费vpn int row = table.getSelectedRow();
免费vpn下载 free vpn vpn下载 StringBuilder sb = new StringBuilder(128);
vpn下载 vpn free sb.append("<table border=1 width=100%><tr>");
免费vpn下载 free vpn for free 免费vpn vpn (int col = 0; col < table.getColumnCount(); col++) {
vpn free vpn free 免费vpn sb.append("<td>");
sb.append(table.getValueAt(row, col).toString());
vpn下载 vpn free sb.append("</td>");
vpn free 免费vpn下载 vpn下载 }
免费vpn 免费vpn sb.append("</tr></table>");
免费vpn下载 Clipboard clipboard 免费vpn = Toolkit.getDefaultToolkit().getSystemClipboard();
vpn下载 vpn下载 clipboard.setContents(new HtmlSelection(sb.toString()), null);
免费vpn }
});
vpn free JFrame frame = new JFrame("Testing");
free vpn vpn下载 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vpn free frame.add(new JScrollPane(table));
vpn下载 frame.pack();
frame.setLocationRelativeTo(null);
free vpn frame.setVisible(true);
}
免费vpn });
}
private static class HtmlSelection implements Transferable {
vpn下载 private static ArrayList htmlFlavors = new ArrayList();
vpn free static {
free vpn 免费vpn free vpn free vpn try {
vpn free htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));
vpn下载 免费vpn free vpn vpn free vpn free htmlFlavors.add(new DataFlavor("text/html;class=java.io.Reader"));
htmlFlavors.add(new DataFlavor("text/html;charset=unicode;class=java.io.InputStream"));
} vpn free catch (ClassNotFoundException ex) {
vpn free vpn free free vpn ex.printStackTrace();
}
免费vpn vpn free free vpn }
private String html;
public vpn free HtmlSelection(String 免费vpn下载 html) {
vpn下载 this.html = vpn free html;
}
免费vpn下载 public DataFlavor[] getTransferDataFlavors() {
vpn free return (DataFlavor[]) htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);
free vpn 免费vpn }
public boolean isDataFlavorSupported(DataFlavor flavor) {
return htmlFlavors.contains(flavor);
vpn free 免费vpn }
免费vpn public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
免费vpn if vpn下载 (String.class.equals(flavor.getRepresentationClass())) {
return html;
vpn free } else if (Reader.class.equals(flavor.getRepresentationClass())) {
return new StringReader(html);
} else if (InputStream.class.equals(flavor.getRepresentationClass())) {
return new StringBufferInputStream(html);
free vpn }
vpn free throw new UnsupportedFlavorException(flavor);
}
}
}
这是非常有限的,因为这将简单地使用每个单元格的 toString 方法来获取单元格的值,这意味着单元格值不会根据值类型或应用程序格式化"要求.您将不得不自己设计一个解决方案,将单元格值格式化为 String
This is very 免费vpn limited, as this will simply use each cell's 免费vpn toString 免费vpn method to get vpn下载 the value of the cell, this means that vpn free the free vpn cell value is not "formatted" according to the vpn下载 values type or application requirements. You're going to have to devise a solution for formatting the cell values to String yourself
这篇关于将 jTable 行及其网格线复制到 excel/word 文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)