konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

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

           

            1* 
            2 * QuickExcel.java 
            3 * 作者:楊慶成 
            4 * Created on 2004年11月22日, 下午4:05 
            5 * 在實際應(yīng)用中經(jīng)常要將數(shù)據(jù)庫中的表導(dǎo)入Excel 
            6 * 本人在Apache的POI基礎(chǔ)上寫了一個簡單的類 
            7 * 有不當(dā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對象輸出到文件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) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 秀山| 达州市| 乐平市| 益阳市| 双柏县| 沧州市| 鹤山市| 延寿县| 永春县| 济宁市| 尼勒克县| 高雄市| 静海县| 方城县| 蓬溪县| 平塘县| 崇礼县| 中宁县| 武宣县| 孝感市| 新乡市| 桐庐县| 大港区| 洛隆县| 揭东县| 星座| 广河县| 宁津县| 武威市| 临湘市| 都昌县| 陆良县| 北票市| 色达县| 临朐县| 灵宝市| 奉节县| 临洮县| 宁蒗| 油尖旺区| 陈巴尔虎旗|