簡體   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.

這是 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.

這是我的 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 {...
                }

我正在使用 CSSTableGenerator 和迭代,但在 excel 中我不會得到預期的結果,或者我沒有在 excel 文件中得到我想要的表格,請幫助我......提前致謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM