So continuing from my exporting sample series here I am
narrating how to export data to HTML file in C#, ASP.Net. For this example I’ll
use same “GetData” method (to get the data for exporting) which we had
used in Export to Excel example, you can use your own method to get data.
/// <summary>
/// Export to Html
/// </summary>
public static
void ExportHtmlFile()
{
const string format = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>";
var sb = new StringBuilder();
sb.AppendFormat("<h1
style='font-size: 18px;font-weight: bold;'>{0}</h1>"
, "Export
HTML Sample");
sb.Append("<table
style='width:500px;'>");
sb.AppendFormat(format
, "First
Name"
, "Last
Name"
, "Email
Address"
, "City");
var
records = GetData();
foreach (var record in
records)
{
sb.AppendFormat(format
, record.FirstName
, record.LastName
, record.Email
, record.City);
}
sb.Append("</table>");
//Get
current response
var
response = HttpContext.Current.Response;
response.BufferOutput = true;
//clear
response
response.Clear();
response.ClearHeaders();
response.ContentEncoding = Encoding.Unicode;
//proivde file
name as "Test_Data.html"
response.AddHeader("content-disposition", "attachment; filename=Test_Data.html");
response.ContentType = "text/html";
response.Write(sb.ToString());
response.End();
}
|
Comments
Post a Comment