0

我预计它将有一个提示屏幕来要求用户打开或保存或取消。

从我的代码下方,我尝试使用 FileOutputStream 将文件保存到特定位置,但是我怎样才能执行屏幕截图下方的操作?

预期截图: 在此处输入图像描述

下面是我的代码:

try {
    /* Create Workbook and Worksheet XLSX Format */
    XSSFWorkbook my_workbook = new XSSFWorkbook();
    XSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
    /* Get access to XSSFCellStyle */
    XSSFCellStyle my_style = my_workbook.createCellStyle();

    /* We will now specify a background cell color */
    my_style.setFillPattern(XSSFCellStyle.FINE_DOTS);
    my_style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    my_style.setFillBackgroundColor(IndexedColors.RED.getIndex());

    /* Create a row in the sheet */
    Row row = my_sheet.createRow(0);
    /* Create a cell */
    Cell cell = row.createCell(0);
    cell.setCellValue("Cell Fill Color Test");
    /* Attach the style to the cell */
    cell.setCellStyle(my_style);

    /* Write changes to the workbook */
    // OutputStream out = new FileOutputStream(
    // new File(ExcelConstant.TEST));
    // my_workbook.write(out);
    // out.close();

    OutputStream output = response.getOutputStream();
    // response.setContentType("application/x-download");
    // response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=Excel.xlsx");
    my_workbook.write(output);
    output.close();
}
4

2 回答 2

1

我为我自己的问题找到了一个解决方案,即不允许 Ajax 下载文件。

来自好友的参考:为什么无法使用ajax请求下载文件?

原因是我在我的 extjs 文件中调用了 ajax。海兹...

于 2013-11-12T14:30:34.780 回答
0

您的问题的替代解决方案。

使用 ajax 调用,您可以将文件保存到磁盘。只有你可以有一个解决方法才能记住它。

post2Url("./ExportToExceljQgrid.do?, 
        { 
            "getRowData":JSON.stringify($("#treegrid").jqGrid('getRowData')),
            "colNames" : JSON.stringify($("#treegrid").jqGrid('getGridParam','colNames')),
            "groupHeader" : JSON.stringify($("#treegrid").jqGrid("getGridParam", "groupHeader").groupHeaders)
        }
        , 'post');

function post2Url(path, params, method) {
    method = method || "post"; 
    var postForm = document.createElement("form");
    postForm.name = "exportForm";
    postForm.method = method;
    postForm.action = path;
    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);
            postForm.appendChild(hiddenField);
         }
    }
    document.body.appendChild(postForm);
    postForm.submit();
    document.body.removeChild(postForm);
}

您将所有 json 数据作为请求中的参数

于 2014-04-23T08:52:20.250 回答