Excel file export, compatible with IE (web front-end and back-end export method)

Excel file export

Foreground export

The foreground export refers to exporting the data of the front page to a local file

XML files and Excel files can be converted to each other, so XML data can be imported from Web services into Excel worksheets.

Use a hidden Table to store the data that needs to be exported, build a template file (an html format file, you can set some attributes and formats), add the data in the table to the template, encrypt it with base64, and use data:application/vnd.ms- excel;base64 sends the data stream locally to complete the export function.

The foreground export is to download the static page (table containing data) to the local

template:

<html xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">

<head>
    <!--[if gte mso 9]>
    <xml>
        <x:ExcelWorkbook>
            <x:ExcelWorksheets>
                <x:ExcelWorksheet>
                    <x:Name>sheet页名称</x:Name>
                    <x:WorksheetOptions>
                        <x:DisplayGridlines />
                    </x:WorksheetOptions>
                </x:ExcelWorksheet>
            </x:ExcelWorksheets>
          </x:ExcelWorkbook>
    </xml>
    <![endif]-->
</head>

<body>
    <table>
       
    </table>
</body>

</html>

accomplish:

var worksheet = 'sheet名称'
var uri = 'data:application/vnd.ms-excel;base64,';
// 获取table元素
var strsheet = $("#table_temp").html();

// 下载的表格模板数据 
// 将table数据插入模板中
 var template = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"";
 template += "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"";
 template += "xmlns=\"http://www.w3.org/TR/REC-html40\">";
 template += "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>";
 template += "<x:Name>" + worksheet + "</x:Name>";
 template += "<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>";
 template += "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->";
 template += "</head><body><table>" + strsheet + "</table></body></html>";

//下载模板 
window.location.href = uri + base64(template);

// base64加密
function base64(str) {
    
    
      return window.btoa(unescape(encodeURIComponent(str)));
}

IE

// 导出入口
function ExportExcel(tableId) {
    
    
    IeAndGoogleExcel(tableId);
}
function getExplorer() {
    
    
    var explorer = window.navigator.userAgent;

    //ie 
    //if (explorer.indexOf("MSIE") >= 0) { ---只支持IE10以下版本
    if (!!window.ActiveXObject || "ActiveXObject" in window) {
    
     //--支持IE6-IE11
        return 'ie';
    }
    //firefox 
    else if (explorer.indexOf("Firefox") >= 0) {
    
    
        return 'Firefox';
    }
    //Chrome
    else if (explorer.indexOf("Chrome") >= 0) {
    
    
        return 'Chrome';
    }
    //Opera
    else if (explorer.indexOf("Opera") >= 0) {
    
    
        return 'Opera';
    }
    //Safari
    else if (explorer.indexOf("Safari") >= 0) {
    
    
        return 'Safari';
    }
}
function IeAndGoogleExcel(tableid) {
    
    //整个表格拷贝到EXCEL中
    if (getExplorer() == 'ie') {
    
    
        AllAreaExcel(tableid);//只支持ie的导出方式
    }
    else {
    
    
        tableToExcel(tableid)//支持非ie浏览器的导出方法
    }
}

function AllAreaExcel(tableId) {
    
    

    try {
    
    
        var curTbl = document.getElementById(tableId);
        var oXL;
        oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel  
        var oWB = oXL.Workbooks.Add(); //获取workbook对象  
        var oSheet = oWB.ActiveSheet;//激活当前sheet  
        var sel = document.body.createTextRange();
        sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中  
        try {
    
    
            sel.select(); //全选TextRange中内容  
        } catch (e1) {
    
    
            e1.description
        }
        sel.execCommand("Copy");//复制TextRange中内容  
        oSheet.Paste();//粘贴到活动的EXCEL中  
        oXL.Visible = true; //设置excel可见属性  
        // var fname = oXL.Application.GetSaveAsFilename("fileName" + ".xls", "Excel Spreadsheets (*.xls), *.xls");
        // oWB.SaveAs(fname);
        oWB.Close();
        oXL.Quit();

    } catch (e) {
    
    
        alert(e.description);
    }
}

