using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
public partial class CRtable : System.Web.UI.Page
{
DBClass db = new DBClass();
OperateAndValidate opAndvalidate = new OperateAndValidate();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//opAndvalidate.cboxBind("SELECT id FROM tb_jiaoyan", "id", DropDownList1);
FileDReportDocument();//调用方法加载报表
}
}
public void FileDReportDocument()
{
exportTypesList.DataSource = System.Enum.GetValues(typeof(ExportFormatType));
exportTypesList.DataBind();
SqlConnection con = db.GetConnection();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tb_jiaoyan", con);
DataSet ds = new DataSet();
da.Fill(ds, "tb_jiaoyan");
con.Close();
hierarchicalGroupingReport.Load(Server.MapPath("CReport.rpt"));//加载报表路径
hierarchicalGroupingReport.SetDataSource(ds);加载报表数据源
this.CrystalReportViewer1.ReportSource = hierarchicalGroupingReport;//为CrystalReportViewer控件指定报表
}
public string exportPath;//存储导出报表的文件路径
public string exportInfo;//存储、提示信息
public DiskFileDestinationOptions diskFileDestinationOptions;
public ExportOptions exportOptions;
public ReportDocument hierarchicalGroupingReport = new ReportDocument();
public bool selectedNoFormat = false;//判断是否选择导出格式
private void ExportSetup()
{
try
{
exportPath = "C://Exported//";
if (!System.IO.Directory.Exists(exportPath))
{
System.IO.Directory.CreateDirectory(exportPath);
}
hierarchicalGroupingReport.Load(Server.MapPath("CReport.rpt"));//加载报表路径
diskFileDestinationOptions = new DiskFileDestinationOptions();
exportOptions = hierarchicalGroupingReport.ExportOptions;
exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
exportOptions.FormatOptions = null;
}
catch (Exception ee)
{
Response.Write(ee.Message.ToString());
}
}
private void ExportSelection()
{
}
//导出.rpt文件(.rpt)
private void ConfigureExportToRpt()
{
exportOptions.ExportFormatType = ExportFormatType.CrystalReport;
exportInfo = exportPath + "Report.rpt";
diskFileDestinationOptions.DiskFileName = exportPath + "Report.rpt";
exportOptions.DestinationOptions = diskFileDestinationOptions;
}
//导出.rtf文件(.rtf)
private void ConfigureExportToRtf()
{
exportOptions.ExportFormatType = ExportFormatType.RichText;
diskFileDestinationOptions.DiskFileName = exportPath + "RichTextFormat.rtf";
exportOptions.DestinationOptions = diskFileDestinationOptions;
exportInfo = exportPath + "RichTextFormat.rtf";
}
//导出.doc文件(.doc)
private void ConfigureExportToDoc()
{
exportOptions.ExportFormatType = ExportFormatType.WordForWindows;
diskFileDestinationOptions.DiskFileName = exportPath + "Word.doc";
exportOptions.DestinationOptions = diskFileDestinationOptions;
exportInfo = exportPath + "Word.doc";
}
//导出.xls文件(.xls)
private void ConfigureExportToXls()
{
exportOptions.ExportFormatType = ExportFormatType.Excel;
diskFileDestinationOptions.DiskFileName = exportPath + "Excel.xls";
exportOptions.DestinationOptions = diskFileDestinationOptions;
exportInfo = exportPath + "Excel.xls";
}
//导出.pdf文件(.pdf)
private void ConfigureExportToPdf()
{
exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
diskFileDestinationOptions.DiskFileName = exportPath + "PortableDoc.pdf";
exportOptions.DestinationOptions = diskFileDestinationOptions;
exportInfo = exportPath + "PortableDoc.pdf";
}
//导出.html文件(.html)
private void ConfigureExportToHtml32()
{
exportOptions.ExportFormatType = ExportFormatType.HTML32;
HTMLFormatOptions html32FormatOptions = new HTMLFormatOptions();
html32FormatOptions.HTMLBaseFolderName = exportPath + "Html32Folder";
html32FormatOptions.HTMLFileName = "html32.html";
html32FormatOptions.HTMLEnableSeparatedPages = false;
html32FormatOptions.HTMLHasPageNavigator = false;
exportOptions.FormatOptions = html32FormatOptions;
exportInfo = exportPath + "Html32Folder";
}
//导出html40.html文件(.html)
private void ConfigureExportToHtml40()
{
exportOptions.ExportFormatType = ExportFormatType.HTML40;
HTMLFormatOptions html40FormatOptions = new HTMLFormatOptions();
html40FormatOptions.HTMLBaseFolderName = exportPath + "Html40Folder";
html40FormatOptions.HTMLFileName = "html40.html";
html40FormatOptions.HTMLEnableSeparatedPages = true;
html40FormatOptions.HTMLHasPageNavigator = true;
html40FormatOptions.FirstPageNumber = 1;
html40FormatOptions.LastPageNumber = 3;
exportOptions.FormatOptions = html40FormatOptions;
exportInfo = exportPath + "Html40Folder";
}
// //导出.xls文件(.xls)
private void ConfigureExportToXlsRec()
{
exportOptions.ExportFormatType = ExportFormatType.ExcelRecord;
diskFileDestinationOptions.DiskFileName = exportPath + "ExcelRecord.xls";
exportOptions.DestinationOptions = diskFileDestinationOptions;
exportInfo = exportPath + "ExcelRecord.xls";
}
//导出报表
protected void Button1_Click(object sender, EventArgs e)
{
try
{
ExportSetup();//调用方法实例对象
switch ((ExportFormatType)exportTypesList.SelectedIndex)//判断
{
case ExportFormatType.NoFormat:
selectedNoFormat = true;
break;
case ExportFormatType.CrystalReport:
ConfigureExportToRpt();
break;
case ExportFormatType.RichText:
ConfigureExportToRtf();
break;
case ExportFormatType.WordForWindows:
ConfigureExportToDoc();
break;
case ExportFormatType.Excel:
ConfigureExportToXls();
break;
case ExportFormatType.PortableDocFormat:
ConfigureExportToPdf();
break;
case ExportFormatType.HTML32:
ConfigureExportToHtml32();
break;
case ExportFormatType.HTML40:
ConfigureExportToHtml40();
break;
case ExportFormatType.ExcelRecord:///新语句两种新的导出格式添加到项目。
ConfigureExportToXlsRec();
break;
}
if (!selectedNoFormat)
{
hierarchicalGroupingReport.Export();
Response.Write("<script>alert('报表已导出至'+'" + exportInfo + "');</script>");
FileDReportDocument();//调用方法加载报表
}
else
{
Response.Write("<script>alert('你选择了未指定导出格式选项');</script>");
FileDReportDocument();//调用方法加载报表
}
}
catch (Exception eee)
{
Response.Write(eee.Message.ToString());
}
}
protected void CrystalReportViewer1_Init(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
//SqlConnection cn = db.GetConnection();
//cn.Open();
//string strSql = "select *from tb_jiaoyan where id='" + DropDownList1.SelectedValue.ToString() + "' ";
//SqlDataAdapter myAdapter = new SqlDataAdapter(strSql, cn);
//DataSet ds = new DataSet();
//myAdapter.Fill(ds, "tb_jiaoyan");
//hierarchicalGroupingReport.Load(Server.MapPath("CReport.rpt"));
//hierarchicalGroupingReport.SetDataSource(ds);
//this.CrystalReportViewer1.ReportSource = hierarchicalGroupingReport;
//可用
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}