2

I am trying to export data to excel using closedxml api but getting virus detected error on export

My code is below

[HttpGet]
        public ActionResult ExportToExcelNew()
        {
            string sheetName = "Report";
            var userClaims = DbAccessWebApiHandler.GetUserClaimAccesshistory(new DateSelection
            {
                StartDate = DateTime.Now.AddDays(-60),
                EndDate = DateTime.Now
            });

            if (userClaims != null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("UserName");
                dt.Columns.Add("Email");
                dt.Columns.Add("Claim");
                dt.Columns.Add("AccessedOn");


               foreach (var item in userClaims)
               {
                    dt.Rows.Add(item.UserName, item.Email, item.Claim, item.AccessedOn);
                }
                XLWorkbook wb = new XLWorkbook(XLEventTracking.Disabled);
                var ws = wb.Worksheets.Add(sheetName);
                ws.Cell(2, 1).InsertTable(dt);
                using (var stream = new MemoryStream())
                {
                    wb.SaveAs(stream);
                    stream.Flush();
                   return new FileContentResult(stream.ToArray(),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                    {
                        FileDownloadName = "Report.xlsx"
                    };
                }
            }

            return View("Index");
        }

While other simple sort of export works which code looks like

[HttpGet]
        public ActionResult ExportToExcel(DateTime? fromDate, DateTime? toDate)
        {
            if (!fromDate.HasValue) fromDate = DateTime.Now.Date;
            if (!toDate.HasValue) toDate = fromDate.GetValueOrDefault(DateTime.Now.Date).Date.AddDays(1);
            if (toDate < fromDate) toDate = fromDate.GetValueOrDefault(DateTime.Now.Date).Date.AddDays(1);
            ViewBag.fromDate = fromDate;
            ViewBag.toDate = toDate;

            var userClaims = DbAccessWebApiHandler.GetUserClaimAccesshistory(new DateSelection
            {
                StartDate = fromDate.Value,
                EndDate = toDate.Value
            });


            if (userClaims != null)
            {
                var gv = new GridView();

                gv.AutoGenerateColumns = false;
                gv.Columns.Add(new BoundField { HeaderText = "UserName", DataField = "UserName" });
                gv.Columns.Add(new BoundField { HeaderText = "Email", DataField = "Email" });
                gv.Columns.Add(new BoundField { HeaderText = "Accessed", DataField = "Claim" });
                gv.Columns.Add(new BoundField { HeaderText = "Date", DataField = "AccessedOn" });

                var dt = new DataTable();
                dt.Columns.Add("UserName");
                dt.Columns.Add("Email");
                dt.Columns.Add("Claim");
                dt.Columns.Add("AccessedOn");


                foreach (var item in userClaims)
                {
                    dt.Rows.Add(item.UserName, item.Email, item.Claim, item.AccessedOn);
                }

                gv.DataSource = dt;
                gv.DataBind();

                for (var i = 0; i < userClaims.Count; i++)
                {
                    gv.Rows[i].Height = 40;
                }

                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment; filename=Reports.xls");
                Response.ContentType = "application/ms-excel";
                Response.Charset = "";
                var sw = new StringWriter();
                var htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }


            return View("Index");
        }

I have Windows 8.1 OS

Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • After a short review of your code, I cannot find anything suspicious. We would need to learn more about your Virus Warning. Where is it coming from? Also, I'd comment out everything from the code and just return the empty `XLWorkbook` and see if the error still occurs. If yes, it's not the content of the file, maybe look for issue somewhere else? If no, try to find the piece of code causing the issue, by commenting in again small working parts of code. – thmshd Oct 02 '17 at 06:29
  • Thanks thmshd. I was also surpised with the error.Posted question here to find out if anybody else has also experienced it. Will try it on another system and check. – Kamran Shahid Oct 02 '17 at 11:31
  • Possible duplicate of [Asp.net MVC 5 closedxml export to excel. getting virus error](https://stackoverflow.com/questions/46504789/asp-net-mvc-5-closedxml-export-to-excel-getting-virus-error) – Francois Botha Oct 02 '17 at 12:17
  • yes. i deleted other post – Kamran Shahid Oct 02 '17 at 12:30
  • Strange thing is that i didn't get the error on my office development machine which has windows 10 installed. Even i have Antivirus installed there – Kamran Shahid Oct 02 '17 at 18:59
  • I thought it was a false alarm anyway, but you want to get rid of it. Theoretically, it's possible to randomly create a code signature close to an Excel Virus. – thmshd Oct 03 '17 at 12:42
  • I have tried MAC, Windows 7, windows 8 and windows 10 at my office. it works their. May be their seem some problem in home system – Kamran Shahid Oct 03 '17 at 18:39
  • I have just seen an issue with this as well. Interestingly, when AV is turrned off, we can see in a stack trace the following: `"Requested registry access is not allowed.","ExceptionType":"System.Security.SecurityException","StackTrace":" at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)\r\n at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)\r\n at Microsoft.Win32.Registry.GetValue(String keyName, String valueName, Object defaultValue)\r\n at MS.Utility.EventTrace.IsClassicETWRegistryEnabled()\r\n at MS.Utility.EventTrace..cctor()"}}` – jktravis Oct 18 '17 at 20:07
  • Very Strange. Any further finding? how can we fix it. Export to excel using the closedxml result in one of the finest output.I wanted to let it use as it is – Kamran Shahid Oct 19 '17 at 06:37

0 Answers0