/// <Summary>
/// Export to. CSV format
/// </Summary>
/// <Param name = "GV"> gridview Control </param>
/// <Param name = "page"> page </param>
/// <Param name = "FILENAME"> file name </param>
Public static void importtocsv (system. Web. UI. webcontrols. gridview GV, system. Web. UI. Page, int [] displaycolumns, string filename)
{
String defaultfilename = "input name.csv ";
If (string. isnullorempty (filename ))
{
Filename = defaultfilename;
}
Stringbuilder sb = new stringbuilder ();
Page. response. appendheader ("content-disposition", "attachment; filename =" + filename );
// Page. response. contenttype = "application/MS-excel ";
Page. response. charset = "UTF-8 ";
Page. response. contentencoding = system. Text. encoding. utf8;
Foreach (system. Web. UI. webcontrols. gridviewrow row in GV. Rows)
{
If (row. rowtype = system. Web. UI. webcontrols. datacontrolrowtype. datarow)
{
For (INT I = 0; I <displaycolumns. length; I ++)
{
// & Nbsp; has instead of string. Empty when default value is string. empty in the cell of gridview, so replace it.
SB. append ("\"");
If (! Row. cells [displaycolumns [I]. Text. Trim (). Equals ("& nbsp ;"))
{
SB. append (row. cells [displaycolumns [I]. Text. Trim ());
}
SB. append ("\"");
If (I! = Displaycolumns. Length-1)
{
SB. append (",");
}
Else
{
SB. append ("\ r \ n ");
}
}
}
}
// Output
Page. response. Write (sb. tostring ());
Page. response. End ();
}