viernes, 29 de febrero de 2008

Crystal Reports en WinForms - Exportar a pdf un reporte con C#.NET

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace ClockManager
{
public partial class Reports : Form
{
#region Propiedades

public string filePath = "";
SaveFileDialog saveFileDialog1 = new SaveFileDialog();

#endregion

#region Metodos

public Reports()
{
InitializeComponent();
}
public void SaveFile()
{


if (filePath.Trim() == "")
return;
//
TuNameSpace.RPT_FECHA_HORA_X_ID rpt = new TuNameSpace.RPT_FECHA_HORA_X_ID();
ReportDocument myReport = new ReportDocument();

//Le paso un parametro al reporte [esto es opcional]
rpt.SetParameterValue("@ID", "25832556");

//Defino los parametros de conexion
string UserID = "usuario1";
string Password = "123456";
string ServerName = "MiPc\\SQLEXPRESS";
string DatabaseName = "MIBASE";

//Seteo los parametros de conexion
rpt.SetDatabaseLogon(UserID, Password, ServerName, DatabaseName);


try
{
//Exporto el archivo
rpt.ExportToDisk(ExportFormatType.PortableDocFormat, this.filePath);
MessageBox.Show("OK!");
}
catch (Exception ex)
{
string a = ex.Message;
}

}

#endregion

private void Reports_Load(object sender, EventArgs e)
{
//Seteo algunas propiedades del SaveFileDialog
this.saveFileDialog1.AddExtension = true;
this.saveFileDialog1.AutoUpgradeEnabled = true;
this.saveFileDialog1.CheckFileExists = false;
this.saveFileDialog1.CheckPathExists = false;
this.saveFileDialog1.DefaultExt = ".pdf";

this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
saveFileDialog1.ShowDialog();
}

#region Eventos

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
this.filePath = saveFileDialog1.FileName;
SaveFile();
}


#endregion
}

}

No hay comentarios: