jxl.jar基础——导出数据库信息
来源:互联网 发布:意林小小姐阅读软件 编辑:程序博客网 时间:2024/05/29 17:28
这是基于JSP的,用了一个第三方包jxl.jar,下面我们基于这个包来完成导出.
首先写一个数据库连接类:
class dbcon{
private ResultSet rs;
private int numColumns;
public dbcon(String sql){
try{
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="youraccount";
//密码
String userPasswd="youpassword";
//数据库名
String dbName="test";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
rs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
numColumns=rmeta.getColumnCount();
}
catch(Exception e){
System.out.println(e);
}
}
public void setRs(ResultSet rs){
this.rs=rs;
}
public ResultSet getRs(){
return rs;
}
public void setNumColumns(int numColumns){
this.numColumns=numColumns;
}
public int getNumColumns(){
return numColumns;
}
}
上面这个类完成取出数据的工作,接着我们要将数据写入到Excel中去:
public class writeExcel{
public writeExcel(OutputStream os,String sql) throws Exception {
dbcon db=new dbcon(sql);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("TestSheet1", 0);
int j=0;
Label labelC;
while(db.getRs().next()){
for(int i=0;i<=db.getNumColumns()-1;i++){
labelC = new Label(i, j, db.getRs().getString(i+1));
ws.addCell(labelC);
}
j++;
}
wwb.write();
wwb.close();
}
}
再写个mian方法测试一下:
public static void main(String[] args)throws Exception{
File f=new File("kk.xls");
f.createNewFile();
new writeExcel(new FileOutputStream(f),"select * from test where name like '%z%'");
}
首先写一个数据库连接类:
class dbcon{
private ResultSet rs;
private int numColumns;
public dbcon(String sql){
try{
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="youraccount";
//密码
String userPasswd="youpassword";
//数据库名
String dbName="test";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
rs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
numColumns=rmeta.getColumnCount();
}
catch(Exception e){
System.out.println(e);
}
}
public void setRs(ResultSet rs){
this.rs=rs;
}
public ResultSet getRs(){
return rs;
}
public void setNumColumns(int numColumns){
this.numColumns=numColumns;
}
public int getNumColumns(){
return numColumns;
}
}
上面这个类完成取出数据的工作,接着我们要将数据写入到Excel中去:
public class writeExcel{
public writeExcel(OutputStream os,String sql) throws Exception {
dbcon db=new dbcon(sql);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("TestSheet1", 0);
int j=0;
Label labelC;
while(db.getRs().next()){
for(int i=0;i<=db.getNumColumns()-1;i++){
labelC = new Label(i, j, db.getRs().getString(i+1));
ws.addCell(labelC);
}
j++;
}
wwb.write();
wwb.close();
}
}
再写个mian方法测试一下:
public static void main(String[] args)throws Exception{
File f=new File("kk.xls");
f.createNewFile();
new writeExcel(new FileOutputStream(f),"select * from test where name like '%z%'");
}
- jxl.jar基础——导出数据库信息
- jxl.jar基础——JXL中的方法介绍
- jxl.jar包基础——简介
- 导出excel jxl.jar
- EXCEL导出—JXL
- 从数据库导出数据到Excel表(jxl.jar)
- java导入导出excel操作(jxl)jxl.jar 包
- Android 数据导出之Excle jxl.jar
- JTable 导出 Excel 利用 jxl.jar
- Android 数据导出之Excle jxl.jar
- 使用JXL数据库导出至Excel表格
- JXL导入导出Excel到数据库
- java语言基础(50)——Eclipse导入和导出jar包,制作jar包说明书
- jxl.jar
- jxl.jar
- Java 使用jxl.jar 导入导出Excel数据
- 利用jxl.jar导出excel表格的用法和源代码
- 使用apache 提供的jxl.jar 导出excel
- JDBC对数据库的事务操作
- 实现透明水印
- 怎样防止浏览器刷新重复提交数据?
- SQL2005中大数据量检索的分页
- struts2-准备开发环境
- jxl.jar基础——导出数据库信息
- 点一下Jtable中的行,数据自动的按顺序添加到指定的textarea中
- IDE中debug 输出
- ASP.NET Post页面及验证视图状态MAC失败问题的解决办法
- safari 3.1 GDI模式渲染解决字体发虚
- 为没有源码的程序集添加强名称
- href定位
- Wince在进程控制程序里的解决办法 error LNK2019错误解决办法
- BDC学习笔记