Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

Below is the method I used to open Excel sheet and save it as CSV file. In Windows 7, Windows Server 2008 R2, the excel process/es is automatically killing itself after processing the excel file/s. In windows Server 2008 Standard Service Pack1, Its going on creating a lot of Excel processes and showing the message 'Microsoft Excel Error - Not enough system resources to display completely.'

Almost near to 200 Excel processes are at Task Manager without killing at that instant and server is hanged./

Below is the code is used to process lot of Excel files.

private static bool SaveExcelCVS(string fileName, string outFile)
        {
            bool success = false;

            try
            {
                Excel.Application xApp = new Excel.Application();
                Excel.Workbook xF1 = xApp.Workbooks.Open(fileName,
                    0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
                xF1.SaveAs(outFile, Excel.XlFileFormat.xlCSV, "", "", false, false,
                Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                false, false, false, false);
                xF1.Close(false);
                xF1 = null;
                xApp.Quit();
                xApp = null;
                success = true;
            }
            catch
            {
            }
            return success;
        }
Posted

Try the following code :

C#
private static bool SaveExcelCVS(string fileName, string outFile)
        {
            bool success = false;
            Excel.Application xApp = new Excel.Application(); 
            try
            {

                Excel.Workbook xF1 = xApp.Workbooks.Open(fileName,
                    0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
                xF1.SaveAs(outFile, Excel.XlFileFormat.xlCSV, "", "", false, false,
                Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                false, false, false, false);
                xF1.Close(false);
                xF1 = null;

                success = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            finally
            {
                xApp.Quit();
                xApp = null;            
            }
            return success;
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Oct-11 18:11pm    
This thing makes little sense: MessageBox.Show("" + ex). You probably meant something like
string.Format(@"{0}: ""{1}""", ex.GetType().Name, ex.Message).
Also, I recommend: never use "", use string.Empty.
--SA
Mehdi Gholam 10-Oct-11 22:33pm    
It's shorthand and shows everything even the stack trace.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900