Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Excel export using stringbuilder in c# console application

I want to convert the string builder data to an excel in c# console application. I tried below code but The Response object is giving error.

str1.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>");
        str1.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Section1>");
        str1.Append("<DIV  style='font-size:12px;'>");
        str1.Append(str.ToString());
        str1.Append("</div></body></html>");
        string strFile = "Text_Excel.xls";
        string strcontentType = "application/excel";
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;
        Response.ContentType = strcontentType;

//file open mode with file name

        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
        Response.Write(str1.ToString());
        Response.Flush();
        Response.Close();
        Response.End();

Can anyone help me on this?

Comments