浩子

在.NET的世界里徜徉

导航

ASP.NET 页面中水晶报表导出之二

这个是从数据库中读出保存的文件并写到客户端,而且文且不会在客户端浏览器中直接打开。

string s_conn = ConfigurationSettings.AppSettings["dbConnISMS"];
SqlConnection conn = new SqlConnection(s_conn);

string sSQL = "SELECT CERTIFICATE, CERT_SIZE, CERT_FORMAT FROM T_ACCEPT_CERTIFICATE " +
 "WHERE SEQ_NO = " + sSeqNo;

SqlCommand comm = new SqlCommand(sSQL, conn);

conn.Open();

SqlDataReader reader = comm.ExecuteReader();

while (reader.Read())
{
 SqlBinary buffer = reader.GetSqlBinary(0);
 string format = reader["CERT_FORMAT"].ToString();
 string filename = "";

 switch (format)
 {
  case "1":
  {
   filename = "Certificate.doc";
   break;
  }
  case "2":
  {
   filename = "Certificate.xls";
   break;
  }
  case "3":
  {
   filename = "Certificate.pdf";
   break;
  }
 }

 string certsize = reader["CERT_SIZE"].ToString();

 Response.Clear();
 Response.ClearContent();
 Response.ClearHeaders();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment;FileName="+filename);
 Response.AddHeader("Content-Length", certsize);

 if (!buffer.IsNull)
  Response.BinaryWrite(buffer.Value);

 Response.End();
}

posted on 2004-11-22 18:16  浩子  阅读(1134)  评论(1编辑  收藏  举报