public void ExportToTxt(Object Sender, EventArgs E)
{
//www.flyso.cn www.flyso.cn
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["sqlcon"]);
SqlDataAdapter da = new SqlDataAdapter("select distinct mobile_no from [80NT]", con);
DataSet ds = new DataSet();
da.Fill(ds, "dtable");
//生成相应的数据
string path = Server.MapPath("80NT.txt"); //根当前程序放在同一个目录
if (File.Exists(path))
{
File.Delete(path);
}
if (!File.Exists(path))
{
File.Create(path).Close();
}
//生成文本文件的路径
FileStream fsobj = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter _sw = new StreamWriter(fsobj, System.Text.UnicodeEncoding.Unicode);
DataTable dt = ds.Tables["dtable"];
_sw.Write("mobile_no" + "\r\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
_sw.Write(dt.Rows[i]["mobile_no"].ToString().Trim() + "\r\n");
}
//循环输出内容以及格式
_sw.Close();
fsobj.Close();
string contentType = "application/";
string filename = "flysonet.txt";
System.Diagnostics.Process.Start("notepad.exe", "80NT.txt");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.End();
//关闭相应的东东
}