Writedstocsv: input dataset to generate a CSV file
# Region writedstocsv import dataset to generate a CSV file
Public void writedstocsv (Dataset DS)
{
String strfile = "";
String Path = "";
Datatable dt = Ds. Tables [0];
// File Information settings
Strfile = strfile + "logbackup ";
Strfile = strfile + datetime. Now. tostring ("yyyymmddhhmmss ");
Strfile = strfile + ". CSV ";
Path = server. mappath (strfile );
// String [] strhead = {"User Name", "employee ID", "affiliated branch", "job time", "usage", "job description "};
System. Io. filestream FS = new filestream (path, system. Io. filemode. Create, system. Io. fileaccess. Write );
Streamwriter Sw = new streamwriter (FS, new system. Text. unicodeencoding ());
// Draw the header
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Sw. Write (Dt. Columns [I]. columnname );
Sw. Write ("\ t ");
}
Sw. writeline ("");
// Draw the table body
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Sw. Write (delquota (Dt. Rows [I] ["userid"]. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (Dt. Rows [I] ["username"]. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (Dt. Rows [I] ["provinceid"]. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (Dt. Rows [I] ["cityid"]. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (Dt. Rows [I] ["areaid"]. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (Dt. Rows [I] ["enabled"]. tostring ()));
Sw. writeline ("");
}
Sw. Flush ();
Sw. Close ();
}
# Endregion
Writedatagridtocsv is passed into the DataGrid to generate a CSV file
# Region writedatagridtocsv input into the DataGrid to generate a CSV file
Public void writedatagridtocsv (system. Web. UI. webcontrols. DataGrid grid)
{
String strfile = "";
String Path = "";
Datatable dt = Ds. Tables [0];
// File Information settings
Strfile = strfile + "logbackup ";
Strfile = strfile + datetime. Now. tostring ("yyyymmddhhmmss ");
Strfile = strfile + ". CSV ";
Path = server. mappath (strfile );
String [] strhead = {"userid", "username", "provinceid", "cityid", "areaid", "enabled "};
System. Io. filestream FS = new filestream (path, system. Io. filemode. Create, system. Io. fileaccess. Write );
Streamwriter Sw = new streamwriter (FS, new system. Text. unicodeencoding ());
// Draw the header
For (INT I = 0; I <strhead. length; I ++)
{
Sw. Write (strhead [I]);
Sw. Write ("\ t ");
}
Sw. writeline ("");
// Draw the table body
For (INT I = 0; I <grid. Items. Count; I ++)
{
Sw. Write (delquota (grid. items [I]. cells [0]. Text. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (grid. items [I]. cells [1]. Text. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (grid. items [I]. cells [2]. Text. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (grid. items [I]. cells [3]. Text. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (grid. items [I]. cells [4]. Text. tostring ()));
Sw. Write ("\ t ");
Sw. Write (delquota (grid. items [I]. cells [5]. Text. tostring ()));
Sw. writeline ("");
}
Sw. Flush ();
Sw. Close ();
}
# Endregion
Toexcel
# Region toexcel
Public static void toexcel (system. Web. UI. Control CTL, string filename)
{
Httpcontext. Current. response. charset = "UTF-8 ";
Httpcontext. Current. response. contentencoding = system. Text. encoding. default;
Httpcontext. Current. response. contenttype = "application/MS-excel ";
Httpcontext. Current. response. appendheader ("content-disposition", "attachment; filename =" + "" + filename + ". xls ");
CTL. Page. enableviewstate = false;
System. Io. stringwriter Tw = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (TW );
CTL. rendercontrol (HW );
Httpcontext. Current. response. Write (TW. tostring ());
Httpcontext. Current. response. End ();
}
# Endregion
Delquota # region delquota
Public String delquota (string Str) // Delete special characters
{
String result = STR;
String [] strquota = {"~ ","! "," @ "," # "," $ "," % "," ^ ","&","*","(",")", "'",";","'",",",". ","/",": ","/, "," <","> ","? "};
For (INT I = 0; I <strquota. length; I ++)
{
If (result. indexof (strquota [I])>-1)
Result = result. Replace (strquota [I], "");
}
Return result;
}
# Endregion
Getdataset # region getdataset
Public static dataset getdataset (string SQL)
{
String connectionstring = system. configuration. configurationsettings. etettings ["connectionstring"];
Sqldataadapter SDA = new sqldataadapter (SQL, connectionstring );
Dataset DS = new dataset ();
SDA. Fill (DS );
Return Ds;
}
# Endregion