问题
JFileChooser fc = new JFileChooser();
int option = fc.showSaveDialog(NewJFrame1.this);
if(option == JFileChooser.APPROVE_OPTION){
String filename = fc.getSelectedFile().getName();
String path = fc.getSelectedFile().getParentFile().getPath();
int len = filename.length();
String ext = "";
String file = "";
if(len > 4){
ext = filename.substring(len-4, len);
}
if(ext.equals(".xls")){
file = path + "\\" + filename;
}else{
file = path + "\\" + filename + ".xls";
}
toExcel(jTable1, new File(file));
}
this is how i save my table to excel it works fine but i want to import these datas after i restart my program can someone help me about this ?
回答1:
You can use Apache POI to export your JTable Data.
See how to do it here, in this thread: apache poi: saving jtable to a file
Edit
I didn't read your question, if you're trying to IMPORT data, try these methods: http://www.zfqjava.com/article/How-to-import-excel-into-JTabel.html
回答2:
//Download and insert jxl-1.0.jar file
public class excelTojTable extends JFrame {
static JTable table;
static JScrollPane scroll;
// header is Vector contains table Column
static Vector headers = new Vector();
// Model is used to construct JTable
static DefaultTableModel model = null;
// data is Vector contains Data from Excel File
static Vector data = new Vector();
static JButton jbClick;
static JFileChooser jChooser;
static int tableWidth = 0; // set the tableWidth
static int tableHeight = 0; // set the tableHeight
public excelTojTable() {
super("Import Excel To JTable");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
jChooser = new JFileChooser();
jbClick = new JButton("Select Excel File");
buttonPanel.add(jbClick, BorderLayout.CENTER);
// Show Button Click Event
jbClick.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jChooser.showOpenDialog(null);
File file = jChooser.getSelectedFile();
if (!file.getName().endsWith("xls")) {
JOptionPane.showMessageDialog(null,
"Please select only Excel file.",
"Error", JOptionPane.ERROR_MESSAGE);
} else {
fillData(file);
model = new DefaultTableModel(data,
headers);
tableWidth = model.getColumnCount()
* 150;
tableHeight = model.getRowCount()
* 25;
table.setPreferredSize(new Dimension(
tableWidth, tableHeight));
table.setModel(model);
}
}
});
table = new JTable();
table.setAutoCreateRowSorter(true);
model = new DefaultTableModel(data, headers);
table.setModel(model);
table.setBackground(Color.pink);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setEnabled(false);
table.setRowHeight(25);
table.setRowMargin(4);
tableWidth = model.getColumnCount() * 150;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension(
tableWidth, tableHeight));
scroll = new JScrollPane(table);
scroll.setBackground(Color.pink);
scroll.setPreferredSize(new Dimension(300, 300));
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(buttonPanel,
BorderLayout.NORTH);
getContentPane().add(scroll,
BorderLayout.CENTER);
setSize(600, 600);
setResizable(true);
setVisible(true);
}
/**
* Fill JTable with Excel file data.
*
* @param file file :contains xls file to display in jTable
*/
void fillData(File file) {
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(file);
} catch (IOException ex) {
Logger.getLogger(
excelTojTable.class.
getName()).log(Level.SEVERE,
null, ex);
}
Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++) {
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new excelTojTable();
}
}
回答3:
try {
jxl.Workbook workbook = jxl.Workbook.getWorkbook(file);
jxl.Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++) {
jxl.Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++) {
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++) {
jxl.Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
}
catch (Exception e) {
e.printStackTrace();
}
this is how i solved my problem i hope it helps someone else
来源:https://stackoverflow.com/questions/31183684/how-to-import-datas-from-excel-to-jtable