excel template report to PDF Download

Recently there is a demand, the need to excel PDF report templates; you need to generate a template based on excel excel file, and then generate a pdf file excel, and finally download function;

Generate excel reports

Use easypoicomponent, the template portion may excel in detail with reference to the document;

Usage Overview

The advantage of this approach is exactly the same style and template styles can be achieved with a single-valued attribute list traversal;

  • Modify cell contents in excel template for the corresponding easypoisupport of expression;
  • Define TemplateParam(template attributes), and data attribute;
  • Call the static method:ExcelExportUtil.exportExcel

excel generate PDF reports

Implementation of species

IText

PDF Generation pure JAVA implementation, cross-platform, disadvantages are also obvious: API more difficult to use, but difficult to handle Excel style;

OpenOffice
  • Import jar package: com.artofsolving-jodconverter

  • openOffice listening port:soffice.exe -headless -accept="socket,host=%s,port=%s;urp;" -nofirststartwizard

  • Convert to PDF using openOffice
            //连接
            var conn = new SocketOpenOfficeConnection(hostname,port);
            conn.connect();
            //转换
            converter = new OpenOfficeDocumentConverter(conn);
            converter.convert(docFile,pdfFile);
            //关闭连接
            conn.disconnect();

Advantages: cross-platform;
Disadvantages: openOffice converted into PDF distortion to some extent, such as automatic border excel bold and so on, some of the style does not support, but the only option on the linux server.

windowOffice or wps

  • Import jar package: jacob-1.19.zip

  • Installation windowOffice or WPS;

  • Conversion:
            //office命令
             ActiveXComponent app = new ActiveXComponent("Excel.Application");
            //Wps方式
            app = new ActiveXComponent("KET.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            excel = Dispatch.call(excels, "Open", path, false, true).toDispatch();
            Dispatch.call(excel, "ExportAsFixedFormat", 0, pdfAbsPath);

Pros: Perfect style;
Cons: only supports window server;

file download

Download the back-end code base

response.setCharacterEncoding(CharsetUtil.UTF_8);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","attachment; filename=" + fileName);
        response.setContentLengthLong(pdfFile.length());

Precautions

  • Different browsers support different file name encoding formats, most can be encoded using UrlEnocde, safari browser needs a string encoding ISO8859-1

Front-end processing

Front-end js download blob, and create the blob Download
config.responseType = "blob";
let blob = new Blob([response.data],{type: 'application/pdf'}); //创建一个blob对象
let downloadName = response.headers["content-disposition"].split(";")[1].split("filename=")[1];
let a = document.createElement('a');
let url = URL.createObjectURL(blob)
a.href = url; // response is a blob
a.download = downloadName;  //文件名称
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();

Disadvantages: Download the file appears in the safari named: known exception;

The use of third-party components

This method is a method on an extension, do not create themselves a label, use the file-sever components, you only need to pass the blob;

 FileSaver.saveAs(blob, downloadName);

Disadvantages: Download the file appears in the safari named: known exception;

The front end of the back-end to create a real Download

let a = document.createElement('a');
a.href = url; // 直接为后端的url;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();

Advantages: Theoretically all browsers can correctly identify the file name information;
disadvantages: because it is not requested by the backend interface ajax backend separation system to be treated separately in the previous verify what permissions (e.g., token, etc.);

Guess you like

Origin www.cnblogs.com/417xiaoliu/p/11039102.html