java实现将oracle表中的数据导出到excel表里

页面导航:首页 > 软件编程 > Java编程 > java实现将oracle表中的数据导出到excel表里

java实现将oracle表中的数据导出到excel表里

来源: 作者: 时间:2016-01-18 15:52 【

1、数据库连接类:Dbutilpackage sql2excel;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaDat...
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);
Label labelB = new Label(1, 0, "ADDRE", format1);
Label labelC = new Label(2, 0, "AGE", format1);

// 将定义好的单元格添加到工作表中
// sheet.addCell(labelA);
// sheet.addCell(labelB);
// sheet.addCell(labelC);
// sheet.addCell(labelD);
// sheet.addCell(labelE);
// sheet.addCell(labelF);
// sheet.addCell(labelG);
// sheet.addCell(labelH);
// sheet.addCell(labelI);
// sheet.addCell(labelJ);
// sheet.addCell(labelK);
sheet.addCell(labelA);
sheet.addCell(labelB);
sheet.addCell(labelC);

// 创建预编译语句对象,一般都是用这个而不用Statement
PreparedStatement pre = null;
// 创建一个结果集对象
ResultSet result = null;
String sql = "select * from student";// 预编译语句
pre = conn.prepareStatement(sql);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
            //查询数据库中所有的数据
            List<StuEntity> list= StuService.getAllByDb();
while (result.next()) {
for (int i = 0; i < list.size(); i++) {

Label labelAi = new Label(0, i + 1, list.get(i).getName());
Label labelBi = new Label(1, i + 1, list.get(i).getAddre());
Label labelCi = new Label(2, i + 1, list.get(i).getAge());
sheet.addCell(labelAi);
sheet.addCell(labelBi);
sheet.addCell(labelCi);

}

}
// 写入数据并关闭文件
book.write();
book.close();
System.out.println("创建文件成功!");

} catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] args) {
new sql2excel().createXLS();
}
}

3、StuEntity
package sql2excel;

public class StuEntity {

/**
* @author liuhaijun
* 
*/
private String name;
private String age;
private String addre;

public StuEntity(String name, String addre, String age) {
this.age = age;
this.name = name;
this.addre = addre;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getAddre() {
return addre;
}
public void setAddre(String Addre) {
this.addre = addre;
}

public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

}
4、StuService
package sql2excel;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

/**
 * @author liuhaijun
 * 
 */
public class StuService {
/**
* @return
*/
public static List<StuEntity> getAllByDb() {
List<StuEntity> list = new ArrayList<StuEntity>();
try {
Connection conn = null;
conn = Dbutil.getConnection();
// 创建预编译语句对象,一般都是用这个而不用Statement
PreparedStatement pre = null;
// 创建一个结果集对象
ResultSet result = null;

String sql = "select * from student";
pre = conn.prepareStatement(sql);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
while (result.next()) {
String name = result.getString("name");
String addre = result.getString("addre");
String age = result.getString("age");
list.add(new StuEntity(name, addre, age));
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

}

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<