简体   繁体   English

如何在 Jquery 中导出 Excel?

[英]How To Export Excel In Jquery?

I'm new to Jquery so what i want is when i click on button Excel Sheet is downloaded through sql query, I want code only in jquery so suggest me some piece of code or idea how to do that. I'm new to Jquery so what i want is when i click on button Excel Sheet is downloaded through sql query, I want code only in jquery so suggest me some piece of code or idea how to do that.

This is my.js file这是 my.js 文件

function Contract_Export_Excel() {
try {
    var dataString = {};

   ShowLoader();
    AjaxSubmission(dataString, '/Contract/ExportExcel', $('input[name=__RequestVerificationToken]').val()).done(function (result) {
        if (result.Message == 'success') {
            if (result.Data.Table != undefined && result.Data.Table.length != 0) {
                if (result.Data.Table[0].CurrentState == 'success') {

                    var item = result.Data.Table[0];

                            debugger;
                            var bytes = new Uint8Array(item.FileContents);
                            var blob = new Blob(bytes, { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                            var link = document.createElement('a');
                            link.href = window.URL.createObjectURL(blob);
                            link.download = "myFileName1.xls";
                            link.click();


                }
                else {...}

i think this above code is not correct suggest me some code under ajax call and im only call the controller for database access and nothing to do in controller so suggest me some code to export excel. i think this above code is not correct suggest me some code under ajax call and im only call the controller for database access and nothing to do in controller so suggest me some code to export excel.

This is my Controller这是我的 Controller

[HttpPost, SessionAuthorization, ValidateAntiForgeryHeader]
    public string ExportExcel(string formData)
    {
        try
        {
            formData = HttpUtility.HtmlDecode(formData);
            ContractAC component = new ContractAC();
            DataSet dataset = new DataSet();
            ContractMapper mapper = JsonConvert.DeserializeObject<ContractMapper>(formData);

            UserSession userSession = (UserSession)Session["userSession"];
            mapper.Company_Id = userSession.Company_Id;
            mapper.Branch_Id = userSession.Branch_Id;
            mapper.FY_Id = userSession.Financial_Year_Id;
            mapper.User_Id = userSession.User_Id;

            dataset = component.ExportExcelAC(mapper);




            return "{\"Status\":true , \"Message\":\"success\" ,\"Data\": " + JsonConvert.SerializeObject(dataset) + "}";
        }
        catch (Exception ex)
        {
            Logging.LogManager.Error("Error Details:- Controller: ContractController, Action: Get_Contracts_List, Type: Post, Error:" + ex);
            return "{\"Status\":true, \"Message\":\"" + ex.Message + "\"}";
        }
    }
function Contract_Export_Excel() {
try {
    var dataString = {};

   ShowLoader();
    AjaxSubmission(dataString, '/Contract/ExportExcel', $('input[name=__RequestVerificationToken]').val()).done(function (result) {
        if (result.Message == 'success') {
            if (result.Data.Table != undefined && result.Data.Table.length != 0) {
                if (result.Data.Table[0].CurrentState == 'success') {

                   // var item = Bind_Items(result.Data.Table1);



                    //debugger;
                           //var bytes = new Uint8Array(Bind_Items(result.Data.Table[1]));
                          /* var blob = new Blob(bytes , { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                            var link = document.createElement('a');
                            link.href = window.URL.createObjectURL(blob);
                            link.download = "myFileName1.xls";
                            link.click(); */
                    var table = $("<table/>").addClass('CSSTableGenerator');

                    $.each(result.Data.Table1, function (rowIndex, r) {
                        var row = $("<tr/>");
                        $.each(r, function (colIndex, c) {

                            row.append($("<t" + (rowIndex == 0 ? "h" : "d") + "/>").text(c));

                        });
                        table.append(row);
                    });
                   // var items = $(document.body).append(table);

                    // var bytes = new Uint8Array(items);
                    var blob = new Blob($(document.body).append(table), { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                     var link = document.createElement('a');
                     link.href = window.URL.createObjectURL(blob);
                     link.download = "myFileName1.xls";
                     link.click();


                }
                else {...
                }

I m using CSSTableGenerator and iteration but in excel i will not getting expected result or im not getting table which i want in excel file, help me for this... Thanks in advance!我正在使用 CSSTableGenerator 和迭代,但在 excel 中我不会得到预期的结果,或者我没有在 excel 文件中得到我想要的表格,请帮助我......提前致谢!

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

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