将oracle数据导成execl

需要两个jar包 classes12.jar及jxl.jar
package ptobu_java;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;

import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;

public class expExecl {
   private Connection conn = null;

  
     public static void main(String[] args) {
      
      expExecl t = new expExecl();
      t.getConn();
      t.excel( "福建");
       //t.eExcel();
    }
    
     public void excel(String filename){
      Statement st = null;
      ResultSet rs = null;
      
      WritableWorkbook wwb;
       try {
        wwb = Workbook.createWorkbook( new File( "d://"+filename+".xls"));
        SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
             java.util.Calendar currentTime =    java.util.Calendar.getInstance();
             String dateString = formatter.format(currentTime.getTime());
        
        for (int type=8;type>=1;type--){
          System.out.println(dateString+"开始第"+type+"类");
          String sheet = "";
          String condition="";
          String title="";
          if(type==1){
            sheet="第一类";
            title="重户口";
            condition="350101";
          }if(type==2){
            sheet="第二类";
            title="出生或补录";
            condition="350102";
          }if(type==3){
            sheet="第三类";
            title="33333333333";
            condition="350103";
          }if(type==4){
            sheet="第四类";
            title="444444444444";
            condition="350104";
          }if(type==5){
            sheet="第五类";
            title="5555555555555555";
            condition="11";
          }if(type==6){
            sheet="第六类";
            title="6666666666666";
            condition="350104";
          }if(type==7){
            sheet="第七类";
            title="77777777777777777";
            condition="33";
          }if(type==8){
            sheet="第八类";
            title="888888888888";
            condition="350103";
          }
          WritableSheet ws = wwb.createSheet(sheet, 0);
          Label caption1 = new Label(0, 0, title);
          ws.mergeCells(0, 0, 10, 1);
          ws.addCell(caption1);
          Label caption41 = new Label(0, 2, "G");
                            Label caption42 = new Label(1, 2, "F0");
                            Label caption43 = new Label(2, 2, "系统码");
                            Label caption44 = new Label(3, 2, "设备码");
                            ws.addCell(caption41);
                            ws.addCell(caption42);
                            ws.addCell(caption43);
                            ws.addCell(caption44);
          st = this.conn.createStatement();
          rs = st.executeQuery("select * from a_test where c='"+condition+"'");
          int cnt = rs.getMetaData().getColumnCount();
          int row = rs.getRow();
  //        System.out.println(cnt);
  //        System.out.println(row);
          Object obj = null;
          int j=3;
          while(rs.next()){
            for (int i=1;i<=cnt;i++ ){
              obj = rs.getString(i);
              //System.out.println("i="+obj);
                                            int k = i - 1;
  
                                            if (obj == null)
                                            {
                                                    Label value = new Label(k, j, "");
                                                    ws.addCell(value);
                                            }
                                            else
                                            {
                                                    Label value = new Label(k, j, obj.toString());
                                                    ws.addCell(value);
  
                                            }
  
            }
            j++;
            //System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
          }
          
        }
        System.out.println(dateString+"结束");
        wwb.write();
        wwb.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    
    
    public void eExcel(){
    
                        WritableWorkbook wwb;
      try {
        wwb = Workbook.createWorkbook(new File("d://福建省.xls"));
        WritableSheet ws = wwb.createSheet("第一页", 9);
                         Label caption1 = new Label(0, 0, "KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码KKS编码");
                         ws.mergeCells(0, 0, 10, 1);
                        // Label caption1 = new Label(3, 0, "KKS编码");
                        // ws.mergeCells(col,row,width,heigh)
                         Label caption20 = new Label(0, 1, "电厂名称:");
                         Label caption21 = new Label(1, 1, "aaaa");
                         Label caption30 = new Label(0, 2, "电厂编码:");
                         Label caption31 = new Label(1, 2, "bbbb");

                         //Label caption40 = new Label(0, 3, "分项");
                         Label caption41 = new Label(0, 3, "G");
                         Label caption42 = new Label(1, 3, "F0");
                         Label caption43 = new Label(2, 3, "系统码");
                         Label caption44 = new Label(3, 3, "设备码");
                         Label caption45 = new Label(4, 3, "部件码");
                         Label caption46 = new Label(5, 3, "工艺码");
                         Label caption47 = new Label(6, 3, "安装地点码");
                         Label caption48 = new Label(7, 3, "地理位置码");
                         Label caption49 = new Label(8, 3, "KKS编码");
                         // Label caption410 = new Label(10, 3, "上级KKS编码");
                         Label caption411 = new Label(9, 3, "设备中文描述");
                         Label caption412 = new Label(10, 3, "专业");
                         Label caption413 = new Label(11, 3, "档号");
                         Label caption414 = new Label(12, 3, "图号");
                         Label caption415 = new Label(13, 3, "图名");
                         ws.addCell(caption1);
                         ws.addCell(caption20);
                         ws.addCell(caption21);
                         ws.addCell(caption30);
                         ws.addCell(caption31);

                         //ws.addCell(caption40);
                         ws.addCell(caption41);
                         ws.addCell(caption42);
                         ws.addCell(caption43);
                         ws.addCell(caption44);
                         ws.addCell(caption45);
                         ws.addCell(caption46);
                         ws.addCell(caption47);
                         ws.addCell(caption48);
                         ws.addCell(caption49);
                         // ws.addCell(caption410);
                         ws.addCell(caption411);
                         ws.addCell(caption412);
                         ws.addCell(caption413);
                         ws.addCell(caption414);
                         ws.addCell(caption415);
                         wwb.write();
                         wwb.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
                    
    }
    
    public void exportExcel(String czrkpcsmc){
      
      
        try
                    {
                    //构建Workbook对象, 只读Workbook对象
                    //Method 1:创建可写入的Excel工作薄
                    jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("d://test.xls"));
                    //Method 2:将WritableWorkbook直接写入到输出流
                    /*
                    OutputStream os = new FileOutputStream(targetfile);
                    jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
                    */
                    jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
                    jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cellaaa");
                    ws.addCell(labelC);
                    //添加带有字型Formatting的对象
                    jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
                    jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
                    jxl.write.Label labelCF = new jxl.write.Label(1, 0, "This is a Label Cellbbb", wcfF);
                    ws.addCell(labelCF);
                    //添加带有字体颜色Formatting的对象
                    jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
                    jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
                    jxl.write.Label labelCFC = new jxl.write.Label(1, 0, "This is a Label Cellcccc", wcfFC);
                    ws.addCell(labelCFC);
                    //2.添加Number对象
                    jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926);
                    ws.addCell(labelN);
                    //添加带有formatting的Number对象
                    jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
                    jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
                    jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
                    ws.addCell(labelNF);
                    //3.添加Boolean对象
                    jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
                    ws.addCell(labelB);
                    //4.添加DateTime对象
                    jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
                    ws.addCell(labelDT);
                    //添加带有formatting的DateFormat对象
                    jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
                    jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
                    jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);
                    ws.addCell(labelDTF);
                    wwb.write();
                    //关闭Excel工作薄对象
                    wwb.close();


                    }
                    catch (Exception e)
                    {
                    e.printStackTrace();
                    }

    }
    
    
    public Connection getConn(){
      Connection conn=null;
      try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        try {
          conn = DriverManager.getConnection(url, "xzy", "xzy");
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      this.conn=conn;
      return conn;
      
    }
    

}

你可能感兴趣的:(oracle,职场,包,import,休闲)