Free Support Forum - aspose.com

Excel not downloading when Aspose.cells used in WebMethod

I need to download excel using ajax call in a webmethod.
Here is my method used to download excel which working fine when called in postback event. But i need to use it in ajax call.

[WebMethod]
protected static void DownloadExcel(DataTable dtExcelData)
{
    try
    {
        if (dtExcelData.Rows.Count > 0)
        {
            Aspose.Cells.License asposeLIC = new Aspose.Cells.License();
            asposeLIC.SetLicense(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + "Aspose LIC\\Aspose.Cells.lic");

            var workbook = new Workbook();
            var worksheet = workbook.Worksheets[0];
            Aspose.Cells.Style style = worksheet.Cells["A1"].GetStyle();
            worksheet.Cells.ImportDataTable(dtExcelData, true, 0, 0, true, false);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");
            worksheet.AutoFitColumns();

            style = worksheet.Cells["A1"].GetStyle();
            style.ForegroundThemeColor = new ThemeColor(ThemeColorType.Accent1, 0);
            style.Font.Color = Color.White;
            style.Pattern = BackgroundType.Solid;


            for (int lnColumn = 0; lnColumn <= worksheet.Cells.MaxColumn; lnColumn++)
                worksheet.Cells[0, lnColumn].SetStyle(style);

            Cells cells = worksheet.Cells;
            Aspose.Cells.Style fontStyle = new Aspose.Cells.Style();
            Aspose.Cells.Style stylefont = workbook.Styles[workbook.Styles.Add()];
            stylefont.Font.Name = "Calibri";
            stylefont.Font.Size = 12;
            StyleFlag flag = new StyleFlag();
            flag.FontName = true;
            flag.FontSize = true;
            cells.ApplyStyle(stylefont, flag);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                workbook.Save(memoryStream, SaveFormat.Xlsx);
                memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.SuppressContent = true;
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
        }
        else
        {


        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

@ArunAnbu,

Please create a simple VS.NET sample project (runnable), zip it and post us here to reproduce the issue, we will check it soon.