1、连接类:Dbutil
package sql2excel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Dbutil {
/*
* 功能:编写一个静态方法用于与数据库建立连接 输入参数:无 返回值:数据库连接对象
*/
public static Connection getConnection() {
// 定义一个连接对象
Connection conn = null;
// 创建预编译语句对象,一般都是用这个而不用Statement
PreparedStatement pre = null;
// 创建一个结果集对象
ResultSet result = null;
// 定义连接数据库的URL资源
String url = "jdbc:oracle:thin:@10.20.56.52:1521:orcl";
// 定义连接数据库的用户名称与密码
String username = "crm";
String password = "crm";
// 加载数据库连接驱动
String className = "oracle.jdbc.driver.Driver";
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获取数据库的连接对象
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("数据库连接建立成功...");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回连接对象
return conn;
}

public static void close(Connection c) {
if (c != null) {
try {
c.close();
} catch (Throwable e) {

e.printStackTrace();
}
}
}

public static void close(PreparedStatement c) {
if (c != null) {
try {
c.close();
} catch (Throwable e) {

e.printStackTrace();
}
}
}
}

2、主程序sql2excel
package sql2excel;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class sql2excel {

public void createXLS() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Dbutil.getConnection();
Date now = new Date(); 
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String nowdate = df.format(now);
// 打开文件
WritableWorkbook book = Workbook.createWorkbook(new File(nowdate+".xls"));

// 生成名为"第一页"的工作表,参数0表示这是第一
WritableSheet sheet = book.createSheet("第一页", 0);

// 设置字体为宋体,16号字,加粗,颜色为黑色
WritableFont font1 = new WritableFont(
WritableFont.createFont("宋体"), 10, WritableFont.BOLD);
font1.setColour(Colour.BLACK);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

// Label labelA = new Label(0, 0, "CALL_GUID", format1);
// Label labelB = new Label(1, 0, "RELATIONID", format1);
// Label labelC = new Label(2, 0, "ANI", format1);
// Label labelD = new Label(3, 0, "DNIS", format1);
// Label labelE = new Label(4, 0, "STAFF_ID", format1);
// Label labelF = new Label(5, 0, "CALLSTARTTIME", format1);
// Label labelG = new Label(6, 0, "CALLENDTIME", format1);
// Label labelH = new Label(7, 0, "CALLRESULT", format1);
// Label labelI = new Label(8, 0, "CALLRESULTREASON_ID", format1);
// Label labelJ = new Label(9, 0, "CALLREMARK", format1);
// Label labelK = new Label(10, 0, "EVENT_GUID", format1);

Label labelA = new Label(0, 0, "NAME", format1);