Como exportar Crystal Report para PDF e Excel (código completo)
Vamos aprender, de forma rápida, como exportar um relatório de Crystal Report para os formatos PDF e Excel. Como exemplo vou pegar o projecto que fizemos no post anterior. Clica aqui para ver.Adicione os botões "Exportar PDF" e "Exportar Excel" no formulário para exportar o relatório.
Para o botão "Exportar PDF" coloque o seguinte código:
private void btnExportToPDF_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo PDF|*.pdf";
string nomeArquivo="";
if(dialog.ShowDialog()== DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.PortableDocFormat, nomeArquivo);
}
}
Para o botão "Exportar Excel" coloque o seguinte código:
private void btnExportToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo Excel|*.xls";
string nomeArquivo = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.Excel, nomeArquivo);
}
}
Como podemos ver, para os dois botões, colocamos um SaveFileDialog para o utilizador poder escolher a pasta e o nome do arquivo.
Executa o programa e testa cada botão. Espero te ajudado.
public partial class FormPaises : Form
{
public FormPaises()
{
InitializeComponent();
}
Reports.ReportPaises2 cr = new Reports.ReportPaises2();
private List<Pais> GetListaPaises()
{
List<Pais> Paises = new List<Pais>
{
new Pais(1, "Angola"),
new Pais(2, "Brasil"),
new Pais(3, "Portugal"),
new Pais(6, "Moçambique"),
new Pais(7, "Cabo-Verde"),
new Pais(8, "Namibia")
};
return Paises;
}
private void FormPaises_Load(object sender, EventArgs e)
{
crystalReportViewer1.ReportSource = cr;
cr.SetDataSource(GetListaPaises());
}
private void btnExportToPDF_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo PDF|*.pdf";
string nomeArquivo="";
if(dialog.ShowDialog()== DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.PortableDocFormat, nomeArquivo);
}
}
private void btnExportToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo Excel|*.xls";
string nomeArquivo = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.Excel, nomeArquivo);
}
}
}
Código completo deste projecto.
public partial class FormPaises : Form
{
public FormPaises()
{
InitializeComponent();
}
Reports.ReportPaises2 cr = new Reports.ReportPaises2();
private List<Pais> GetListaPaises()
{
List<Pais> Paises = new List<Pais>
{
new Pais(1, "Angola"),
new Pais(2, "Brasil"),
new Pais(3, "Portugal"),
new Pais(6, "Moçambique"),
new Pais(7, "Cabo-Verde"),
new Pais(8, "Namibia")
};
return Paises;
}
private void FormPaises_Load(object sender, EventArgs e)
{
crystalReportViewer1.ReportSource = cr;
cr.SetDataSource(GetListaPaises());
}
private void btnExportToPDF_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo PDF|*.pdf";
string nomeArquivo="";
if(dialog.ShowDialog()== DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.PortableDocFormat, nomeArquivo);
}
}
private void btnExportToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Arquivo Excel|*.xls";
string nomeArquivo = "";
if (dialog.ShowDialog() == DialogResult.OK)
{
nomeArquivo = dialog.FileName;
cr.ExportToDisk(ExportFormatType.Excel, nomeArquivo);
}
}
}