locked
Problem with Excel Dcom objects RRS feed

  • Question

  • User-476194302 posted

    Hey fellows,

    I am using Microsoft Excel Interop (DCom) object in my ASP.NET project, everything works out well but the problem is that everytime an Excel file is Created a new Excel Process is started in the memory so the memory consumption goes high. Can anyone tell me the solution to it.

    Thursday, December 25, 2008 1:57 AM

Answers

  • User-1256393885 posted

    This code may help you.

    object m_objOpt = System.Reflection.Missing.Value;

    string file = HttpContext.Current.Server.MapPath(".") + "\\test.xls";if (File.Exists(file))

    {

    File.Delete(file);

    }

    Excel.Workbook wb;

    Excel.Worksheet ws;

    Excel.Application ex = new Excel.Application();

    wb = (Excel.Workbook)(ex.Workbooks.Add(m_objOpt));

    ws = (Excel.Worksheet)(wb.Sheets["Sheet1"]); /* Here u can assign sheets from previous excel files */ws.Name = "Your File Name to write";

    wb.SaveCopyAs(file);

    wb.Close(null, null, null);

     

     

     

    Bye

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 26, 2008 1:10 AM

All replies

  • User-1256393885 posted
    Hi After creating the files the system is getting hang while releasing the COM object.The solution for this is to do not release the com object.it doesn't affect anything.it has been worked for me. Bye
    Thursday, December 25, 2008 2:57 AM
  • User-476194302 posted

    I am not releasing the resources but the problem is that it makes multiple instances of MS EXCEL in the memory pool and the server gets slow

    Thursday, December 25, 2008 3:11 AM
  • User-1256393885 posted
    Will it be possible for you to show the source code bye
    Thursday, December 25, 2008 10:30 PM
  • User-476194302 posted

    Sure Here is the Code, this is what I am doing Initialization till the end

     

    Excel.Application m_objExcel = null;
    Excel.Workbooks m_objBooks = null;
    Excel._Workbook m_objBook = null;
    Excel.Sheets m_objSheets = null;
    Excel._Worksheet m_invsheet = null;
    Excel._Worksheet m_inssheet = null;
    
    // Start a new workbook in Excel.
    m_objExcel = new Excel.Application();
    m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
    m_objBook = (Excel._Workbook)m_objBooks.Open(APP_PATH + "template/invoice3.xls", Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    
    m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
    /*
    After all the Processing
    */
    m_objBooks.Close();
    
    m_objBooks = null;
    m_objBook = null;
    m_objSheets = null;
    m_objSheet = null;
    m_objExcel.Quit();
    m_objExcel = null;
    Thursday, December 25, 2008 10:49 PM
  • User-1256393885 posted
    This is the code where u reading the file.But the new excel process is creating while writing the excel file,so plz send the code of writing the file. -Bye
    Thursday, December 25, 2008 11:16 PM
  • User-476194302 posted

    You mean this one  

    //Save the EDITED workbook as a new file
    private string fileNameWithPath = "Invoice-AB110.xls";
    private object m_objOpt = System.Reflection.Missing.Value;
    m_objBook.SaveAs(fileNameWithPath, m_objOpt, m_objOpt,
                m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
     Let me know if this is what you are asking for
    Thursday, December 25, 2008 11:29 PM
  • User-1256393885 posted

    This code may help you.

    object m_objOpt = System.Reflection.Missing.Value;

    string file = HttpContext.Current.Server.MapPath(".") + "\\test.xls";if (File.Exists(file))

    {

    File.Delete(file);

    }

    Excel.Workbook wb;

    Excel.Worksheet ws;

    Excel.Application ex = new Excel.Application();

    wb = (Excel.Workbook)(ex.Workbooks.Add(m_objOpt));

    ws = (Excel.Worksheet)(wb.Sheets["Sheet1"]); /* Here u can assign sheets from previous excel files */ws.Name = "Your File Name to write";

    wb.SaveCopyAs(file);

    wb.Close(null, null, null);

     

     

     

    Bye

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 26, 2008 1:10 AM