konhon

          忘掉過(guò)去,展望未來(lái)。找回自我,超越自我。
          逃避不一定躲的過(guò), 面對(duì)不一定最難過(guò), 孤單不一定不快樂(lè), 得到不一定能長(zhǎng)久, 失去不一定不再擁有, 可能因?yàn)槟硞€(gè)理由而傷心難過(guò), 但我卻能找個(gè)理由讓自己快樂(lè).

          Google

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

           

            1* 
            2 * QuickExcel.java 
            3 * 作者:楊慶成 
            4 * Created on 2004年11月22日, 下午4:05 
            5 * 在實(shí)際應(yīng)用中經(jīng)常要將數(shù)據(jù)庫(kù)中的表導(dǎo)入Excel 
            6 * 本人在Apache的POI基礎(chǔ)上寫(xiě)了一個(gè)簡(jiǎn)單的類 
            7 * 有不當(dāng)指出請(qǐng)指正,謝謝! 
            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對(duì)象輸出到文件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 優(yōu)華 閱讀(533) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 镇远县| 广州市| 江西省| 龙门县| 丰镇市| 莎车县| 无极县| 依安县| 伊春市| 广西| 蒙自县| 巴东县| 福鼎市| 开原市| 靖边县| 海南省| 新巴尔虎右旗| 将乐县| 于田县| 饶河县| 隆昌县| 星座| 泸州市| 耿马| 通州市| 恩施市| 曲松县| 玉树县| 遵化市| 林口县| 当阳市| 宕昌县| 甘谷县| 南开区| 伊通| 霍城县| 长寿区| 湖南省| 上虞市| 三都| 常德市|