现在的位置: 首页 > 综合 > 正文

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

2013年02月17日 ⁄ 综合 ⁄ 共 1008字 ⁄ 字号 评论关闭
这个是从数据库中读出保存的文件并写到客户端,而且文且不会在客户端浏览器中直接打开。

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();
}

抱歉!评论已关闭.