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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

ActionResult ExportToExcel

Thread ID:

Created:

Updated:

Platform:

Replies:

129318 Mar 12,2017 07:42 AM UTC Mar 14,2017 05:47 AM UTC ASP.NET MVC 6
loading
Tags: XlsIO
Bernard Jurlina
Asked On March 12, 2017 07:42 AM UTC

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"].Merge();
            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["A2:G2"].Merge();

            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;

                row++;
            }
            sheet.UsedRange.AutofitColumns();
            sheet.Columns[6].ColumnWidth = 25;

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

            workbook.Close();
            excelEngine.Dispose();
            return View("Index");
        }

Thanks!

Attachment: images_8128da10.zip

Bernard Jurlina
Replied On March 12, 2017 07:46 AM UTC

I forgot to add ajax call:

$.ajax({
                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 12:38 PM UTC

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

This is the URL:
http://localhost:52840/AnprSettings/ExportToExcel?date1=2017-02-28T23%3A00%3A00.000Z&date2=2017-03-12T23%3A00%3A00.000Z



Bernard Jurlina
Replied On March 12, 2017 11:14 PM UTC

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.

Thanks!

Abirami Varadharajan [Syncfusion]
Replied On March 13, 2017 12:49 PM UTC

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); 
System.IO.File.Delete(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. 
 
Regards, 
Abirami. 


Bernard Jurlina
Replied On March 13, 2017 03:16 PM UTC

Hi Abirami,

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

Regards,
Bernard.

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

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


CONFIRMATION

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.

Warning Icon 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.Close Icon

;