简体   繁体   English

如何使用AJAX调用和JavaScript将数据从数据库导出到Excel?

[英]How to export data from database into Excel using AJAX call & JavaScript?

I have listbox containing some Farmers Ids,i want to to get the details (phone number, address, etc) from the database and export it to Excel file, I thought of using AJAX call to get the data from the database, but how can I export these data to Excel file? 我有一个包含一些Farmers ID的listbox ,我想从数据库中获取详细信息(电话号码,地址等)并将其导出到Excel文件,我想到了使用AJAX调用从数据库中获取数据,但是如何我将这些数据导出到Excel文件吗?

JavaScript code: JavaScript代码:

function ExportData()
{
    var ul = document.getElementById("FarmersID");
    var items = ul.getElementsByTagName("option");
    var farmers=[];

    if (items.length == 0)
    {
        alert ("No Farmers Found!");
        return false;
    }
    else
    {
        for (var i = 0; i < items.length; ++i) 
        {
            farmers.push(items[i].text);
        }
        var jsonString = JSON.stringify(farmers);

        $.ajax({
            type:"POST",
            url:"get_farmerdata.php",
            data:
                { 
                   'FarmerData':jsonString
                },
            success: function (response) 
                {
                    alert(response);
                },
            error: function(jqXHR, textStatus, errorThrown) {
            console.log('ERROR', textStatus, errorThrown);
            }
       })
   }
}

PHP code: PHP代码:

<html>
<?php
    include_once "connect.php";

    $FarmerData=json_decode($_POST['FarmerData'],true);
    $ids = join("','",$FarmerData);   

    $stmt ="SELECT * FROM Farmers where Farm_id IN ('$ids')";

    foreach ($conn->query($stmt) as $row) 
    {
        echo $row['nick_name_']."<br>";
    }   
?>
</html>

You could use table2excel jquery plugin. 您可以使用table2excel jquery插件。

In your PHP code, just echo the database data that you execute in a simple HTML table. 在您的PHP代码中,只需在简单的HTML表中回显您执行的数据库数据即可。

After that in your jquery, append the response table from the php to your page, and use the table2excel plugin to export it: 之后,在您的jquery中,将响应表从php附加到您的页面,并使用table2excel插件将其导出:

success: function (response) 
{
   $('#YourTableDiv').append(response);

   $('#YourTableDiv').table2excel({
      exclude: ".excludeThisClass",
      name: "Worksheet Name",
      filename: "SomeFile" //do not include extension
      fileext: ".xls", // MUST use .xls extension, only this will work
   }); 
},

This is my example code in jsfiddle that I create: https://jsfiddle.net/bj3djhbr/ 这是我在jsfiddle中创建的示例代码: https ://jsfiddle.net/bj3djhbr/

Note, in this fiddle I'm using old version of table2excel, so it will download the file in .xlxs, which is unfortunately cannot be opened (known issue), so just rename it to .xls. 请注意,在这个小提琴中,我使用的是旧版本的table2excel,因此它将以.xlxs格式下载文件,但很遗憾,该文件无法打开(已知问题),因此只需将其重命名为.xls。 But if you use the latest version, on the github link that I give you, it should work fine. 但是,如果您使用最新版本,请在我给您的github链接上正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM