• <legend id='A0G7A'><style id='A0G7A'><dir id='A0G7A'><q id='A0G7A'></q></dir></style></legend>
      <bdo id='A0G7A'></bdo><ul id='A0G7A'></ul>
      <i id='A0G7A'><tr id='A0G7A'><dt id='A0G7A'><q id='A0G7A'><span id='A0G7A'><b id='A0G7A'><form id='A0G7A'><ins id='A0G7A'></ins><ul id='A0G7A'></ul><sub id='A0G7A'></sub></form><legend id='A0G7A'></legend><bdo id='A0G7A'><pre id='A0G7A'><center id='A0G7A'></center></pre></bdo></b><th id='A0G7A'></th></span></q></dt></tr></i><div id='A0G7A'><tfoot id='A0G7A'></tfoot><dl id='A0G7A'><fieldset id='A0G7A'></fieldset></dl></div>
      <tfoot id='A0G7A'></tfoot>

        <small id='A0G7A'></small><noframes id='A0G7A'>

      1. 将 jTable 行及其网格线复制到 excel/word 文档中

        时间:2024-08-24

          <legend id='xH7dr'><style id='xH7dr'><dir id='xH7dr'><q id='xH7dr'></q></dir></style></legend>
            <tbody id='xH7dr'></tbody>

          <small id='xH7dr'></small><noframes id='xH7dr'>

        • <i id='xH7dr'><tr id='xH7dr'><dt id='xH7dr'><q id='xH7dr'><span id='xH7dr'><b id='xH7dr'><form id='xH7dr'><ins id='xH7dr'></ins><ul id='xH7dr'></ul><sub id='xH7dr'></sub></form><legend id='xH7dr'></legend><bdo id='xH7dr'><pre id='xH7dr'><center id='xH7dr'></center></pre></bdo></b><th id='xH7dr'></th></span></q></dt></tr></i><div id='xH7dr'><tfoot id='xH7dr'></tfoot><dl id='xH7dr'><fieldset id='xH7dr'></fieldset></dl></div>

            <tfoot id='xH7dr'></tfoot>

                <bdo id='xH7dr'></bdo><ul id='xH7dr'></ul>

                1. 本文介绍了将 jTable 行及其网格线复制到 excel/word 文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以复制 jTable 行并将其粘贴到 Word 文档或带有格式化网格(彩色水平和垂直网格线)的新电子邮件中.如果可以,如何?

                  Is it possible to copy jTable row and paste it into word document or in a new email with its formatted grid (colored horizontal and vertical grid lines).. If yes, how?

                  当我从 jTable 复制一行并将其粘贴到 Word 文档中时,Word 将其识别为表格行,但我必须通过添加网格线并为它们着色来设置样式

                  When I copy a row from jTable and paste it into word document, 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 a VERY simply example of copying a row of data into a HTML based table. I was able to copy any row and paste into word 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 void main(String[] args) {
                          new CopyTable();
                      }
                  
                      public CopyTable() {
                          EventQueue.invokeLater(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                  } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                                      ex.printStackTrace();
                                  }
                  
                                  DefaultTableModel model = new DefaultTableModel();
                                  for (int index = 0; index < 26; index++) {
                                      model.addColumn((char) (index + 65));
                                  }
                  
                                  for (int row = 0; row < 26; row++) {
                                      Vector rowData = new Vector();
                                      for (int col = 0; col < 26; col++) {
                                          rowData.add(row + "x" + col);
                                      }
                                      model.addRow(rowData);
                                  }
                  
                                  JTable table = new JTable(model);
                                  table.getActionMap().put("copy", new AbstractAction() {
                                      @Override
                                      public void actionPerformed(ActionEvent e) {
                                          int row = table.getSelectedRow();
                                          StringBuilder sb = new StringBuilder(128);
                                          sb.append("<table border=1 width=100%><tr>");
                                          for (int col = 0; col < table.getColumnCount(); col++) {
                                              sb.append("<td>");
                                              sb.append(table.getValueAt(row, col).toString());
                                              sb.append("</td>");
                                          }
                                          sb.append("</tr></table>");
                  
                                          Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                                          clipboard.setContents(new HtmlSelection(sb.toString()), null);
                                      }
                                  });
                  
                                  JFrame frame = new JFrame("Testing");
                                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  frame.add(new JScrollPane(table));
                                  frame.pack();
                                  frame.setLocationRelativeTo(null);
                                  frame.setVisible(true);
                              }
                          });
                      }
                  
                      private static class HtmlSelection implements Transferable {
                  
                          private static ArrayList htmlFlavors = new ArrayList();
                  
                          static {
                  
                              try {
                  
                                  htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));
                  
                                  htmlFlavors.add(new DataFlavor("text/html;class=java.io.Reader"));
                  
                                  htmlFlavors.add(new DataFlavor("text/html;charset=unicode;class=java.io.InputStream"));
                  
                              } catch (ClassNotFoundException ex) {
                  
                                  ex.printStackTrace();
                  
                              }
                  
                          }
                  
                          private String html;
                  
                          public HtmlSelection(String html) {
                  
                              this.html = html;
                  
                          }
                  
                          public DataFlavor[] getTransferDataFlavors() {
                  
                              return (DataFlavor[]) htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);
                  
                          }
                  
                          public boolean isDataFlavorSupported(DataFlavor flavor) {
                  
                              return htmlFlavors.contains(flavor);
                  
                          }
                  
                          public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                  
                              if (String.class.equals(flavor.getRepresentationClass())) {
                  
                                  return html;
                  
                              } else if (Reader.class.equals(flavor.getRepresentationClass())) {
                  
                                  return new StringReader(html);
                  
                              } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
                  
                                  return new StringBufferInputStream(html);
                  
                              }
                  
                              throw new UnsupportedFlavorException(flavor);
                  
                          }
                  
                      }
                  }
                  

                  这是非常有限的,因为这将简单地使用每个单元格的 toString 方法来获取单元格的值,这意味着单元格值不会根据值类型或应用程序格式化"要求.您将不得不自己设计一个解决方案,将单元格值格式化为 String

                  This is very limited, as this will simply use each cell's toString method to get the value of the cell, this means that the cell value is not "formatted" according to the 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 文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为您的应用程序启用网格/集群的最佳 Java 库是什么? 下一篇:在Java中为网格创建绘制矩形(填充黑色)函数

                  相关文章

                2. <tfoot id='anVBr'></tfoot>
                3. <legend id='anVBr'><style id='anVBr'><dir id='anVBr'><q id='anVBr'></q></dir></style></legend>
                4. <i id='anVBr'><tr id='anVBr'><dt id='anVBr'><q id='anVBr'><span id='anVBr'><b id='anVBr'><form id='anVBr'><ins id='anVBr'></ins><ul id='anVBr'></ul><sub id='anVBr'></sub></form><legend id='anVBr'></legend><bdo id='anVBr'><pre id='anVBr'><center id='anVBr'></center></pre></bdo></b><th id='anVBr'></th></span></q></dt></tr></i><div id='anVBr'><tfoot id='anVBr'></tfoot><dl id='anVBr'><fieldset id='anVBr'></fieldset></dl></div>

                    <small id='anVBr'></small><noframes id='anVBr'>

                        <bdo id='anVBr'></bdo><ul id='anVBr'></ul>