【问题标题】:How to add download option for openwith and saveAs on exporting file to excel in servlet如何在将文件导出到 servlet 中的 excel 时添加 openwith 和 saveAs 的下载选项
【发布时间】:2012-11-21 05:09:31
【问题描述】:

大家好!!! 我做了一个应用程序,我需要将数据库中的数据导出到 .excel 格式中,在其中我为生成的 .excel 文件提供了硬编码路径以保存在系统上的特定位置。现在根据我的要求,我需要提供下载选项用于浏览器中的 openwith 和 saveAs。 下面我发布我的代码。请各位大神帮帮我...将不胜感激。提前谢谢...

String datum1 = request.getParameter("fromdate");
        String datum2 = request.getParameter("todate");
        SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MM-yyyy");
        Date date = sdfSource.parse(datum1);
        Date date2 = sdfSource.parse(datum2);
        SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
        datum1 = sdfDestination.format(date);
        System.out.println(datum1);
        datum2 = sdfDestination.format(date2);
        System.out.println(datum2);

        String filename = "d:/".concat(datum1).concat(" ").concat("To").concat(" ").concat(datum2).concat(".xls");
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("CallBillingSystem");

        HSSFRow rowhead = sheet.createRow((short) 0);
        rowhead.createCell((short) 0).setCellValue("calldate");
        rowhead.createCell((short) 1).setCellValue("src");
        rowhead.createCell((short) 2).setCellValue("dst");

        String strQuery = "";
        ResultSet rs = null;

        conexion conexiondb = new conexion();
        conexiondb.Conectar();

        strQuery = "SELECT * FROM cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";

        // strQuery = "SELECT * FROM cdrcost where date(calldate) between '2011-09-01' and '2012-01-01'";

        rs = conexiondb.Consulta(strQuery);
        int i = 1;
        while (rs.next()) {
            HSSFRow row = sheet.createRow((short) i);
            row.createCell((short) 0).setCellValue(rs.getString("calldate"));
            row.createCell((short) 1).setCellValue(rs.getString("src"));
            row.createCell((short) 2).setCellValue(rs.getString("dst"));

            i++;
        }
        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);
        fileOut.close();
        System.out.println("Your excel file has been generated!");

    } catch (Exception ex) {
        System.out.println(ex);

    }


}

【问题讨论】:

    标签: excel servlets download export-to-excel


    【解决方案1】:

    您应该在响应标头中使用Content-Disposition

    尝试使用此代码

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition",
                     "attachment;filename=FileName.xls");
    
    OutputStream out = response.getOutputStream();
    
    FileInputStream in = new FileInputStream(my_file);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
    

    【讨论】:

    • 谢谢先生您的快速响应。但我真的不知道我需要把这段代码放在哪里,请您指导我的代码
    • 写在fileOut.close();System.out.println("Your excel file has been generated!");之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2015-02-15
    • 2017-06-24
    相关资源
    最近更新 更多