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 優華 閱讀(532) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 建昌县| 黄大仙区| 佛山市| 天峨县| 姜堰市| 会泽县| 张家川| 红原县| 商丘市| 蓬莱市| 连城县| 怀柔区| 雅江县| 巴彦淖尔市| 凤山县| 靖江市| 习水县| 寿光市| 陇川县| 栾川县| 隆子县| 瑞安市| 昭觉县| 容城县| 同江市| 凤翔县| 元朗区| 高唐县| 无棣县| 绥阳县| 会东县| 凤翔县| 四川省| 应用必备| 财经| 临洮县| 加查县| 永顺县| 陈巴尔虎旗| 诏安县| 女性|