Hello,
You could do it as a two step process: export and print.
First export the report to plain text if your version of Crystal Reports supports that export format. Not all versions of Crystal Reports will export to plain text. For example, I'm using Crystal Reports for Visual Studio .NET, and the only export formats available are:
Adobe Acrobat (.pdf)
Crystal Reports for Visual Studio .NET (.rpt)
HTML 3.2 and 4.0 (.html)
Microsoft Excel (.xls)
Microsoft Rich Text (.rtf)
Microsoft Word (.doc)
Version 10, however, has additional export formats, including text/CSV, XML, and Lotus.
I don't know what front-end langauge you are using but the following is in C#. The code toggles off the toolbar export button in the CRViewer's Load event. The export and print to NotePad is accoplished
by clicking a command button 'Export Report' placed on the form that hosts your crystal reports viewer.
The code below but first I needed to drag a Process Component from the Toolbox to the form's component tray. Then:
******Code******
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// Add this using directive.
using System.Diagnostics;
namespace CrystalReportsTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Diagnostics.Process notePad;
private CrystalDecisions.Windows.Forms.CrystalReportViewer CrystalReportViewer1;
private System.Windows.Forms.Button cmdExportToNotePad;
private System.ComponentModel.Container components = null;
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;
private void cmdExportReportNotePad_Click(object sender, System.EventArgs e)
{
// Invoke the Exort report dialog. Be sure and save the text
// report to the My Documents directory. That is where the
// printing routine will look for it.
this.CrystalReportViewer1.ExportReport();
//Print exported ascii report in NotePad
PrintDoc();
}
public void PrintDoc()
{
Process notePad = new Process();
try
{
// Get the path that stores user documents. [My Documents]
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolde r.Personal);
// The exported file to be printed is assigned here.
notePad.StartInfo.FileName = myDocumentsPath + "\\TestNotePad.txt";
notePad.StartInfo.Verb = "Print";
notePad.StartInfo.CreateNoWindow = true;
notePad.Start(); // Process opens NotePad and Prints exported text report.
}
// Note that if your word processor generates exceptions
// such as this, they are handled first.
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
Console.WriteLine(e.Message +
". You do not have permission to print this file.");
}
}
}
private void CrystalReportViewer1_Load(object sender, System.EventArgs e)
{
// CrystalReportTest is the report to export to text
CrystalReportViewer1.ReportSource = new CrystalReportTest();
// Export tool bar button is removed prorammatically
// so that user's must use the programmed button.
CrystalReportViewer1.ShowExportButton = true;
}
}
}
|