Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:26:23+00:00 2026-05-13T21:26:23+00:00

Below is my code, i tried to open my excel file in my c#

  • 0

Below is my code, i tried to open my excel file in my c# application but the program give’s me an error message “Cannot Access “my excel.xls”. But when I specify the file path in my string path variable it works, the problem is I need to get the file path from an openFileDialog.

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Reflection;
using MOIE = Microsoft.Office.Interop.Excel;
using OFFICE = Microsoft.Office.Core;

namespace EmpUploader
{
   public class ExcelCon
    {
       private OleDbDataReader reader = null;
       private OleDbCommand excelCommand = new OleDbCommand();
       private OleDbDataAdapter adapter = new OleDbDataAdapter();
       private DataTable excelData = new DataTable();
       private MOIE.ApplicationClass objExcel = new MOIE.ApplicationClass();
       private MOIE.Workbook wb = null;
       private string myConn = "";
       private string strSQL = "";
       private string err = "";
       private string path2 = "";
       private int sheetCount = 0;
       private OleDbConnection Con = new OleDbConnection("");

 #region "excel interop prarameters"
       private static object xl_missing = Type.Missing;
       private static object xl_true = true;
       private static object xl_false = false;


       private object xl_update_links = xl_missing;
       private object xl_read_only = xl_missing;
       private object xl_format = xl_missing;
       private object xl_password = xl_missing;
       private object xl_write_res_password = xl_missing;
       private object xl_ignore_read_only = xl_missing;
       private object xl_origin = xl_missing;
       private object xl_delimiter = xl_missing;
       private object xl_editable = xl_missing;
       private object xl_notify = xl_missing;
       private object xl_converter = xl_missing;
       private object xl_add_to_mru = xl_missing;
       private object xl_local = xl_missing;
       private object xl_corrupt_load = xl_missing;
#endregion
  }
//MY CODE FOR OPENING THE EXCEL
//note that my file path came from an openfiledialog
  public void InitializeConnection(string path)
       {


               //connection string for excel
               myConn = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + ";      Extended Properties =Excel 8.0";
               Con.ConnectionString = myConn;
               Con.Open();

               //this is the sample specified path that worked when i test my application
               //path = @"C:\shinetsu p5 emp list.xls";
               objExcel.Visible = false;
               wb = objExcel.Workbooks.Open(path, xl_update_links, xl_read_only, xl_format, xl_password, xl_write_res_password, xl_ignore_read_only, xl_origin, xl_delimiter, xl_editable, xl_notify, xl_converter, xl_add_to_mru, xl_local, xl_corrupt_load);

           sheetCount = wb.Worksheets.Count;



       }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:26:23+00:00Added an answer on May 13, 2026 at 9:26 pm

    The OpenFileDialog class has a property “FileName” which contains the full name of the file (Path + Filename). So just use that property as your “path” variable.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog_members.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code below I tried this call but it didnt work... even
The below code works as far as I can tell except for the var
Hello, I have 3 Buttons add,delete,open as RelayCommands in my DocumentViewModel . Below you
I'm reading an article from this website , but when i run the code
When I opened a VC6 project in VS2008 and tried building it , initially
I am still learning to do javascript and django and yesterday I tried to
Is there any replacement for z-index? My code is too long, thus I will
Hi I'm attempting to use the ASPFPDF class to output an image on a
I'm currently trying to find a PDF library which will run without a running
My VB6 project requires keyboard short-cuts for toolbar control in its MDI Form. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.