Using system. web;
Public class sdsavecsv
{
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "response"> httpresponse object </param>
Public sdsavecsv (httpresponse response)
{
This. response = response;
}
/// <Summary>
/// Enter the output file
/// </Summary>
/// <Param name = "fullpath"> file name </param>
/// <Param name = "content"> file content </param>
/// <Returns> success/failure </returns>
Public bool outfile (string fullpath, string content)
{
Bool retvalue = false;
Try
{
This. response. buffer = true;
This. response. clear ();
This. response. charset = system. text. encoding. default. bodyname;
This. response. contentencoding = system. text. encoding. getencoding ("gb2312"); // There is no garbled code when opening gb2312 in excel.
This. response. appendheader ("content-disposition", "attachment; filename =" + fullpath );
This. response. contenttype = "application/ms-excel ";
This. response. output. write (content );
This. response. flush ();
This. response. end ();
Retvalue = true;
}
Catch (exception ex)
{
Retvalue = false;
Throw new applicationexception (string. format ("output data csv error: {0}", fullpath), ex );
}
Return retvalue;
}
}
/// ===================================================
Stringbuilder sbfile = new stringbuilder ();
Sbfile. append ("Merchant, egg name, egg id, time, total egg count, total egg count ");
Sbfile. appendline (); // line feed
Foreach (datarow row in ds. tables [0]. rows)
{
Sbfile. appendformat ("{0}, {1}, {2}, {3}, {4}, {5}", eggmerchants1.merchantname, row ["eggname"], row ["rowid"], row ["date"], row ["pep"], row ["num"]);
Sbfile. appendline ();
}
New sdsavecsv (this. response). outfile (string. format ("fetchegg(%0%%%%.csv", datetime. now), sbfile. tostring ());