public void exportSchEx(List<DutySchedule> list,String mon){
SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd");
File f = new File("E:/值班列表.xls");
OutputStream  os = null;
WritableFont font1=new WritableFont(WritableFont.createFont("宋体"),16,WritableFont.BOLD); 
WritableFont font2=new WritableFont(WritableFont.createFont("宋体"),10); 
WritableFont font3=new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.BOLD); 
WritableCellFormat format1=new WritableCellFormat(font1);
WritableCellFormat format2=new WritableCellFormat(font2);
WritableCellFormat format3=new WritableCellFormat(font3);
try {
font1.setColour(Colour.RED);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
format2.setAlignment(jxl.format.Alignment.CENTRE);
format2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
format3.setAlignment(jxl.format.Alignment.CENTRE);
format3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
} catch (WriteException e3) {
e3.printStackTrace();
} try {
os = new FileOutputStream(f);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
        WritableWorkbook wwb = null;        try {
            wwb = Workbook.createWorkbook(os);
        } catch (Exception e) {
            e.printStackTrace();
        }
        WritableSheet ws = wwb.createSheet("dutySchedule", 0);// 创建sheet        try {
         Label l = new Label(2, 0, mon+"月的排班列表",format1);
         ws.addCell(l);
            l = new Label(0, 1, "值班日期",format3);
            ws.addCell(l);
            l = new Label(1, 1, "cdn白班",format3);
            ws.addCell(l);
            l = new Label(2, 1, "cdn晚班",format3);
            ws.addCell(l);
            l = new Label(3, 1, "web白班",format3);
            ws.addCell(l);
            l = new Label(4, 1, "web晚班",format3);
            ws.addCell(l);
            int i = 2;
            for (Object s : list) {
             DutySchedule ds = (DutySchedule)s;
                l = new Label(0, i, ft.format(ds.getDutyTime()).toString(),format2);
                ws.addCell(l);
                l = new Label(1, i, ds.getCdnDay(),format2);
                ws.addCell(l);
                l = new Label(2, i, ds.getCdnNight(),format2);
                ws.addCell(l);
                l = new Label(3, i, ds.getWebDay(),format2);
                ws.addCell(l);
                l = new Label(4, i, ds.getWebNight(),format2);
                ws.addCell(l);
                i++;
            }
            ws.setColumnView(0, 20);// 设置列宽
            ws.setColumnView(1, 20);
            ws.setColumnView(2, 20);
            ws.setColumnView(3, 20);
            ws.setColumnView(4, 20);
            ws.setRowView(0, 500);// 设置行高
            ws.setRowView(1, 500);        } catch (jxl.write.biff.RowsExceededException e1) {
            e1.printStackTrace();
        } catch (jxl.write.WriteException e1) {
            e1.printStackTrace();
        }        // 输出流
        try {
            wwb.write();
        } catch (IOException ex) {            ex.printStackTrace();
        }
        // 关闭流
        try {
            wwb.close();
            os.close();
        } catch (Exception ex) {            ex.printStackTrace();
        }
}
怎样也让客户端也下载到E盘了?

解决方案 »

  1.   


    /**
     * <p>
     * 这个静态的方法用来从数据库中读取数据,然后将取到的数据以Excel格式写入到你所提供的输出流中。
     * </p>
     * <p>
     * 这个输出流需要你从外部关闭。
     * 
     * @param connection -
     *            数据库连接对象
     * @param sqlString -
     *            要执行的sql语句
     * @param outputStream -
     *            要写入的输出流
     * @param titleNames -
     *            Excel文件中每个sheet里列头的名称,你指定的列头必须要和sql语句查询出的结果一一对应
     * @throws IOException -
     *             进行文件操作的时候有可能会发生的异常
     * @throws SQLException -
     *             在已经关闭或者是不存在的连接上进行操作的时候抛出,也可能是你传入的sql语句不正确
     * @throws WriteException -
     *             在创建Excel文件结构的时候可能会抛出这个异常
     */
    public static void outputExcelFromDataBase(Connection connection,
    String sqlString, OutputStream outputStream, String titleNames[])
    throws IOException, SQLException, WriteException {
    // 从数据库中查询数据
    Statement st = connection.createStatement();
    ResultSet rs = st.executeQuery(sqlString);
    ResultSetMetaData rsmd = rs.getMetaData(); WritableWorkbook workBook = Workbook.createWorkbook(outputStream);
    // 创建sheet
    WritableSheet sheet = workBook.createSheet("shet1", 0);
    int columCount = 0;
    columCount = rsmd.getColumnCount();
    // 做出列头
    for (int i = 0; i < titleNames.length; i++) {
    // 做出列头
    Label lable = new Label(i, 0, titleNames[i]);
    sheet.addCell(lable);
    } // 行号,从1开始
    int row = 1;
    // 开始数据填充
    while (rs.next()) {
    for (int colum = 0; colum < columCount; colum++) {
    Label lable = new Label(colum, row, rs.getString(colum + 1));
    sheet.addCell(lable);
    }
    row++;
    if (row % 1000 == 0) {
    workBook.write();
    }
    }
    workBook.write();
    workBook.close();
    }你就直接使用public static void outputExcelFromDataBase(Connection connection,
    String sqlString, OutputStream outputStream, boolean hasTitle)这个方法,然后把response.getOutPutStream()作为outputStream就可以了。
    只能写10000个字,完整的代码弄不上来,你将就着改改用吧。
      

  2.   

    action里面这样写:String fileName = "人员信息统计表(" + format.format(new Date()) + ").xls";
    try {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.addHeader("Content-disposition", "attachment; filename="
    + fileName);
    OutputStream out = null;
    try {
    out = response.getOutputStream();
                            //在这里调用我刚刚给你的那个方法
    out.flush();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return null;
      

  3.   

    在问一个问题,response怎么获得的?
      

  4.   

    .....  httpresponse对象或者是HttpServletResponse
      

  5.   

    response.reset();
    能告诉我问什么我每次执行到它就会抛异常?InvocationTargetException
      

  6.   

    把你的代码贴出来,你是不是在action里面写的哦