import java.io.File;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import java.sql.*;
import java.util.*;
public class DBtoExcel {
/**
* 导出Excel表
* @param rs 数据库结果集
* @param filePath 要保存的路径,文件名为 fileName.xls
* @param sheetName 工作簿名称 工作簿名称,本方法目前只支持导出一个Excel工作簿
* @param columnName 列名,类型为Vector
*/
public void WriteExcel(ResultSet rs, String filePath, String sheetName, Vector<String> columnName) {
WritableWorkbook workbook = null;
WritableSheet sheet = null;
int rowNum = 1; // 从第一行开始写入
try {
workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
this.writeCol(sheet, columnName, 0); // 首先将列名写入
// 将结果集写入
while(rs.next()) {
Vector<String> col = new Vector<String>(); // 用以保存一行数据
for(int i = 1; i <= columnName.size(); i++) { // 将一行内容保存在col中
col.add(rs.getString(i));
}
// 写入Excel
this.writeCol(sheet, col, rowNum++);
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
// 关闭
workbook.write();
workbook.close();
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
/***
* 将数组写入工作簿
* @param sheet 要写入的工作簿
* @param col 要写入的数据数组
* @param rowNum 要写入哪一行
* @throws WriteException
* @throws RowsExceededException
*/
private void writeCol(WritableSheet sheet, Vector<String> col, int rowNum) throws RowsExceededException, WriteException {
int size = col.size(); // 获取集合大小
for(int i = 0; i < size; i++) { // 写入每一列
Label label = new Label(i, rowNum, (String) col.get(i));
sheet.addCell(label);
}
}
public static void main(String[] args) throws SQLException {
String DRIVER = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3307/zy_video";//根据自己的数据库设置路径和用户名密码
String USERNAME = "bin";
String USERPASSWORD = "123456";
String sql = "select * from u"; // 根据自己的要求书写sql语句
Vector<String> columnName = new Vector<String>(); // 列名
columnName.add("ID");
columnName.add("UM");
columnName.add("SEX");
// 连接数据库
ResultSet rs ;
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL,USERNAME,USERPASSWORD);
PreparedStatement ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
// 导出文件的路径和工作簿名称都可根据自己的需求修改
new DBtoExcel().WriteExcel(rs, "D:/book.xls", "访问记录", columnName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.zhiyou100.video.utils;
import java.io.File;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/**
作者:于留俊
QQ:1099887856
**/
public class TestExcelToDb {
public static void main(String[] args) throws Exception {
String DRIVER = "com.mysql.jdbc.Driver";
String URL = "jdbc:mysql://localhost:3307/zy_video";//根据自己的数据库设置路径和用户名密码
String USERNAME = "bin";
String USERPASSWORD = "123456";
int i;
Sheet sheet;
Workbook book;
Cell cell1,cell2,cell3;
try {
//hello.xls为要读取的excel文件名
book= Workbook.getWorkbook(new File("D://book.xls"));
//获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
sheet=book.getSheet(0);
//获取左上角的单元格
cell1=sheet.getCell(0,0);
System.out.println("标题:"+cell1.getContents());
i=1;
while(true)
{
//获取每一行的单元格
cell1=sheet.getCell(0,i);//(列,行)
cell2=sheet.getCell(1,i);
cell3=sheet.getCell(2,i);
if("".equals(cell1.getContents())==true) //如果读取的数据为空
break;
System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents()+"\t");
// 连接数据库
boolean rs;
try {
Class.forName(DRIVER);
Connection conn;
conn = (Connection) DriverManager.getConnection(URL,USERNAME,USERPASSWORD);
String sql = "insert into u (um,sex) values('"+cell2.getContents()+"','"+cell3.getContents()+"')"; // 根据自己的要求书写sql语句
PreparedStatement ps = conn.prepareStatement(sql);
rs = ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
book.close();
}
catch(Exception e) { }
}
}