1> define model to generate a CSV file
public class Csvmodel
{public
int Id {get; set;}
public string Productnum {get; set;}
public string Com {get; set;}
public string Name {get; set;}
public string Phone {get; set;}
public string Email {get; set;}
public string Addinfo {get; set;}
public string String1 {get; set;}
public string String3 {get; set;}
}
2> Generate a CSV section
public static string Saveascsv<t> (String fileName, ilist<t> Listmodel) where T:class, new () {
String results = "";
try {StringBuilder sb = new StringBuilder (); Show columns to display by reflection bindingflags BF = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public |
bindingflags.static;//Reflection identification Type ObjType = typeof (T);
propertyinfo[] Propinfoarr = objtype.getproperties (BF); String Header = String.
Empty;
list<string> listpropertys = new list<string> (); foreach (PropertyInfo info in Propinfoarr) {if String.Compare (info. Name.toupper (), "ID")!= 0)//does not take account of the self-growing ID or the automatically generated GUID such as {if (!listpropertys.conta INS (info. Name)) {Listpropertys.add (info).
Name); Header = = info.
Name + ","; }//Here is the header header = "Product number" + "," + "Company Name" + "," + "Contact name" + "," + "contact
People telephone "+", "+" Contact Mailbox "+", "+" contact address "+", "+" Inquiry Time "+", "+" whether to view; Sb. Appendline (header. Trim (', ')); CSV header foreach (T model in Listmodel) {string Strmodel = string.
Empty; foreach (String strprop in Listpropertys) {foreach (PropertyInfo propinfo in Propinfoarr) {if (string). Compare (PropInfo.Name.ToUpper (), strprop.toupper ()) = = 0) {Pr Opertyinfo Modelproperty = model. GetType ().
GetProperty (Propinfo.name); if (modelproperty!= null) {object Objresult = modelproperty.getvalue (model, NULL); string result = ((Objresult = null)? string. Empty:objresult). ToString ().
Trim (); if (result. IndexOf (', ')!=-1 {result = "\" "+ RESULT.R Eplace ("\" "," \ "\") + "" "; Special character Processing} if (!string. IsNullOrEmpty (Result)) {Type valuetype = mode
Lproperty.propertytype;
if (Valuetype.equals (typeof (Nullable<decimal>)) { result = Decimal. Parse (Result).
ToString ("#.#");
else if (Valuetype.equals (typeof (decimal))
{ result = Decimal. Parse (Result).
ToString ("#.#");
else if (Valuetype.equals typeof (Nullable<double>)) {result = Double. Parse (Result).
ToString ("#.#");
else if (Valuetype.equals (typeof double)) {result = Double. Parse (Result).
ToString ("#.#");
else if (Valuetype.equals typeof (Nullable<float>)) {result = float. Parse (Result).
ToString ("#.#"); else if (valuetype.equalS (typeof (float)) {result = float. Parse (Result).
ToString ("#.#");
} Strmodel + = result + ",";
} else {
Strmodel + = ",";
} break; }} Strmodel = strmodel.substring (0, Strmodel.length-1
); Sb.
Appendline (Strmodel); } string content = sb.
ToString ();
results = content;
Catch {} return results;
}
3> respond to the Click event, locate the desired data, and call the previous method
protected void Btnimport_click (object sender, EventArgs e) {DataSet ds = new BLL. Consultationprice ().
GetList ("1=1");
list<csvmodel> db_list = new list<csvmodel> (); if (ds. Tables.count > 0) {if (ds. Tables[0]. Rows.Count > 0) {for (int i = 0; I < ds. Tables[0]. Rows.Count;
i++) {Csvmodel Db_model = new Csvmodel (); Db_model. Id = ds. Tables[0]. rows[i]["ProductId"].
ToInt (); Db_model. Productnum = ds. Tables[0]. rows[i]["Productnum"].
ToString (); Db_model. Com = ds. Tables[0]. rows[i]["Com".
ToString (); Db_model. Name = ds. Tables[0]. rows[i]["Name"].
ToString (); Db_model. Phone = ds. Tables[0]. rows[i]["Phone"].
ToString (); Db_model. Email = ds. Tables[0]. rows[i]["Email"].
ToString (); Db_model. Addinfo = ds. Tables[0]. rows[i]["Addinfo"].
ToString (); Db_model. String1 = ds. Tables[0]. rows[i]["String1"].
ToString (); Db_model. String3 = ds. Tables[0]. rows[i]["Int1"]. ToInt () = = 1?
"Viewed": "Not viewed"; Db_list.
ADD (Db_model); }}//saveascsv (@ "C:\Users\Administrator\Desktop\" + System.DateTime.Now.ToStrin
G ("Yyyy-mm-dd-hh-mm-ss") + ". csv", db_list); String OI = Saveascsv (@ "C:\Users\Administrator\Desktop\" + System.DateTime.Now.ToString ("yyyy-mm-dd-hh-mm-ss") + ".
CSV ", db_list);
StringBuilder objcsv = analysislayer.loadchargeinfotocsvstring (Viewstatefrom, Viewstateto, generatesortstring ());
String strfile = Server.MapPath ("Mygod.csv");
Response.Clear ();
Response.Buffer = true;
Response.Charset = "Utf-8"; String names = System.DateTime.Now.ToString ("Online Request yyYy_mm_dd_hh_mm_ss ");
Response.appendheader ("Content-disposition", "Attachment;filename=" +names+ ". csv");
response.contentencoding = System.Text.Encoding.Default; RESPONSE.CONTENTTYPE Specifies the file type can be Application/ms-excel | | Application/ms-word | | Application/ms-txt | | application/ms-html | |
or other browsers can directly support file Response.ContentType = "Application/ms-txt"; This.
EnableViewState = false; Response.Write (OI.
ToString ());
Response.End ();
Response.Write (OI);
}
If there is a problem, please add me qq:631931078 or 352167311