opening an exel file in a c# program

This is a discussion on opening an exel file in a c# program within the C# Programming forums, part of the General Programming Boards category; Hi I am getting the following error while opening the exel file. I am sure the path and the file ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    177

    opening an exel file in a c# program

    Hi

    I am getting the following error while opening the exel file. I am sure the path and the file name is correct.
    Code:
    'G:\myCodeProgram\swiming\beforeMatch\Files\Input\arrange.xlsx' 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, make sure that the file has not been renamed, moved, or deleted.
    
    System.Runtime.InteropServices.COMException was unhandled
      HelpLink="C:\\Program Files (x86)\\Microsoft Office\\Office12\\1033\\XLMAIN11.CHM"
      Message="'G:\\myCodeProgram\\swiming\\beforeMatch\\Files\\Input\arrange.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted."
      Source="Microsoft Office Excel"
      ErrorCode=-2146827284
      StackTrace:
           at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
           at matchProvider.mainInfo.button1_Click(Object sender, EventArgs e) in G:\myCodeProgram\swiming\beforeMatch\1\matchProvider\matchProvider\mainInfo.cs:line 116
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at matchProvider.Program.Main() in G:\myCodeProgram\swiming\beforeMatch\1\matchProvider\matchProvider\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    I use visual Studio 2008 , win7 (86X)

    and here is the code
    Code:
    object opt = System.Reflection.Missing.Value;
                    Excel.Application excel = new Excel.ApplicationClass();
                    Excel.Sheets Sheets;
                    Excel.Workbook wArrange;
    
                    wArrange = excel.Workbooks.Open(path+"\arrange.xlsx", opt, false, opt, opt, opt, true, opt, opt, opt, opt, opt, opt, opt, opt);
                    Sheets = wArrange.Worksheets;
                    teamNum = wArrange.Sheets.Count;
    
                    wArrange.Close(true, opt, opt);

    Can you plz tell me what is wrong?

    Tnx
    arian

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    path + "\\arrange.xlsx"
    EDIT: Look at the original error message more closely. It has \\ except for the last one which has single.
    Last edited by C_ntua; 08-13-2010 at 06:02 PM.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    What a big/stupid mistake I have done!

    Thanks a lot for your help

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    What a big/stupid mistake I have done!
    Nah. Just an oversight. It happens to all of us.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You can also use the @"path1\path2" syntax, which escapes characters within the string automatically. Or use Path.Combine()
    Here to Deceive, Inveigle, Obfuscate Since 1945

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by indigo0086 View Post
    Or use Path.Combine()
    QFT. I got tired of making the same error as the OP; I always use Path.Combine() now. The @"" syntax is fine if you have a hardcoded path, but if you've got to piece parts together, Path.Combine() is the way to go.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 11:31 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21