c#
  • .net
  • excel
  • oledb
  • 2016-01-21 88 views 0 likes 
    0

    我正在盡我所能在Excel文件中插入一個新的數據行。 請看看。如何在excel中使用oledb插入新行c#.net

    try{ 
         string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\rising rent\\csharp-Excel.xls;Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;MAXSCANROWS=15;READONLY=FALSE;ImportMixedTypes=Text'"; 
         OleDbConnection conn = new OleDbConnection(ConnectionString); 
         conn.Open(); 
         OleDbCommand cmd = new OleDbCommand("INSERT INTO [Inventory$] (C_DATE) VALUES('555')",conn); 
         cmd.ExecuteNonQuery(); 
         conn.Close(); 
    
    } 
    catch (Exception ex) 
    { 
         MessageBox.Show(ex.ToString()); 
    } 
    

    錯誤是這樣的,請看看,並分享您的意見

    「System.Data.OleDb:我使用C#.NET框架(3.5)

    代碼面臨這個問題.OleDbException:操作必須使用一個更新的查詢在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult小時) 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,對象&的ExecuteReuslt)。在System.Data.OleDb.OleDbCommand.ExecuteCommandText(對象&的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(的CommandBehavior行爲,字符串方法) 在System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 在RisingRentACar .Inventory.button1_Click(對象發件人,EventArgs e)在C:\用戶\哈姆扎·哈菲茲\文檔\ Visual Studio的2015年\項目\ RisingRentACar \ RisingRentACar \ Inventory.cs:行82"

    回答

    1

    所以您的解決方案是關閉,我知道這已經過了四個月了,但是幫助別人。我遇到了同樣的問題,最終讓它工作。

    您不需要連接字符串中的全部內容。這是對我有用的東西。

    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
    + FileNameAndPath + ";Extended Properties=\"Excel 12.0 Xml; HDR=YES\";"; 
    

    "HDR=YES"意味着在第一行有標題細胞。您可以使用列名插入。

    其次,查詢必須圍繞列名具有[]。喜歡:

    command.CommandText = "Insert into [Sheet1$] ([ColumnName]) values('Value')"; 
    

    希望這可以幫助像我這樣的人誰看了這個帖子尋找答案這個問題。

    這裏是我的整體解決方案:

    private void InsertData(List<string> columnNames, List<string> theValues) 
    { 
    
        OleDbConnection connection = null; 
        OleDbCommand command = null; 
        string connectionString = ""; 
        string columns = ""; 
        string values = ""; 
    
        try 
        { 
    
         connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtDestination.Text + ";Extended Properties=\"Excel 12.0 Xml; HDR=YES\";"; 
    
    
         using (connection = new OleDbConnection(connectionString)) 
         { 
    
          connection.Open(); 
    
          for (int index = 0; index < columnNames.Count; index++) 
          { 
    
           columns += (index == 0) ? "[" + Regex.Replace(columnNames[index], @"\t|\n|\r", "\"") + "]" : ", [" + Regex.Replace(columnNames[index], @"\t|\n|\r", "\"") + "]"; 
           values += (index == 0) ? "'" + Regex.Replace(theValues[index], @"\t|\n|\r", "\"") + "'" : ", '" + Regex.Replace(theValues[index], @"\t|\n|\r", "") + "'"; 
    
          } 
    
          using (command = connection.CreateCommand()) 
          { 
    
           command.CommandText = string.Format("Insert into [Sheet1$] ({0}) values({1})", columns, values); 
    
           command.ExecuteNonQuery(); 
    
    
          } 
    
         } 
    
        } 
        catch (Exception ex) 
        { 
    
         ProcessError(ex); 
    
        } 
    
    } 
    
    相關問題