zoukankan      html  css  js  c++  java
  • [转]swing中如何将jtable中的数据导入到excel中?

    这个版本的代码是可以支持中文,需要导入jxl.jar包,并添加到Build Path中(自行搜索下载)。

    最终代码:

    package test;
    
    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);
        }
    }

    程序运行效果图:

    程序会在当前目录下导出一个名为results.xls的Excel,效果图:

    原文链接:swing中如何将jtable中的数据导入到excel中?

  • 相关阅读:
    cxf简单实例
    生成素描图片
    Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (2)
    Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (1)
    Spring boot的put请求 (如何实现RestFul Api风格)
    net start npf启用失败
    vue项目中:‘vue-cli-service' 不是内部或外部命令,也不是可运行的程序或批处理文件的报错
    SpringBoot + Mybatis 打印sql语句修改配置文件的方式
    vue-cli最新脚手架的安装
    SpringBoot项目中设置日期返回的格式
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/12497234.html
Copyright © 2011-2022 走看看