A simple way to convert or export data to a CSV format. Columns are separated by comma (a delimiter) and rows by each line.
In my example below, I used StringBuilder for the content of my file, Environment.NewLine to move my cursor to next line. In the programming world, records can be pulled from any collections such as Dataset, LINQ and the likes.
After the page has been executed, a .csv file will be created and a message will pop-up to ask if you want to save or open the file.
C# Code:
public void ExportCSV()
In my example below, I used StringBuilder for the content of my file, Environment.NewLine to move my cursor to next line. In the programming world, records can be pulled from any collections such as Dataset, LINQ and the likes.
After the page has been executed, a .csv file will be created and a message will pop-up to ask if you want to save or open the file.
C# Code:
public void ExportCSV()
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=Result.csv");
StringBuilder sbt = new StringBuilder();
sbt.Append("Name,Age");
sbt.Append(Environment.NewLine);
sbt.Append("Anna,15");
sbt.Append(Environment.NewLine);
sbt.Append("Joe,12");
sbt.Append("Anna,15");
sbt.Append(Environment.NewLine);
sbt.Append("Joe,12");
context.Response.Write(csvresult);
context.Response.End();
}
protected void btnExport_Click(object sender, EventArgs e)
{
ExportCSV();
}
L.





0 comments:
Post a Comment