资源简介
java导出oracle到excel excel导入oracle
代码片段和文件信息
package cn.com.db.connectDB;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import cn.com.db.util.ConnectDB;
public class WriterTable{
private ConnectDB db = ConnectDB.getInstance();
/**
* conncet oracle database
*/
private void readData() {
Vector> vector = new Vector>();
Connection con = db.getConnection();
String sql = “select * from emp“;
PreparedStatement pr = null;
ResultSet re = null;
try {
pr = con.prepareStatement(sql);
re = pr.executeQuery();
Vector v = null;
while (re.next()) {
v = new Vector();
v.add(““ + re.getString(1));
v.add(““ + re.getString(2));
v.add(““ + re.getString(3));
v.add(““ + re.getString(4));
v.add(““ + re.getString(5));
v.add(““ + re.getString(6));
v.add(““ + re.getString(7));
v.add(““ + re.getString(8));
vector.add(v);
}
createExcel(vector);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* create a EXCEl
*/
private void createExcel(Vector> vector) {
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File(“d:/oracle.xls“));
WritableSheet sheet = book.createSheet(“数据库备份“ 0);
WritableCellFormat form = new WritableCellFormat();
form.setAlignment(Alignment.CENTRE);
// merge the sheet cell;
sheet.mergeCells(0 0 7 0);
Label title = new Label(0 0 “数据库备份表“form);
sheet.addCell(title);
int i = 1;
Iterator> it = vector.iterator();
while (it.hasNext()) {
Vector v = it.next();
Iterator ite = v.iterator();
int j = 0;
while (ite.hasNext()) {
Label label = new Label(j i ite.next());
sheet.addCell(label);
j++;
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
book.write();
book.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
/**
* initialization the methods of this class
*/
public void init() {
readData();
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 2656 2009-05-27 01:08 db\connectDB\WriterTable.java
I.A.... 2791 2009-05-27 01:20 db\createTable\CreateTable.java
I.A.... 651 2009-05-27 00:27 db\test\Test.java
I.A.... 1494 2009-05-26 11:15 db\util\ConnectDB.java
I..D... 0 2009-05-27 16:42 db\connectDB
I..D... 0 2009-05-27 16:42 db\createTable
I..D... 0 2009-05-27 16:42 db\test
I..D... 0 2009-05-27 16:42 db\util
I..D... 0 2009-05-27 16:42 db
----------- --------- ---------- ----- ----
7592 9
评论
共有 条评论