2012-04-12 71 views
0

我需要以編程方式打開存儲在MOSS 2007共享文檔列表中的Excel文件。我想使用OleDbConnection,以便我可以將該文件的內容作爲DataTable返回。我相信這是可能的,因爲Web上的一些articles暗示這是可能的。OleDbConnection到MOSS 2007共享文檔中的Excel文件

初始化字符串的格式不符合規定規格從索引0

:該錯誤消息是,目前試圖初始化一個新的連接(oledbConn =新的OleDbConnection(_connStringName)時,我的代碼失敗我相信,我只是不能夠計算該文件的正確路徑這裏是我的代碼:

 public DataTable GetData(string fileName, string workSheetName, string filePath) 
    { 
// filePath == C:\inetpub\wwwroot\wss\VirtualDirectories\80\MySpWebAppName\Shared Documents\FY12_FHP_SPREADSHEET.xlsx 
      // Initialize global vars 
     _connStringName = DataSource.Conn_Excel(fileName, filePath).ToString(); 
     _workSheetName = workSheetName; 
     dt = new DataTable(); 
     //Create the connection object 
     if (!string.IsNullOrEmpty(_connStringName)) 
     { 
      SPSecurity.RunWithElevatedPrivileges(delegate() 
      { 
       oledbConn = new OleDbConnection(_connStringName); 

       try 
       { 

        oledbConn.Open(); 
        //Create OleDbCommand obj and select data from worksheet GrandTotals 
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + _workSheetName + ";", oledbConn); 

        //create new OleDbDataAdapter 
        OleDbDataAdapter oleda = new OleDbDataAdapter(); 
        oleda.SelectCommand = cmd; 
        oleda.Fill(dt); 
       } 
       catch (Exception ex) 
       { 
        System.Diagnostics.Debug.WriteLine(ex.Message); 
       } 
       finally 
       { 
        oledbConn.Close(); 
       } 
      }); 
     } 
     return dt; 
    } 

     public static OleDbConnection Conn_Excel(string ExcelFileName, string filePath) 
    { 
// filePath == C:\inetpub\wwwroot\wss\VirtualDirectories\80\MySpWebAppName\Shared Documents\FY12_FHP_SPREADSHEET.xlsx 
OleDbConnection myConn = new OleDbConnection(); 
     myConn.ConnectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0"); 
     return myConn; 
    } 

什麼我做錯了,或者是有沒有更好的辦法讓Excel文件內容作爲一個DataTable?

回答