标题:java swing jtable 自定义导出
只看楼主
xioun
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:68
专家分:309
注 册:2013-4-8
结帖率:85.71%
 问题点数:0 回复次数:1 
java swing jtable 自定义导出
整理了一下jtable自定义导出
-----------------------------------
package test;

import java.awt.*;
import java.awt.event.*;
import *;
import javax.swing.*;
import javax.swing.table.*;
public class ExeclExporter  {
    public ExeclExporter() { }

    public void exportTable(JTable table, File file) throws IOException {
        TableModel model = table.getModel();
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
        for(int i=0; i < model.getColumnCount(); i++) {
            bWriter.write(model.getColumnName(i));
            bWriter.write("\t");
        }
        bWriter.newLine();
        for(int i=0; i< model.getRowCount(); i++) {
            for(int j=0; j < model.getColumnCount(); j++) {
                bWriter.write(model.getValueAt(i,j).toString());
                bWriter.write("\t");
            }
            bWriter.newLine();
        }
        bWriter.close();
        System.out.println("write out to: " + file);
    }
   
    public static void main(String[] args) {
        String[][] data = {
            { "Housewares",  "$1275.00" },
            { "Pets",         "$125.00" },
            { "Electronics", "$2533.00" },
            { "Mensware",     "$497.00" }
        };
        String[] headers = { "Department", "Daily Revenue" };

        final 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) {
                   // TODO add your handling code here:导出
                FileDialog fd = new FileDialog(frame, "保存流水记录", FileDialog.SAVE);
                fd.setLocation(400, 250);
                fd.setVisible(true);
                String stringfile = fd.getDirectory()+fd.getFile()+".xls";
                   try {
                       ExeclExporter oDao = new ExeclExporter();
                    oDao.exportTable(table, new File(stringfile));
                   } 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);
    }
}

搜索更多相关主题的帖子: package public import java file 
2013-10-08 11:51
ly_4223
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2013-1-13
得分:0 
ExeclExporter oDao = new ExeclExporter();这个需要引入什么包呢?
2013-10-16 20:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-421565-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019274 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved