CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Location
    Lahore, Pakistan.
    Posts
    10

    Open Excel file, change it and save it

    I need to do the following:
    -> open Excel file
    -> change values of required cells
    -> then save the file

    right now I am doing this:
    Code:
    Excel.Application xlAppoutput = null;
    Excel.Workbook xlWorkBookoutput = null;
    Excel.Worksheet xlWorkSheetoutput = null;
    Excel.Range rangeoutput = null;
    try
                    {  
                        xlAppoutput = new Excel.ApplicationClass();
                        xlWorkBookoutput = xlAppoutput.Workbooks.Open(@"D:\Askari9\output\myFile.xls", Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                        xlWorkSheetoutput = (Excel.Worksheet)xlWorkBookoutput.Worksheets.get_Item(1);
                        rangeoutput = xlWorkSheetoutput.UsedRange;
    
                        //(rangeoutput.Cells[12, 3] as Excel.Range).Value2 = "new value";
    
                        xlWorkBookoutput.Close(true, null, null);
                        xlAppoutput.Quit();
    
                        releaseObject(xlWorkSheetoutput);
                        releaseObject(xlWorkBookoutput);
                        releaseObject(xlAppoutput);
                    }catch(Exception ex){
                        System.Console.Write(ex.StackTrace);
                        System.Console.Write("System could not successfully generate bill for House#" + Reader.GetValue(houseNoFieldIndexInDB).ToString()+"\n");
                        releaseObject(xlWorkSheetoutput);
                        releaseObject(xlWorkBookoutput);
                        releaseObject(xlAppoutput);
                    }

    I get an exception on 'xlAppoutput.Quit();'


    Can anyone please help me fix this? or tell me any other way of doing the same job (open excel file, change it then save it)

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Open Excel file, change it and save it

    Quote Originally Posted by adi.shoukat View Post
    I get an exception on 'xlAppoutput.Quit();'
    You'll need to provide some more detail there
    It's not a bug, it's a feature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured