search
Homephp教程PHP开发How to implement EasyUI combined with JS to export Excel files

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),