import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
public class SimpleTable2 {
/**
*
*/
public SimpleTable2() {
JFrame f = new JFrame();
Object[][] p = {
{"阿呆", new Integer(66), new Integer(32), new Integer(98),
new Boolean(false), new Boolean(false)},
{"阿呆", new Integer(82), new Integer(69), new Integer(128),
new Boolean(true), new Boolean(false)},};
String[] n = {"姓名", "语文", "数学", "总分", "及格", "作弊"};
TableColumn column = null;
JTable table = new JTable(p, n);
table.setPreferredScrollableViewportSize(new Dimension(550, 30));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i < 6; i++) {
// 利用JTable中的getColumnModel()方法取得TableColumnModel对象;再利用TableColumnModel界面所定义的getColumn()方法取
// TableColumn对象,利用此对象的setPreferredWidth()方法就可以控制字段的宽度.
column = table.getColumnModel().getColumn(i);
if ((i % 2) == 0)
column.setPreferredWidth(150);
else
column.setPreferredWidth(50);
}
JScrollPane scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setTitle("Simple Table");
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// FileNameExtensionFilter filter = new FileNameExtensionFilter();
// chooser.setFileFilter(filter);
// int returnVal =
chooser.showSaveDialog(null);
// if(returnVal == JFileChooser.APPROVE_OPTION) {
// System.out.println("You chose to open this file: " +
// chooser.getSelectedFile().getName());
// }
File fi = chooser.getSelectedFile();
String file = fi.getAbsolutePath()+".xls";
System.out.println(file);
try {
FileWriter out = new FileWriter(file);
for (int i = 0; i < table.getColumnCount(); i++) {
out.write(table.getColumnName(i) + "\t");
}
out.write("\n");
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
out.write(table.getValueAt(i, j).toString() + "\t");
}
out.write("\n");
}
out.close();
JOptionPane.showMessageDialog(null, "文件导出成功");
Runtime.getRuntime().exec("cmd /c start \"\" \"" + file+"\"");
}
// 打开生成的excel文件
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SimpleTable2();
}
}
稍微做了些修改
1.难道这个程序不是你写的? 书上的?
修改一下模式文件选择器的模式就可以了
2.这个是win中 start命令的问题,修改一下参数就行
3.上面两个原因造成的
ihgy