通过JFileChooser完成导出文件(.xls)

import java.io.File;
import java.io.IOException;
import java.util.Vector;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * 导出文件
 * @author martin

 */
public class ExportFile {

    /**
     * 向Excel中导入数据
     * @param vector 内容
     * @param filepath 存储路径
     * @param title 标题
     */
    public void writeExcel(Vector<Vector<String>> vector, String filepath, String[] title) throws Exception{
        File excel = new File(filepath);
        WritableWorkbook workbook = Workbook.createWorkbook(excel);
        WritableSheet sheet = workbook.createSheet("Sheet", 0);
        Label label = null;
        for(int i=0; i<title.length; i++) {
            //Label(列号, 行号, 内容)
            label = new Label(i, 0, title[i]);                            //put the title
                sheet.addCell(label);
            }
        /*MD5 md5 = new MD5();                                        //使用MD5加密 */

        for(int i=0; i<vector.size(); i++){
            Vector<String> vtemp = vector.get(i);
                for(int j=0; j<vtemp.size(); j++) {
                    String content = vtemp.get(j);
                    /*if(j == 1){
                        content = md5.toDigest(content);
                    }*/
                    label = new Label(j, i+1, content);
                    sheet.addCell(label);
                }
            }
            workbook.write();
            workbook.close();
        }

————————————————下面为调用部分(Start)—————————————————

    /**
     * 执行导出
     * @param evt
     */
    private void btnSaveActionPerformed(ActionEvent evt) {
        TableModel model = (TableModel)tabDetail.getModel();
        if(model.getValueAt(0, 0) != null) {
            JFileChooser fileChooser = new JFileChooser();          //实例化一个文件选择器
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);    //只能选择文件
            fileChooser.showOpenDialog(this);
            try{

                if(fileChooser.getSelectedFile() != null){          //获取选择的文件
                    String strPath = fileChooser.getSelectedFile().getAbsolutePath();
                    if(!strPath.endsWith(".xls"))
                        strPath += ".xls";
                    writeExcel(model.getDataVector(), strPath, (String[])getColumuNames().toArray(new String[0]));
                    JOptionPane.showMessageDialog(null, "成功!", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
                }
            } catch(Exception e){
                JOptionPane.showMessageDialog(null, " 失败!", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

—————————————————上面为调用部分(End)—————————————————

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值