sql = "select CardName,CardPass,isUse,CardType,Datatime,isExport from Card where isExport='0'";
SqlConnection con = Db.GetDb();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "123"); string path = Server.MapPath("flysoblog.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["123"];
sw.Write("\0\0\0" + "学生卡号" + "\0\0\0\0\0\0\0\0\0\0\0\0学生密码" + "\0\0\0\0\0\0卡号类型\0\0\0\0\0\0使用情况\0\0\0\0生成时间\r\n");
sw.Write("-------------------------------------------------------------------------\r\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
sw.Write(dt.Rows[i]["CardName"].ToString().Trim() + "\0\0\0");
sw.Write(dt.Rows[i]["CardPass"].ToString().Trim() + "\0\0\0"); using (SqlConnection conn = Db.GetDb())
{
string sqln = "update Card set isExport=1 where CardName=@CarName";
SqlCommand annd = new SqlCommand(sqln, conn);
annd.Parameters.Add("@CarName", SqlDbType.VarChar).Value = dt.Rows[i]["CardName"].ToString().Trim(); conn.Open();
annd.ExecuteNonQuery();
annd.Clone(); } sw.Write(dt.Rows[i]["CardType"].ToString().Trim() + "\0\0\0");
string isSy = string.Empty;
isSy = dt.Rows[i]["isUse"].ToString().Trim();
if (isSy.ToLower().Equals("true"))
{
isSy = "\0\0\0\0\0已使用\0";
}
else
{
isSy = "\0\0\0\0\0未使用\0";
}
sw.Write(isSy + "\0\0\0");
sw.Write(dt.Rows[i]["DataTime"].ToString().Trim() + "\0\0\0");
sw.Write("\r\n");
}
//循环输出内容以及格式
con.Close();
sw.Close();
fsobj.Close();
sw.Dispose();
fsobj.Dispose();
string contentType = "application/";
string filename = "flysonetCard.txt";
System.Diagnostics.Process.Start("notepad.exe", "学习卡-" + System.DateTime.Now.ToString("yyyy年mm月dd日") + ".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();
//执行完上面的代码之后 我想跳转到别的页面
//这是一个点击按钮另存为 之后再跳转到别的页面
//请详细代码告诉我 谢谢老鸟
解决方案 »
- 报Object reference not set to an instance of an object 的错 为什么?
- 求高人。Request.QueryString("?")和Request("?")有哪些区别
- 索引超出范围
- 求asp.net的code?
- 提取日期字段里的年月日
- xp+vs2005下设计的水晶报表,放到win2003上打印,格式偏差,何故?
- 《网络聊天室》求解
- 我有一个存储过程p_backupdb ,实现数据库的备份,我在ASP.NET下如何 ,调用这个存储过程??在线等!
- ASP.NET网站管理工具中配置问题?
- 在线人数程序
- 十分奇怪:vs中调试时重定向正确,iis中通过本机IP地址访问却无法打开
- Gridview 的导出 出现乱码
HttpContext.Current.Response.End();
上面说的我都用过了 研究了一下午了 直接跳转不行 别猜
可在其他页面DownLoadFiles.aspx下载文件
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
Response.End();
Me.Response.Redirect(URL)
Response.Redirect("page.aspx");//请用 Response.Redirect("page.aspx",false); 替换
//只要不在页面中使用Response.End();就不会出现线程中止异常.
//Response.Redirect("page.aspx");默认调用了Response.End();
//使用重载传入false可以不调用Response.End();而不出现此异常
解决方案
要解决此问题,请使用下列方法之一:
• 对于 Response.End,调用 HttpContext.Current.ApplicationInstance.CompleteRequest 方法而不是 Response.End 以跳过 Application_EndRequest 事件的代码执行。
• 对于 Response.Redirect,请使用重载 Response.Redirect(String url, bool endResponse),该重载对 endResponse 参数传递 false 以取消对 Response.End 的内部调用。例如:
Response.Redirect ("nextpage.aspx", false);
如果使用此替代方法,将执行 Response.Redirect 后面的代码。
• 对于 Server.Transfer,请改用 Server.Execute 方法。