Home  >  Article  >  php教程  >  How to implement EasyUI combined with JS to export Excel files

How to implement EasyUI combined with JS to export Excel files

高洛峰
高洛峰Original
2016-12-07 11:14:331670browse

I’ll stop talking nonsense and get straight to the point! ! It is rare that simple JS can export Excel. Generally, it is necessary to call the Office Excel component installed on the client to complete this work. Here I mainly talk about how DataGrid in EasyUI combines JS to export Excel files

1. The core code segment for exporting Excel is as follows

function Exproter() {
      //获取Datagride的列
      var rows = $('#test').datagrid('getRows');
      var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
      var oWB = oXL.Workbooks.Add(); //获取workbook对象
      var oSheet = oWB.ActiveSheet; //激活当前sheet
      for (var i = 0; i < rows.length; i++) {
        oSheet.Cells(i + 1, 1).value = rows[i].O_NAME;
      }
      oXL.Visible = true; //设置excel可见属性
}

2. The main prerequisite for the smooth execution of the above JS method is

1. Excel has been installed on the machine.

2. Internet Options => Security => Internet

" Initialize and run scripts for ActiveX controls that are not marked as safe, set to enable "

//EasyUI datagrid 动态导出Excel
function ExporterExcel() {
      //获取Datagride的列
      var rows = $(&#39;#tt&#39;).datagrid(&#39;getRows&#39;);
      var columns = $("#tt").datagrid("options").columns[0];
      var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
      var oWB = oXL.Workbooks.Add(); //获取workbook对象
      var oSheet = oWB.ActiveSheet; //激活当前sheet
      //设置工作薄名称
      oSheet.name = "导出Excel报表";
      //设置表头
      for (var i = 0; i < columns.length; i++) {
        oSheet.Cells(1, i+1).value = columns[i].title;
      }
      //设置内容部分
      for (var i = 0; i < rows.length; i++) {
        //动态获取每一行每一列的数据值
        for (var j = 0; j < columns.length; j++) {       
          oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];
        } 
      }      
      oXL.Visible = true; //设置excel可见属性
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn