java高手快来啊!关于用JFileChooser保存文件的问题 谁帮帮我啊,急死啦!!!!!
java高手快来啊!关于用JFileChooser保存文件的问题谁帮帮我啊,急死啦!!!!!废话少说,大家先看我的代码。以下是把JTable里的数据保存到Excel中im...
java高手快来啊!关于用JFileChooser保存文件的问题 谁帮帮我啊,急死啦!!!!!
废话少说,大家先看我的代码。 以下是把JTable里的数据保存到Excel中
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.show();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//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 path=fi.getPath();
String a="导出文件.xls";
File file=new File(path+"/"+a);
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.getPath()); }
//打开生成的excel文件
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new SimpleTable2();
}
}
我有以下几个问题:
一,如何自行设置文件保存名,并且按照自己填的名字保存文件
二,为什么java本身不能识别空格键, 比如我保存路径为 D:\My Documents 但是java本身只能识别到D:\My
三,保存之后打开为什么还说有错误
首先谢谢大家的帮忙啊,最好在源代码上改 自己写的代码有点乱,因为我是初手还请大家包涵。 展开
废话少说,大家先看我的代码。 以下是把JTable里的数据保存到Excel中
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.show();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//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 path=fi.getPath();
String a="导出文件.xls";
File file=new File(path+"/"+a);
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.getPath()); }
//打开生成的excel文件
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new SimpleTable2();
}
}
我有以下几个问题:
一,如何自行设置文件保存名,并且按照自己填的名字保存文件
二,为什么java本身不能识别空格键, 比如我保存路径为 D:\My Documents 但是java本身只能识别到D:\My
三,保存之后打开为什么还说有错误
首先谢谢大家的帮忙啊,最好在源代码上改 自己写的代码有点乱,因为我是初手还请大家包涵。 展开
1个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableColumn;
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.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().endsWith(".xls");
}
public String getDescription() {
return "Excel";
}
});
// FileNameExtensionFilter filter = new FileNameExtensionFilter();
// chooser.setFileFilter(filter);
// int returnVal =
chooser.showSaveDialog(f);
// if(returnVal == JFileChooser.APPROVE_OPTION) {
// System.out.println("You chose to open this file: " +
// chooser.getSelectedFile().getName());
// }
File file = chooser.getSelectedFile();
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.getPath());
}
// 打开生成的excel文件
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SimpleTable2();
}
}
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableColumn;
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.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().endsWith(".xls");
}
public String getDescription() {
return "Excel";
}
});
// FileNameExtensionFilter filter = new FileNameExtensionFilter();
// chooser.setFileFilter(filter);
// int returnVal =
chooser.showSaveDialog(f);
// if(returnVal == JFileChooser.APPROVE_OPTION) {
// System.out.println("You chose to open this file: " +
// chooser.getSelectedFile().getName());
// }
File file = chooser.getSelectedFile();
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.getPath());
}
// 打开生成的excel文件
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SimpleTable2();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询