var tableToExcel = (function () {
    
    

    var worksheet = 'sheetName'
    var uri = 'data:application/vnd.ms-excel;base64,';
    var strsheet = $("#group_info").html();
    //下载的表格模板数据 
    var template = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"";
    template += "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"";
    template += "xmlns=\"http://www.w3.org/TR/REC-html40\">";
    template += "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>";
    template += "<x:Name>" + worksheet + "</x:Name>";
    template += "<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>";
    template += "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->";
    template += "</head><body><table>" + strsheet + "</table></body></html>";
    //下载模板 
    window.location.href = uri + base64(template);
});

background export

Background export means that data is written to a temporary file in the background, and then the file is transferred to the foreground in the form of a stream.

THEN

Official website documentation: http://poi.apache.org/components/index.html

Apache POI is a free and open source Java API written in Java

Apache POI provides API to read and write documents in Microsoft Office formats, including excel, world, and ppt.

Ideas:

Call the service to obtain the data to be exported, use the API provided by POI to create a temporary file, and write the data into the file. Then pass the file to the front end in the form of a stream.

pom introduces the following dependencies:

<!--POI Office 文档的 Java 处理包-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${pioVersion}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>${pioVersion}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>${pioVersion}</version>
</dependency>

Create an excel file and put in the required data

// 新建workbook对象
Workbook wb = new HSSFWorkbook();

// 创建 sheet页对象
Sheet sheetName = wb.createSheet("sheetName");

// 创建行对象; 从第0行开始
Row row = sheetName.createRow(0);

// cell最小单元各
Cell cell = row.createCell(0);
cell.setCellValue("0,0");

row.createCell(1).setCellValue("1,0");
row.createCell(2).setCellValue("2,0");

// 使用循环可将list数据按照自定义格式写入excel文件中

// 更多设置 比如换行、样式、字体等参考文档api

// 将数据写入输出流生成文件
try  (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
    
    
    wb.write(fileOut);
}

Return the background file to the foreground in the form of a stream

Example: It can be used as a public export interface, after writing the temporary file, call the service to return to the foreground

@PostMapping("/Export")
@Override
public ResponseEntity<byte[]> cdrDetailExport(@RequestParam(name = "FILE_NAME") String fileName, HttpServletResponse response) {
    
    
    // 获取配置的文件路径
    String filePath = EnvCfgUtil.getStrValue("exportfile-path");
    try{
    
    
        File cdrFile = new File(filePath+"/"+fileName);
        if(cdrFile.exists()){
    
    
            FileInputStream inputStream = null;
            try{
    
    
                inputStream = new FileInputStream(cdrFile);
                byte[] body = StreamUtils.copyToByteArray(inputStream);
                response.setContentType("application/vnd.ms-excel;charset=gbk");
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
                headers.add("Content-Disposition", "attachment;filename=" +fileName);
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
                ResponseEntity<byte[]> rsp = new ResponseEntity(body, headers, HttpStatus.OK);
                return rsp;
            }catch (Exception e) {
    
    
                e.printStackTrace();
                log.error("文件导出失败[{}]", e.getMessage());
                throw new BusiException("10111020000040203", "文件导出失败");
            } finally {
    
    
                if (null != inputStream) {
    
    
                    try {
    
    
                        inputStream.close();
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                        log.error("文件导出失败[{}]", e.getMessage());
                        throw new BusiException("10111020000040203", "文件导出失败");
                    }
                }
                cdrFile.delete();
            }
        }
    }catch(Exception e){
    
    
        e.printStackTrace();
        log.error("文件导出失败[{}]", e.getMessage());
        throw new BusiException("10111020000040203", "文件导出失败");
    }
    return null;
}

Guess you like

Origin blog.csdn.net/weixin_40307206/article/details/119940380