konhon

忘掉過去,展望未來。找回自我,超越自我。
逃避不一定躲的过, 面对不一定最难过, 孤单不一定不快乐, 得到不一定能长久, 失去不一定不再拥有, 可能因为某个理由而伤心难过, 但我却能找个理由让自己快乐.

Google

语源科技BlogJava 首页 新随笔 联系 聚合 管理
  203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

 

  1* 
  2 * QuickExcel.java 
  3 * 作者:杨庆成 
  4 * Created on 2004年11月22日, 下午4:05 
  5 * 在实际应用中经常要将数据库中的表导入Excel 
  6 * 本人在Apache的POI基础上写了一个简单的类 
  7 * 有不当指出请指正,谢谢! 
  8 *  
  9 */ 
 10
 11package yqc.poi; 
 12
 13import java.sql.*; 
 14import java.util.*; 
 15import java.io.*; 
 16import java.io.ByteArrayInputStream; 
 17import java.io.FileInputStream; 
 18import java.io.FileOutputStream; 
 19import java.io.IOException; 
 20
 21import org.apache.poi.hssf.usermodel.*; 
 22import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
 23import org.apache.poi.hssf.record.*; 
 24import org.apache.poi.hssf.model.*; 
 25import org.apache.poi.hssf.usermodel.*; 
 26import org.apache.poi.hssf.util.*;import yqc.sql.*; 
 27
 28/** 
 29 * 
 30 * @author  Administrator 
 31 */
 
 32public class QuickExcel { 
 33     
 34    /** Creates a new instance of QuickExcel */ 
 35    private QuickExcel(String file){ 
 36        _file=file; 
 37    }
 
 38     
 39    private void open()throws IOException{ 
 40        InputStream stream = null; 
 41        Record[] records = null; 
 42        POIFSFileSystem fs = 
 43            new POIFSFileSystem(new FileInputStream(_file)); 
 44        _wb = new HSSFWorkbook(fs); 
 45    }
 
 46     
 47    private void create(){ 
 48        _wb=new HSSFWorkbook(); 
 49    }
 
 50     
 51    public static QuickExcel newInstance (String file){ 
 52        QuickExcel qe=new QuickExcel(file); 
 53        qe.create(); 
 54        return qe; 
 55    }
 
 56     
 57    public static QuickExcel openInstance(String file) throws IOException { 
 58        QuickExcel qe=new QuickExcel(file); 
 59        qe.open(); 
 60        return qe; 
 61    }
 
 62     
 63    public void close(){ 
 64        try{ 
 65            FileOutputStream fileOut = new FileOutputStream(_file); 
 66            _wb.write(fileOut);//把Workbook对象输出到文件workbook.xls中 
 67            fileOut.close(); 
 68        }
 
 69        catch (Exception ex){ 
 70            System.out.println(ex.getMessage()); 
 71        }
 
 72    }
 
 73     
 74    private void removeSheet(String sheetName){ 
 75        int i=_wb.getSheetIndex("sheetName"); 
 76        if (i>=0) _wb.removeSheetAt(i); 
 77    }
 
 78     
 79    public int fillSheet (ResultSet rs,String sheetName)throws SQLException { 
 80        HSSFSheet st= _wb.createSheet(sheetName); 
 81        ResultSetMetaData rsmd= rs.getMetaData(); 
 82        int index=0; 
 83        int result=0; 
 84        HSSFRow row=st.createRow(index++); 
 85        for(int i=1;i<=rsmd.getColumnCount();++i){ 
 86            HSSFCell cell=row.createCell((short)(i-1)); 
 87            cell.setCellValue(rsmd.getColumnName(i)); 
 88        }
 
 89        while(rs.next()) { 
 90            result++; 
 91            row=st.createRow(index++); 
 92            for(int i=1;i<=rsmd.getColumnCount();++i){ 
 93                HSSFCell cell=row.createCell((short)(i-1)); 
 94                cell.setEncoding(cell.ENCODING_UTF_16); 
 95                cell.setCellValue(rs.getString(i)); 
 96            }
 
 97        }
 
 98        return result; 
 99}
 
100     
101    public static void main(String[] args){ 
102        try{ 
103            QuickConnection qc=new MssqlConnection("jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=ls"); 
104            QuickExcel qe=QuickExcel.newInstance("a.xls"); 
105            qc.connect(); 
106            String sql="select * from ls.dbo.radio1_emcee"; 
107            ResultSet rs=qc.getStatement().executeQuery(sql); 
108            qe.fillSheet(rs,"MT"); 
109            qe.close(); 
110            qe=QuickExcel.openInstance("a.xls"); 
111            qe.fillSheet(rs,"MO"); 
112            qe.close(); 
113            qc.close(); 
114        }
 
115        catch (SQLException ex){ 
116            System.out.println(ex.getMessage()); 
117        }
 
118        catch (IOException ex){ 
119            System.out.println(ex.getMessage()); 
120        }
 
121    }
 
122     
123    HSSFWorkbook _wb; 
124    String _file="new.xls"; 
125}
 
126
posted on 2005-08-09 22:25 konhon 优华 阅读(545) 评论(0)  编辑  收藏 所属分类: Java