We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

ActionResult ExportToExcel

Thread ID:





129318 Mar 12,2017 03:42 AM Mar 14,2017 01:47 AM ASP.NET MVC 6
Tags: XlsIO
Bernard Jurlina
Asked On March 12, 2017 03:42 AM

I upgraded from syncfusion version 14 to the latest version and now my code for creating and saving excel document won't work.

Extension.zip are my ExcelResult.cs and XlslOExtension.cs files.
Images.zip are the screenshots from F12 of Network and Console in IE.

Action in my controller:

public ActionResult ExportToExcel(DateTime date1, DateTime date2)
            var DataSource = db.recognitions.Include(c => c.CaptureSource).Where(w => w.date >= date1 && w.date <= date2).OrderByDescending(r => r.date).ToList();
            ExcelEngine excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;
            IWorkbook workbook = application.Workbooks.Create(1);
            workbook.Version = ExcelVersion.Excel2007;
            IWorksheet sheet = workbook.Worksheets[0];
            sheet.FirstVisibleRow = 0;

            sheet.Range["A1:G1"].CellStyle.Font.Bold = true;
            sheet.Range["A1:G1"].Text = "Pregled očitanja";
            sheet.Range["A1:G1"].HorizontalAlignment = ExcelHAlign.HAlignCenter;


            sheet.Range["A3"].Text = "Capture source";
            sheet.Range["B3"].Text = "Date";
            sheet.Range["C3"].Text = "Time";
            sheet.Range["D3"].Text = "Plate text";
            sheet.Range["E3"].Text = "Country assumed";
            sheet.Range["F3"].Text = "Confidence level";
            sheet.Range["G3"].Text = "Plate image";

            int row = 4;
            foreach (var item in DataSource)
                sheet.Range[row, 1].Text = item.CaptureSource.name;
                sheet.Range[row, 2].Text = item.date.ToShortDateString();
                sheet.Range[row, 3].Text = item.date.ToLongTimeString();
                sheet.Range[row, 4].Text = item.plate_text;
                sheet.Range[row, 5].Text = item.country_assumed;
                sheet.Range[row, 6].Text = item.confidence_level;

                IPictureShape pic = sheet.Pictures.AddPicture(row, 7, item.plate_image_url);
                sheet.Rows[row - 1].RowHeight = pic.Height;
                sheet.Rows[row - 1].HorizontalAlignment = ExcelHAlign.HAlignCenter;
                sheet.Rows[row - 1].VerticalAlignment = ExcelVAlign.VAlignCenter;

                sheet.Range[row, 7].ColumnWidth = 25;

            sheet.Columns[6].ColumnWidth = 25;

                return excelEngine.SaveAsActionResult(workbook, "SpreadSheet.xls", HttpContext.ApplicationInstance.Response, ExcelDownloadType.PromptDialog, ExcelHttpContentType.Excel2007);
            catch (Exception)
            { }

            return View("Index");


Attachment: images_8128da10.zip

Bernard Jurlina
Replied On March 12, 2017 03:46 AM

I forgot to add ajax call:

                url: '@Url.Action("ExportToExcel", "AnprSettings")',
                data: { date1: date1.toISOString(), date2: date2.toISOString() },
                type: 'GET',
                success: function (result) {
                    //do the necessary updations
                error: function (result) {

Attachment: Extension_681972dd.zip

Bernard Jurlina
Replied On March 12, 2017 08:38 AM

Internet explorer prompts for download when I open the request URL in the new IE tab

This is the URL:

Bernard Jurlina
Replied On March 12, 2017 07:14 PM

So, there is something wrong with ajax call, that's for sure.
How can I call ExportToExcel action on button click with ajax and to get prompt dialog for file download?
It's working fine when I open the action from address bar.


Abirami Varadharajan [Syncfusion]
Replied On March 13, 2017 08:49 AM

Hi Bernard, 
Excel file could not be downloaded directly in client machine using Ajax call. However, this can be achieved by an workaround by saving the Excel document in the disk and downloading it. Please find the below code sample to download the Excel file. 
Code Example: 
string fullPath = Path.Combine(Server.MapPath("~/temp"), file); 
byte[] fileByteArray = System.IO.File.ReadAllBytes(fullPath); 
return File(fileByteArray, "application/vnd.ms-excel", file); 
We have prepared the sample for the same. The sample can be downloaded from following link. 
Please let us know if you have any concerns. 

Bernard Jurlina
Replied On March 13, 2017 11:16 AM

Hi Abirami,

thanks for the information and for the example. I'll try it right away.


Abirami Varadharajan [Syncfusion]
Replied On March 14, 2017 01:47 AM

Hi Bernard, 
Thank you updating us. 
Please let us know if you need any further assistance. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.