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; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)