- C# code
protected void Button1_Click(object sender, EventArgs e) { string path = Server.MapPath(("init_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + ".txt")); //FileStream fsobj = new FileStream(path, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(path,true, System.Text.UnicodeEncoding.Unicode); SqlDataAdapter da = new SqlDataAdapter(sql, myConnection); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { sw.Write(dt.Rows[i]["userID"].ToString().Trim() + "," + dt.Rows[i]["userName"].ToString().Trim() + "," + dt.Rows[i]["Address"].ToString().Trim() + "," + dt.Rows[i]["Telphone"].ToString().Trim() + "\r\n"); } sw.Close();
导出后显示的文本如下:
1,张飞,南宁,1111
2,吕梁,广西,11
3,水平,中国,9999
我的问题是怎么让生成的文本不直接保存到目录中,而是弹出一个保存窗口,可以保存到电脑的任何一个位置...用C#编写...
------解决方案--------------------------------------------------------
我有自己写的类(VB),你要的话可以晚上发给你.
------解决方案--------------------------------------------------------
用javascript
- HTML code
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为><OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
------解决方案--------------------------------------------------------
- C# code
private void ExportTXT() { string str_File=""; string strconn= ConfigurationSettings.AppSettings["dsn"]; SqlConnection cn=new SqlConnection(strconn); string s_temp="select applypernum ,starttime ,endtime,leavemode,leavehour from temptable order by tempedi"; string SaveFileName=DL1.SelectedItem.Text.ToString(); SaveFileName=SaveFileName+".txt"; try { cn.Open(); SqlCommand cmtemp=new SqlCommand(s_temp,cn); SqlDataReader drtemp=cmtemp.ExecuteReader(); while(drtemp.Read()) { str_File=str_File+drtemp["applypernum"].ToString(); str_File=str_File+drtemp["starttime "].ToString; ........ str_File=str_File+"\r\n"; } if(File.Exists("c:\\"+SaveFileName)) { Label1.Text="该文件已经存在!已经被删除,请重新再次导出即可"; File.Delete("c:\\"+SaveFileName); return; } else { StreamWriter sw=File.CreateText("c:\\"+SaveFileName); //创建文本文件 sw.WriteLine(str_File); sw.Close(); Label1.Text="写入文件成功!"; } } catch(Exception ex) { Label1.Visible=true; Label1.Text=ex.Message; } finally { cn.Close(); } }
------解决方案--------------------------------------------------------
在做Web程序之前,先要搞清楚Web程序对本地资源基本上是不可访问的.
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
不是很明白楼主的意思
如果是要让文件保存到客户端的任意位置,可以把文件内容以流的形式输出来,让用户选择存放地址
- C# code
public static void DownloadSmallFiles(HttpResponse _Response, string Vpath, string FielName) { _Response.Clear(); _Response.Buffer = true; _Response.Charset = "utf-8"; _Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FielName, System.Text.Encoding.UTF8)); _Response.ContentType = "application/octet-stream"; _Response.WriteFile(Vpath); _Response.Flush(); _Response.End(); }