java 用poi包导出excel

/*
 * ExportExcel.java
 *
 * Created on 2007年4月24日, 下午 11:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/

package  ecs.com.system;

import  java.io.File;
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  javax.swing.JFileChooser;
import  javax.swing.JTable;
import  javax.swing.filechooser.FileFilter;
import  javax.swing.table.TableModel;
import  org.apache.poi.hssf.usermodel.HSSFCell;
import  org.apache.poi.hssf.usermodel.HSSFCellStyle;
import  org.apache.poi.hssf.usermodel.HSSFFont;
import  org.apache.poi.hssf.usermodel.HSSFRichTextString;
import  org.apache.poi.hssf.usermodel.HSSFRow;
import  org.apache.poi.hssf.usermodel.HSSFSheet;
import  org.apache.poi.hssf.usermodel.HSSFWorkbook;
import  org.apache.poi.hssf.util.HSSFColor;

/**
 *
 * 
@author JoelHe
 
*/

public   class  ExportExcel  {
    
    
/** Creates a new instance of ExportExcel */
    JTable table;
    FileOutputStream fos;
    JFileChooser jfc
=new JFileChooser();
    
public ExportExcel(JTable table) {
        
this.table=table;
        jfc.addChoosableFileFilter(
new FileFilter(){
            
public boolean accept(File file){
                
return (file.getName().indexOf("xls")!=-1);
            }

            
public String getDescription(){
                
return "Excel";
            }

        }
);
        jfc.showSaveDialog(
null);
        File file
=jfc.getSelectedFile();
        
try {
            
this.fos=new FileOutputStream(file+".xls");
        }
 catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

    }

    
public void export(){
        HSSFWorkbook wb
=new HSSFWorkbook();
        HSSFSheet hs
=wb.createSheet();
        TableModel tm
=table.getModel();
        
int row=tm.getRowCount();
        
int cloumn=tm.getColumnCount();
        HSSFCellStyle style
=wb.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font 
= wb.createFont();
        font.setFontHeightInPoints((
short)11);
        style.setFont(font);
        HSSFCellStyle style1
=wb.createCellStyle();
        style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style1.setFillForegroundColor(HSSFColor.ORANGE.index);
        style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font1 
= wb.createFont();
        font1.setFontHeightInPoints((
short)15);
        font1.setBoldweight((
short)700);
        style1.setFont(font);
        
for(int i=0;i<row+1;i++){
            HSSFRow hr
=hs.createRow(i);
            
for(int j=0;j<cloumn;j++){
                
if(i==0){
                    String value
=tm.getColumnName(j);
                    
int len=value.length();
                    hs.setColumnWidth((
short)j,(short)(len*400));
                    HSSFRichTextString srts
=new HSSFRichTextString(value);
                    HSSFCell hc
=hr.createCell((short)j);
                    hc.setEncoding((
short)1);
                    hc.setCellStyle(style1);
                    hc.setCellValue(srts);
                }
else{
                    String value
=tm.getValueAt(i-1,j).toString();
                    HSSFRichTextString srts
=new HSSFRichTextString(value);
                    HSSFCell hc
=hr.createCell((short)j);
                    hc.setEncoding((
short)1);
                    hc.setCellStyle(style);
                    
if(value.equals("")||value==null){
                        hc.setCellValue(
new HSSFRichTextString(""));
                    }
else{
                        hc.setCellValue(srts);
                    }

                }

            }

        }

        
try {
            wb.write(fos);
            fos.close();
        }
 catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值