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

2024-08-24Java开发问题
3

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

问题描述

是否可以复制 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 文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How 免费vpn to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a 免费vpn report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 vpn下载 .properties vpn下载 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 免费vpn DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change 免费vpn下载 the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13