java 如何将数据从excel导入jTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31183684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to import datas from excel to jTable?
提问by user3670278
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 ?
这就是我如何将我的表格保存到 excel 它工作正常但我想在我重新启动我的程序后导入这些数据有人可以帮助我吗?
回答by Gabriel Camara
You can use Apache POI to export your JTable Data.
您可以使用 Apache POI 导出 JTable 数据。
See how to do it here, in this thread: apache poi: saving jtable to a file
在此线程中查看如何执行此操作: apache poi:将 jtable 保存到文件
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
我没有读你的问题,如果你想导入数据,试试这些方法:http: //www.zfqjava.com/article/How-to-import-excel-into-JTabel.html
回答by Madhusara Maheshka
//Download and insert jxl-1.0.jarfile
//下载并插入jxl-1.0.jar文件
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();
}
}
回答by user3670278
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
这就是我解决我的问题的方法我希望它可以帮助其他人