jxl.jar 导出excel

 <%@ page pageEncoding="utf-8" %>
<%@ page language="java" contentType="text/html; charset=GBK" %><%@ page import="java.util.*" %>
<%@ page import="java.io.OutputStream" %>

<%@ page import="jxl.Workbook" %>
<%@ page import="jxl.write.WritableCellFormat" %>
<%@ page import="jxl.write.WritableSheet" %>
<%@ page import="jxl.write.WritableWorkbook" %>
<%@ page import="jxl.write.WriteException" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%  
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "filename=Member.xls");//attachment// WritableWorkbook是JexcelApi的一个类。
// 以下可以理解为创建一个excel文件,然后在excel里面创建一个表
    OutputStream os = response.getOutputStream();//取得输出流
    WritableWorkbook workbook = Workbook.createWorkbook(os);
    WritableSheet sheet = workbook.createSheet("First Sheet", 0);// 组织excel文件的内容
    jxl.write.Label label = null;
    int excelCol = 0;
    int row = 0;
    try {
        label = new jxl.write.Label(excelCol++, row, "帐号");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "姓名");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "身份证");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "联系电话");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "生日");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "城市");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "职业");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "已购音箱型号");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "试听与否");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol++, row, "邮箱");
        sheet.addCell(label);
        label = new jxl.write.Label(excelCol, row, "注册时间");
        sheet.addCell(label);
        //jxl.write.Number number = null;
        jxl.write.DateTime dateTime;
        jxl.write.DateFormat customDateFormat = new jxl.write.DateFormat("yyyy-MM-dd");//时间格式
        WritableCellFormat dateFormat = new WritableCellFormat(customDateFormat);        for(int i=0;i<10;i++){
            excelCol = 0;
            row = i + 1;            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");     
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            /*字串格式*/
            try{
                label = new jxl.write.Label(excelCol++, row, formatDate.format("100"));
                sheet.addCell(label);
            }catch (Exception e){
                //Maybe somebody fogot to input his birthday ,I need do nothing here!
            }            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            label = new jxl.write.Label(excelCol++, row, "100");
            sheet.addCell(label);            /*时间格式*/
         
                    //label = new jxl.write.Label(excelCol, row, formatDate.format(rr.getCreateTime()));
            //sheet.addCell(label);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
//      生成excel文件
        workbook.write();
        workbook.close();
        os.close();
    }
%>
  

posted @ 2012-08-23 21:24  Brook Legend  阅读(360)  评论(0编辑  收藏  举报