swing中如何将jtable中的数据导入到excel中?

 我来答
步己0M
2013-10-15 · 超过67用户采纳过TA的回答
知道答主
回答量:123
采纳率:60%
帮助的人:60.4万
展开全部
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Utils; /////////////////////////////////////////////////// // import import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.table.*; import jxl.*; import jxl.write.*; import jxl.write.biff.RowsExceededException; /** * * @author Administrator */ public class ExcelExporter { public ExcelExporter() { } public void exportTable(JTable table, File file) throws IOException { try { OutputStream out = new FileOutputStream(file); TableModel model = table.getModel(); WritableWorkbook wwb = Workbook.createWorkbook(out); // 创建字表,并写入数据 WritableSheet ws = wwb.createSheet("中文", 0); // 添加标题 for (int i = 0; i < model.getColumnCount(); i++) { jxl.write.Label labelN = new jxl.write.Label(i, 0, model.getColumnName(i)); try { ws.addCell(labelN); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 添加列 for (int i = 0; i < model.getColumnCount(); i++) { for (int j = 1; j <= model.getRowCount(); j++) { jxl.write.Label labelN = new jxl.write.Label(i, j, model.getValueAt(j - 1, i).toString()); try { ws.addCell(labelN); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } wwb.write(); try { wwb.close(); } catch (WriteException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "导入数据前请关闭工作表"); } } public static void main(String[] args) { String[][] data = { {"中文", "$1275.00"}, {"Pets", "$125.00"}, {"Electronics", "$2533.00"}, {"Mensware", "$497.00"} }; String[] headers = {"Department", "Daily Revenue"}; JFrame frame = new JFrame("JTable to Excel Hack"); DefaultTableModel model = new DefaultTableModel(data, headers); final JTable table = new JTable(model); JScrollPane scroll = new JScrollPane(table); JButton export = new JButton("Export"); export.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { ExcelExporter exp = new ExcelExporter(); exp.exportTable(table, new File("results.xls")); } catch (IOException ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } } }); frame.getContentPane().add("Center", scroll); frame.getContentPane().add("South", export); frame.pack(); frame.setVisible(true); } }
龍王TA0541
2013-10-26 · 超过67用户采纳过TA的回答
知道答主
回答量:129
采纳率:0%
帮助的人:144万
展开全部
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Utils; /////////////////////////////////////////////////// // import import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.table.*; import jxl.*; import jxl.write.*; import jxl.write.biff.RowsExceededException; /** * * @author Administrator */ public class ExcelExporter { public ExcelExporter() { } public void exportTable(JTable table, File file) throws IOException { try { OutputStream out = new FileOutputStream(file); TableModel model = table.getModel(); WritableWorkbook wwb = Workbook.createWorkbook(out); // 创建字表,并写入数据 WritableSheet ws = wwb.createSheet("中文", 0); // 添加标题 for (int i = 0; i < model.getColumnCount(); i++) { jxl.write.Label labelN = new jxl.write.Label(i, 0, model.getColumnName(i)); try { ws.addCell(labelN); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 添加列 for (int i = 0; i < model.getColumnCount(); i++) { for (int j = 1; j
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式