package myBean;

import java.sql.*;
import java.io.*;
import jxl.*;
import jxl.write.*;
import myBean.DBConnection;

public class JXLExcel {
	private DBConnection con;
	public JXLExcel() {
		con = new DBConnection();
	}

	public void exportExcel(String name, OutputStream os) {
		try {
			WritableWorkbook wwb = Workbook.createWorkbook(os);
			//一个是工作表的名称，另一个是工作表在工作薄中的位置
			WritableSheet ws = wwb.createSheet(name, 0);
			ws.addCell(new Label(0, 1, "姓名"));
			ws.addCell(new Label(1, 1, "电子邮件"));
			
			ResultSet rs = con.execQuery("select name, email from users");
			int nRow=1;
			while(rs.next()) {
				ws.addCell(new Label(0, nRow, rs.getString("name")));
				ws.addCell(new Label(1, nRow, rs.getString("email")));
				
				nRow++;
			}
		
			wwb.write();
			wwb.close();
		} catch(Exception e) {
			System.out.println("JXLExcel.exportExcel()" + e.getMessage());
		}			
	}

/*	
	public static void main(String []args) {
		try {
		FileOutputStream o=new FileOutputStream("jxl.xls");
		new JXLExcel().exportExcel(o);
		o.close();
		}catch(Exception e) {
			System.out.println("main()" + e.getMessage());
		}
	}	
*/
}