Angular JS应用:保存excel文件并稍后显示

时间:2019-02-08 10:31:11

标签: javascript angularjs excel servlets clob

在我的应用程序(使用AngularJS框架开发)中,我必须在客户端(Java语言)中创建和下载Excel文件。为此,我有此代码

            var form = document.createElement('form');
            form.action = 'download';
            form.method = 'POST';
            form.style.display = 'none';

            var inputsrtoken = document.createElement('input');
            inputsrtoken.style.setAttribute("width", "0");
            inputsrtoken.style.setAttribute("diplay", "none");
            inputsrtoken.style.setAttribute("visibility", "hidden");                                
            inputsrtoken.type = 'text';
            inputsrtoken.name = 'srtoken';
            inputsrtoken.value = token;

            var inputData = document.createElement('input');
            inputData.style.setAttribute("width", "0");
            inputData.style.setAttribute("diplay", "none");
            inputData.style.setAttribute("visibility", "hidden");                               
            inputData.type = 'text';
            inputData.name = 'inputRicalcolo';
            inputData.value = JSON.stringify(excelData);

            var submit = document.createElement('input');
            submit.type = 'submit';
            submit.id = 'submitProject';

            form.appendChild(inputsrtoken);
            form.appendChild(inputData);
            form.appendChild(submit);
            document.body.appendChild(form);

            $('#submitProject').click();
            document.body.removeChild(form);

变量excelData中包含一些有关生成excel文件的有用信息。

服务器端,我有这段代码(在下载servlet中)

           if (Base64.isArrayByteBase64(excelin.getBytes("UTF-8"))) {
                excelin = new String(Base64.decodeBase64(excelin.getBytes("UTF-8")), "UTF-8");
            }

            bpdf = createExcelRicalcolo(excelin);

            String chiave = request.getParameter("chiave");
            DateFormat formatter = new SimpleDateFormat("ddMMyyyy-HHmm");
            String format = formatter.format(new Date());
            String filename = "RICALCOLO_ONLINE_"+chiave +"_"+ format + ".xls";

            byte[] b = bpdf;

            if (b != null) {
                int length = b.length;
                ServletOutputStream op = resp.getOutputStream();
                resp.setContentType("application/ms-excel");
                resp.setContentLength(length);
                resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
                resp.setHeader("Expires", "0");
                resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                resp.setHeader("Pragma", "public");

                op.write(b); 
                op.flush();
                op.close();
            }

效果很好!
但是,现在,这是我的问题:是否可以生成文件而不下载,而只能保存(可能带有javascript服务)以供后续下载请求使用?
实际上,我的目标是:我想生成一个excel文件(不向用户显示),将其作为CLOB保存在DB2列上,然后(通过按下应用程序上的另一个按钮)然后用户可以下载它。
您是否认为可以在不破坏代码逻辑的情况下进行此类操作?
将文件另存为CLOB将解决以下问题。
宽容任何愿意帮助我的人。

0 个答案:

没有答案