2010-03-09 6 views
0

아래 코드는 내 C# 응용 프로그램에서 Excel 파일을 열려고했지만 프로그램에서 "excel.xls에 액세스 할 수 없습니다"라는 오류 메시지가 나타납니다.하지만 작동 내 문자열 경로 변수에 파일 경로를 지정, 문제는 내가 OpenFileDialog를에서 파일 경로를 얻을 필요가있다.C에서 Excel 파일을 열 수 없습니다. #

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

InitializeConnection을 호출하는 코드, 특히 파일 대화 코드를 포함시킵니다. –

+0

개인 무효 btnBrowse_Click_1 (개체 발신자, EventArgs e) { openFileDialog1.InitialDirectory = "c : /"; openFileDialog1.Filter = "xls 파일 (* .xls) | * .xls | 모든 파일 (*. *) | *. *"; openFileDialog1.FileName = ""; openFileDialog1.ShowDialog(); openFileDialog1.CheckFileExists = true; txtpath.Text = openFileDialog1.FileName; if (openFileDialog1.FileName! = "") { excel.InitializeConnection (txtpath.Text); } } –

답변

관련 문제