0

i have export file from folowing code in c#

string fileName = "Census_Medical_Report_" + DateTime.Now.ToString("yyyyMMddhhmmss");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName + ".xls"));
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/ms-excel ";

            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //Build export table header structure
            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
  sb.Append("<tr><td colspan='14' align='right'>Retrieved on: " + DateTime.Now.ToString() + "</td></tr>");
            sb.Append("<tr><td colspan='14' style='font-size:14pt;color:black;font-weight:bold' align='center'>Medical, Dental, and Vision Census Report</td></tr>");
            sb.Append("<tr><td colspan='14'>&nbsp;</td></td>");
            sb.Append("<tr style='background-color:#393939;color:#ffffff'>");
            sb.Append("<td style='width:100px'>FName</td>");
            sb.Append("<td style='width:100px'>LName</td>");
            sb.Append("<td style='width:75px'>DOB</td>");
            sb.Append("<td style='width:75px'>Gender</td>");
            sb.Append("<td style='width:110px'>City</td>");
            sb.Append("<td style='width:110px'>State</td>");
            sb.Append("<td style='width:75px'>Zip Code</td>");
            sb.Append("<td style='width:100px'>DOH</td>");
            sb.Append("<td style='width:150px'>Occupation</td>");           
            sb.Append("<td style='width:175px'>Medical Coverage Type</td>");
            sb.Append("<td style='width:300px'>Medical Product</td>");
            sb.Append("<td style='width:100px'>Medical Status</td>");
            sb.Append("<td style='width:175px'>Dental Coverage Type</td>");
            sb.Append("<td style='width:300px'>Dental Product</td>");
            sb.Append("<td style='width:100px'>Dental Status</td>");
            sb.Append("<td style='width:175px'>Vision Coverage Type</td>");
            sb.Append("<td style='width:300px'>Vision Product</td>");
            sb.Append("<td style='width:100px'>Vision Status</td>");
            for (int i = 1; i <= getMaxrecdependent; i++)
            {
                sb.Append("<td style='width:100px'>Relation" + i + "</td>");
                sb.Append("<td style='width:100px'>FirstName" +i + "</td>");
                sb.Append("<td style='width:100px'>MiddleName" + i + "</td>");
                sb.Append("<td style='width:100px'>LastName" + i + "</td>");
                sb.Append("<td style='width:100px'>DOB" + i + "</td>");
                sb.Append("<td style='width:100px'>Gender" + i + "</td>");
                sb.Append("<td style='width:100px'>Notes" + i + "</td>");
            }
            sb.Append("</tr>");
    sb.Append("</table>");

            StringReader sr = new StringReader(sb.ToString());
            HttpContext.Current.Response.Output.Write(sb.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

when excel file downloaded it show security warning message in property window and i also setting in trust center of excel, but it does not open in excel 2016. Please suggest what need to do.Thanks

  • That's not an Excel file. That's an HTML table masquerading as the old-style, binary, `xls` format. Excel will try to *import* it but it will fail if there is any problem with the table. It's probably invalid HTML as well, as there is no `html` tag. Create a *real* XLSX file with a library like EPPlus. Populating a sheet for a DataTable or collection is as simple as calling `sheet.LoadFromCollection(someList);` – Panagiotis Kanavos Aug 19 '16 at 12:04
  • thanks for replay "Panagiotis Kanavos" . but when i unblock from property window , then working fine. – Pargat Singh Aug 19 '16 at 12:09
  • It's still a fake Excel file, that's larger than an actual file XLSX file, takes 20 more lines to write and is going to cause problems for customers that discover they can't save changes. One that can easily fail too, if you add eg a `<` character in a cell, use dates or decimals or send the file to clients that have a different locale – Panagiotis Kanavos Aug 19 '16 at 12:15
  • Besides, xlsx filed aren't affected by blocking because they don't contain any macros. Your file was blocked because it's an HTML file that has to be imported and can't be considered safe – Panagiotis Kanavos Aug 19 '16 at 12:18
  • ok Thanks. when same file downloaded in another machine it open easily and working fine. – Pargat Singh Aug 19 '16 at 12:31

0 Answers0