java 导出excel方法

http://download.youkuaiyun.com/source/1781433下载jxl.jar文件

/*Title是保存出来的文件名,gbl_LastOpenPath用于记录上次打开的路径*/
public void ExportToExcel(JTable table, String Title){
        File DefaultFile;
        JFileChooser fc = new JFileChooser();
        File file;
        if(gbl_LastOpenPath!=null){
            DefaultFile = new File(gbl_LastOpenPath+"/"+Title+".xls");
            if(DefaultFile.exists()){
                //fc.setSelectedFile(DefaultFile);
//                fc.setCurrentDirectory(DefaultFile); //设置打开的默认路径
                fc.setSelectedFile(DefaultFile);
            }
        }
        if(gbl_LastOpenPath==null){
            //String s = System.getProperty("user.dir");
            fc.setSelectedFile(new File("c:/"+Title+".xls"));
        }
        int Selection = fc.showDialog(this, null);
        fc.setVisible(true);
        if(Selection==0){
            file = fc.getSelectedFile();
            gbl_LastOpenPath = file.getParent();
            /*开始导出数据*/
            try {
                WritableWorkbook book = Workbook.createWorkbook(file);
                WritableSheet sheet=book.createSheet(Title,0); //工作表名称
                sheet.mergeCells(0,0,(table.getColumnCount()-1),0); //合并第一行
                /*表头:加粗*/
                WritableFont CaptionFont = new WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
                /*表头:居中*/
                WritableCellFormat CatpionStyle = new WritableCellFormat(CaptionFont);
                CatpionStyle.setAlignment(Alignment.CENTRE);
                CatpionStyle.setVerticalAlignment(VerticalAlignment.CENTRE);
                sheet.addCell(new Label(0,0,Title,CatpionStyle));
                /*写表头*/
                WritableFont TitleFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
                /*表头:居中*/
                WritableCellFormat TitleStyle = new WritableCellFormat(TitleFont);
                TitleStyle.setAlignment(Alignment.CENTRE);
                TitleStyle.setVerticalAlignment(VerticalAlignment.CENTRE);
                for(int i = 0;i

  
                    sheet.addCell(new Label(i,1,table.getTableHeader().getColumnModel().getColumn(i).getHeaderValue().toString(),TitleStyle));
                }
                for(int i = 0;i

  
                    for(int j = 0;j

  
                        if(table.getValueAt(i, j)!=null){
                            Label label = new Label(j,i+2,table.getValueAt(i, j).toString());
                            sheet.addCell(label);
                        }
                    }
                }
                book.write();
                book.close();
                JOptionPane.showMessageDialog(null, "已成功导出到"+file.getPath());
            } catch (WriteException ex) {
                //Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                //Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
 
 
        }
 
    }
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值