0

I expected it will have a prompt-out screen to asking user to Open or Save or Cancel.

From below my code, i try FileOutputStream is work to save the file into a specific location, but how can i do something like below my screenshot?

Expected screenshot: enter image description here

Below is my code:

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();
}
cchana
  • 4,899
  • 3
  • 33
  • 43
Kim
  • 980
  • 1
  • 15
  • 29
  • I am not sure what the problem is, your description is vague. – Mark Rotteveel Nov 08 '13 at 08:40
  • 1
    Sorry about my language issue, i tried provide more information and re-explain again my problem. Kindly advice. Thank You. – Kim Nov 08 '13 at 08:50
  • 1
    I remember something about Internet Explorer being annoying if you use the wrong content-type or content-disposition, not sure about the details though. – Mark Rotteveel Nov 08 '13 at 08:55
  • 1
    i tried to remove response.setContentType, but the problem still there. Is that possible google chrome browser blocked the prompt-out? – Kim Nov 08 '13 at 09:04
  • The content type in your code looks wrong. What happens if you set the correct content type for an Excel .xlsx file? – Gagravarr Nov 08 '13 at 10:12
  • ya. I tried modify the content type, the prompt-out screen still not function.. am i did something wrong? Thanks. Note: i was updated my modified code on the first post. – Kim Nov 08 '13 at 11:21
  • This is not a POI question. Your problem is related to a behaviour that is purely driven by the user preferences in the browser of a user. – mwhs Nov 08 '13 at 12:37

2 Answers2

1

I found an solution for my own question which is the Ajax is not allowed to download the file.

Refer from buddy: Why threre is no way to download file using ajax request?

The reason because i call ajax in my extjs file. haiz...

Community
  • 1
  • 1
Kim
  • 980
  • 1
  • 15
  • 29
0

Alternative solution to your problem.

with ajax call you can save file to disk. it remains to memory only you can have a workaround for this.

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);
}

you get all the json data as parameters in request

Arun Pratap Singh
  • 3,428
  • 30
  • 23