在web上实现动态数据源,导出PDF文件、DOC文件

using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace crdataset
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  OleDbConnection adoOleDbConnection;
  OleDbDataAdapter adoOleDbDataAdapter;
  public DataSet dataSet;
  CrystalReport1 crReportDocument;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;

 
  private void ExportReport(ReportDocument Report,string OptionStr)
  {
     
   string ExportPath;
   ExportPath = Request.PhysicalApplicationPath + "Exported//";
   if (Directory.Exists(ExportPath) == false) Directory.CreateDirectory(Request.PhysicalApplicationPath + "Exported//");

   
   ReportDocument crReportDocument = Report;
   DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();
   ExportOptions crExportOptions = crReportDocument.ExportOptions;

   switch(OptionStr)
   {
    case "Portable Document (PDF)":
     crDiskFileDestinationOptions.DiskFileName = ExportPath + "PortableDoc.pdf";
     crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
     crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
     crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
     break;
    case "MS Word (DOC)":
     crDiskFileDestinationOptions.DiskFileName = ExportPath + "Word.doc";
     crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
     crExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
     crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
     break;
   
   }   
   
   try
   {
    // Export the report
    crReportDocument.Export();
    crReportDocument.Close();
    Response.ClearContent();
    Response.ClearHeaders();
    switch(OptionStr)
    {
     case "Portable Document (PDF)":
      Response.ContentType="application/pdf";
      Response.WriteFile(ExportPath + "PortableDoc.pdf");
      break;
     case "MS Word (DOC)":  
      Response.ContentType="application/msword";
      Response.WriteFile(ExportPath + "Word.doc");
      break;
    
    }

    Response.Flush();
    Response.Close();
    
   }
   catch (Exception err)
   {
    Response.Write("<BR>");
    Response.Write(err.Message.ToString());
   }
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   string connectionString= "";
   connectionString = "Provider=SQLOLEDB;";
   connectionString += "Server=it003;Database=pubs;";
   connectionString += "User ID=sa;Password=";

   //Create and open a connection using the connection string
   adoOleDbConnection = new OleDbConnection(connectionString);

   //Build a SQL statement to query the datasource
   string sqlString = "";
   sqlString = "Select *  From authors";

   //Retrieve the data using the SQL statement and existing connection
   adoOleDbDataAdapter = new OleDbDataAdapter(sqlString, adoOleDbConnection);

   //Create a instance of a Dataset
   dataSet = new DataSet();

   //Fill the dataset with the data retrieved.  The name of the table
   //in the dataset must be the same as the table name in the report.
   adoOleDbDataAdapter.Fill(dataSet, "authors");
   //Create an instance of the strongly-typed report object
   crReportDocument = new CrystalReport1();
   //Pass the populated dataset to the report
   crReportDocument.SetDataSource(dataSet);
   //Set the viewer to the report object to be previewed.
   CrystalReportViewer1.ReportSource = crReportDocument;
  }
  

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   this.crReportDocument = new CrystalReport1();
   //Pass the populated dataset to the report
   DataSet ds = new DataSet();
            OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB;server=localhost;database=pubs;uid=sa;pwd=");
   con.Open();
   OleDbDataAdapter ada=new OleDbDataAdapter("select * from authors where au_id like '%899%'",con);
   ada.Fill(ds,"authors");
   crReportDocument.SetDataSource(ds);
   ExportReport(this.crReportDocument,"Portable Document (PDF)");

  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle SQL是Oracle公司开发的一种关系数据库管理系统的查询语言。它是一种结构化查询语言,用于在关系型数据库中进行数据查询、插入、更新和删除等操作。 PDF(Portable Document Format)是一种跨平台的电子文档格式,最初由Adobe Systems开发。它可以保持文档的完整性和可读性,无论在哪种操作系统或设备上打开。 结合这两个概念,Oracle SQL PDF可以指的是将Oracle数据库中的数据PDF格式输出的过程或工具。 在Oracle数据库中,可以使用查询语句从数据库中检索数据。通过使用一些特定的SQL函数和语法,可以将查询结果导出PDF格式的文件中。这样做的好处是可以轻松地在不同的操作系统和设备上查看和共享这些数据。 一种常见的方法是使用Oracle SQL开发工具,如Oracle SQL Developer或PL/SQL Developer等。这些工具提供了将查询结果导出为多种格式的功能,包括PDF格式。用户只需运行适当的查询,并选择导出选项为PDF,然后选择保存目标路径,即可生成相应的PDF文件。 Oracle数据库还提供了一些专门用于生成PDF的工具和插件。例如,Oracle BI Publisher可以将数据库查询结果以PDF格式输出,并可进行样式和布局的定制。 总而言之,Oracle SQL PDF是指将Oracle数据库中的数据PDF格式导出的过程或工具。这样做可以方便地在不同的操作系统和设备上共享、查看和打印数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值