copydatatoexcel.java~15~

来自「java(Swing) access做的成绩管理系统」· JAVA~15~ 代码 · 共 66 行

JAVA~15~
66
字号
package edu.xscj.action;

import javax.swing.JTable;
import java.awt.datatransfer.*;
import java.awt.*;
import javax.swing.JOptionPane;

public class CopyDataToExcel {

    public CopyDataToExcel() {
    }

    public void copy(JTable jtable) {
        if (jtable == null) {
            return;
        }

        int row = jtable.getRowCount();
        int col = jtable.getColumnCount();

        StringBuffer sbf = new StringBuffer();
        Clipboard clipboard;
        StringSelection strselection;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                sbf.append(jtable.getModel().getValueAt(i, j));
                if (j < col - 1) {
                    sbf.append("\t");
                }
            }
            sbf.append("\n");
        }
        strselection = new StringSelection(sbf.toString());
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(strselection, strselection);
    }

    public void copyRow(JTable jtable) {
        if (jtable == null) {
            return;
        }

        int row = jtable.getRowCount();
        int col = jtable.getColumnCount();

        StringBuffer sbf = new StringBuffer();
        Clipboard clipboard;
        StringSelection strselection;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                sbf.append(jtable.getModel().getValueAt(i, j));
                if (j < col - 1) {
                    sbf.append("\t");
                }
            }
            sbf.append("\n");
        }
        strselection = new StringSelection(sbf.toString());
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(strselection, strselection);
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?