Rising Sun

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

只有注册用户登录后才能发表评论。


网站导航: