jQuery 使用jQuery通过AJAX发送Excel数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/149821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:42:40  来源:igfitidea点击:

Use jQuery to send Excel data using AJAX

jqueryajaxexcel

提问by Jim

I have the following function that is pulling data from a database. The ajax call is working correctly. How can I send the tab delimited data in my success function to the user? Setting the contect type to "application/vnd.ms-excel" didn't work. The alert on success shows the correctly formatted data.

我有以下功能从数据库中提取数据。ajax 调用工作正常。如何将成功函数中的制表符分隔数据发送给用户?将联系类型设置为“application/vnd.ms-excel”不起作用。成功警报显示格式正确的数据。

     function SendToExcel() {
       $.ajax({
           type: "GET",
           url: "/Search.aspx",
           contentType: "application/vnd.ms-excel",
           dataType: "text",
           data: "{id: '" + "asdf" + "'}",
           success: function(data) {
              alert(data);
           },
           error: function (jqXHR, textStatus, errorThrown) {
              alert(jqXHR.responseText);
       }});
     }

I don't want to display the data in the browser--I want to send it to Excel.

我不想在浏览器中显示数据——我想将它发送到 Excel。

EDIT:I found a way to do what I wanted. Instead of redirecting the users to a new page that would prompt them to save/open an Excel file, I opened the page inside a hidden iframe. That way, the users click a button, and they are prompted to save/open an Excel file. No page redirection. Is it Ajax? No, but it solves the real problem I had.

编辑:我找到了一种方法来做我想做的事。我没有将用户重定向到提示他们保存/打开 Excel 文件的新页面,而是在隐藏的 iframe 中打开了该页面。这样,用户单击一个按钮,就会提示他们保存/打开 Excel 文件。没有页面重定向。是阿贾克斯吗?不,但它解决了我遇到的真正问题。

Here's the function I'm calling on the button click:

这是我在单击按钮时调用的函数:

     function SendToExcel() {
        var dataString = 'type=excel' +
            '&Number=' + $('#txtNumber').val() + 
            '&Reference=' + $('#txtReference').val()

        $("#sltCTPick option").each(function (i) {
             dataString = dataString + '&Columns=' + this.value;
        });

        top.iExcelHelper.location.href = "/Reports/JobSearchResults.aspx?" + dataString;;
     }

采纳答案by Shog9

AJAX is... the wrong choice. Redirect the user to a server resource that will send the data down with the proper MIME type, and let the browser figure out what to do with it.

AJAX 是……错误的选择。将用户重定向到将使用正确的 MIME 类型向下发送数据的服务器资源,并让浏览器弄清楚如何处理它。

回答by fernando

in HTML I have a Form with serial inputs elements and a button that calls a JavaScript function onclick="exportExcel();

在 HTML 中,我有一个带有串行输入元素的表单和一个调用 JavaScript 函数的按钮 onclick="exportExcel();



然后在 JavaScript 文件中:

function exportExcel(){
    var inputs = $("#myForm").serialize();
    var url = '/ajaxresponse.php?select=exportExcel&'+inputs;
    location.href = url;
}

and finally a pivot file who response to something

最后是一个对某事做出反应的数据透视文件

PHP code:

PHP代码:

case 'exportExcel':{
                     ob_end_clean();
                     header("Content-type: application/vnd.ms-excel");
                     header("Content-Disposition: attachment;
                     filename=exportFile.xls");
                     echo $html->List($bd->ResultSet($_GET));
                }

$html is an object who handle html, and $bd is an object that returns data from Database send your own html table or whatever you want.

$html 是处理 html 的对象,$bd 是从数据库返回数据的对象,发送您自己的 html 表或任何您想要的。

回答by Randy

Since it uses JavaScript, AJAX is bound by JavaScript's designed limitations, which includes interacting with other processes on the client's machine. In this case, it's a good thing; you wouldn't want a site to be able to automatically load an Excel document with a malicious macro in it.

由于它使用 JavaScript,因此 AJAX 受到 JavaScript 设计限制的约束,其中包括与客户端机器上的其他进程交互。在这种情况下,这是一件好事;您不希望网站能够自动加载带有恶意宏的 Excel 文档。

If you want to display the data in the browser, you can use AJAX; otherwise, you'll want to just give a link to an Excel document and let the browser's regular download handling capabilities figure out what to do.

如果要在浏览器中显示数据,可以使用AJAX;否则,您只需提供一个 Excel 文档的链接,并让浏览器的常规下载处理功能找出要执行的操作。

回答by Sugendran

It's possible that you don't want to do this with javascript.

您可能不想使用 javascript 执行此操作。

What I think you want to do is create a response page with the mine type application/csvthen redirect the user to that page. I would probably do a window.open() since the user doesn't lose the page they're currently on.

我认为您想要做的是使用 mine 类型application/csv创建一个响应页面,然后将用户重定向到该页面。我可能会做一个 window.open() 因为用户不会丢失他们当前所在的页面。