痞子龍㊣

C#零起点

 

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

string sDestFile = Path.GetTempFileName();

   string sExportFormatType = text_export.SelectedItem.Text;

   if (sExportFormatType == "")
    return;

   DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
   diskOpts.DiskFileName = sDestFile;

   oRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;

   switch (sExportFormatType)
   {
    case "Mircrosoft Word 文档":
    {
     oRpt.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
     break;
    }
    case "Mircrosoft Excel 文档":
    {
     oRpt.ExportOptions.ExportFormatType = ExportFormatType.Excel;
     break;
    }
    case "Adobe PDF 文档":
    {
     oRpt.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
     break;
    }
   }

   oRpt.ExportOptions.DestinationOptions = diskOpts;

   oRpt.Export();

   Response.ClearContent();
   Response.ClearHeaders();

   switch (sExportFormatType)
   {
    case "Mircrosoft Word 文档":
    {
     Response.ContentType = "application/msword";
     break;
    }
    case "Mircrosoft Excel 文档":
    {
     Response.ContentType = "application/vnd.ms-excel";
     break;
    }
    case "Adobe PDF 文档":
    {
     Response.ContentType = "application/pdf";
     break;
    }
   }

   Response.WriteFile(sDestFile);
   Response.Flush();
   Response.Close();

   File.Delete(sDestFile);

posted on 2006-12-20 10:39  hnsjack(痞子龍)  阅读(231)  评论(0编辑  收藏  举报

导航