很久以前参考的一个例子
如果是b/s结构的系统,这种打印方式不太容易实现
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;public class JTableTest extends JFrame{    final static Button button = new Button("Print"); public static void main(String args[]) {
JTableTest Test = new JTableTest();
    Test.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
         System.exit(0);
       }
     });
Test.setSize(300,200);
Test.setVisible(true);
}

public JTableTest() {        button.addActionListener(this);
JTable jt = new JTable(new MyTableModel());
//MyTable jt = new MyTable(new MyTableModel());
jt.setRowHeight(20);
jt.setCellSelectionEnabled(false);
getContentPane().add(new JScrollPane(jt));

TablePrinter tabeprint = new TablePrinter(jt);
tabeprint.print();

}

public void actionPerformed(ActionEvent e) {
   
if (e.getSource() instanceof Button) {
         if(e.getSource() == Button) {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PrintJob.setPrintable(this);
        
//Book book = new Book ();
         //book.append (new ShapesPrint(), printJob.defaultPage());
         //book.append (new ShapesPrint(), printJob.defaultPage());
//printJob.setPageable (book);         if (printJob.printDialog()) {
         try {
                  printJob.print();  
          } catch (Exception PrintException) {
                 PrintException.printStackTrace();
             }
         }
}
}
}
}
/*
class MyTable extends JTable {
public MyTable(TableModel model){
super(model);
    setUI(new MultiSpanCellTableUI());
     setCellSelectionEnabled(false);//设置单元格为不可选
     getTableHeader().setReorderingAllowed(false);//设置是否可以修改列头和列
     setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
}
*/
class MyTableModel extends AbstractTableModel {
private String[] columns = {"姓名","年龄","已婚"};
private Object[][] data = 
{{"王晓明asfsdfsdfsdf",new Integer(28),new Boolean(true)},
 {"程晓华",new Integer(26),new Boolean(false)},
 {"张小李",new Integer(35),new Boolean(true)},
 {"林晓力",new Integer(18),new Boolean(false)}
};

public int getColumnCount() {
return (columns.length);
}

public int getRowCount() {
return (data.length);
}

public String getColumnName(int col) {
return null;
}

public Object getValueAt(int row, int col) {
return (data[row][col]);
}

public Class getColumnClass(int col) {
return (getValueAt(0,col).getClass());
}

public boolean isCellEditable( int row,int col) {
return (col > 0 );
}

public void setValueAt(Object value, int row, int col) {
if(col == 1) {
try {
data[row][col] = new Integer((String)value);
}
catch (NumberFormatException e) {
}
}else 
data[row][col] = value;
fireTableCellUpdated(row,col);

}
}