Avatar of RockBaby
RockBabyFlag for Singapore

asked on 

StreamWriter

Hi,
I'm trying to create a save dialog and convert to csv format. When i click cancel button, it prompt error. how do i solve it?
and in the dialog, there's a section call "save as". How do i type in .csv text in there?

below my code:
private void btnCSV_Click(object sender, System.EventArgs e)
{
      string fileName = "";
      string fileNameDel = "";
      string fileNameExt = "";
      CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument( );
      report = this.crystalReportViewer1.ReportSource as CrystalDecisions.CrystalReports.Engine.ReportDocument;
      if (report != null)
      {
            SaveFileDialog SDialog = new SaveFileDialog();
            SDialog.DefaultExt = "xls";
            if (SDialog.ShowDialog() == DialogResult.OK)
            {                    
                  report.ExportToDisk(ExportFormatType.Excel, SDialog.FileName);
                  fileName = SDialog.FileName;
                  fileNameDel = fileName;
                  fileNameExt = fileName;
            }

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = false;
            excelApp.DisplayAlerts = false;
            Microsoft.Office.Interop.Excel.Workbook excelWk = excelApp.Workbooks.Open(fileName,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing);

            fileName = Path.ChangeExtension(fileName, ".csv");
            excelApp.ActiveWorkbook.SaveAs
                  (fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excelApp.ActiveWorkbook.Save();
            excelApp.Quit();
            fileNameExt = Path.GetExtension(fileNameExt);

            if(fileNameExt == ".xls")
            {
                  File.Delete(fileNameDel);
            }
      }
}

Error:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in HARTS_RPT.exe
Additional information: '' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.
C#

Avatar of undefined
Last Comment
RockBaby
Avatar of RockBaby
RockBaby
Flag of Singapore image

ASKER

ok.. i manage to solve the error..
now my question is how to set a text(.csv) in the "save as" box?
Avatar of Member_2_3737922
Member_2_3737922

Hi RockBaby! Rizwan here again. I could not understand your question. Where do you want to set the text?

Also to solve the error put the while code inside the "if (SDialog.ShowDialog() == DialogResult.OK)" as this:
private void btnCSV_Click(object sender, System.EventArgs e)
{
      string fileName = "";
      string fileNameDel = "";
      string fileNameExt = "";
      CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument( );
      report = this.crystalReportViewer1.ReportSource as CrystalDecisions.CrystalReports.Engine.ReportDocument;
      if (report != null)
      {
            SaveFileDialog SDialog = new SaveFileDialog();
            SDialog.DefaultExt = "xls";
            if (SDialog.ShowDialog() == DialogResult.OK)
            {                    
                  report.ExportToDisk(ExportFormatType.Excel, SDialog.FileName);
                  fileName = SDialog.FileName;
                  fileNameDel = fileName;
                  fileNameExt = fileName;
           

               Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = false;
            excelApp.DisplayAlerts = false;
            Microsoft.Office.Interop.Excel.Workbook excelWk = excelApp.Workbooks.Open(fileName,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing);

            fileName = Path.ChangeExtension(fileName, ".csv");
            excelApp.ActiveWorkbook.SaveAs
                  (fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excelApp.ActiveWorkbook.Save();
            excelApp.Quit();
            fileNameExt = Path.GetExtension(fileNameExt);

            if(fileNameExt == ".xls")
            {
                  File.Delete(fileNameDel);
            }
      }
   }
}
Avatar of RockBaby
RockBaby
Flag of Singapore image

ASKER

i have already solve the problem..
but now i want it like..
when the save dialog box pop up.. u need to type in ur file name rite.. then below there's another edit box as "Save As". now in there is empty. i wan to add an extension to it..
ASKER CERTIFIED SOLUTION
Avatar of Member_2_3737922
Member_2_3737922

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Do this before "if(SDialog.show() == DialogResult.OK)" line
Avatar of RockBaby
RockBaby
Flag of Singapore image

ASKER

yeah... thanks man!
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo