简体   繁体   中英

How to update excel sheet row from C#

I'm frustrated because i can't solve this problem...

Well, i has been making a desktop application in C#, i almost complete the application, But i have a big problem.

My application import an excel workbook who has some information that it is showed in a Gridview. It works ok, but my app update or modified the data that is showed in a gridview, only one column that is called Balance.

I want to update this column in my excel workbook, is like an update condition in SQL, for example I have this information that is imported to my app.

Excel 工作簿

If i modify in my app some row, for example the row that has the order "12345", i want to update in my excel workbook the column called Balance of that row.

I has been trying with this code:

    string order = textBox1.Text;
    string balance = textBox2.Text;

    filePath = openFileDialog1.FileName;
    extension = Path.GetExtension(filePath);
    header = "Yes";
    OleDbConnection MyConnection = new OleDbConnection(conStr); ;
    OleDbCommand myCommand = new OleDbCommand();
    string sql = null;
    MyConnection.Open();
    myCommand.Connection = MyConnection;
    sql = "UPDATE [" + sheetName + "] SET [" + sheetName + "].[Order]=123 WHERE [" + sheetName + "].[Balance]=12313";
    myCommand.CommandText = sql;
    myCommand.ExecuteNonQuery();
    MyConnection.Close();

And if i debug the code it is the sentence that is executed, and i think that it is ok, but it doesn't works.

UPDATE ['12234$'] SET ['12234$'].[Order]=123 WHERE ['12234$'].[Balance]=12313

It give me this error:

Message=No value given for one or more required parameters.
  Source=Microsoft Access Database Engine

Consider using an NuGet package to work with Excels instead. EPPlus is an excellent one.

using (var package = new ExcelPackage(@"someFile.xlsx")
{
   var sheet = package.Workbook.Worksheets.First();
   sheet.Cells["E2"].Value = 12345.32M;
   package.Save();
}

Sql is no longer possible, but you can use Linq:

using (var package = new ExcelPackage(@"someFile.xlsx")
{
   var sheet = package.Workbook.Worksheets.First();
   int rowIndex = sheet.Cells["C2:C5"].First(x => int.Parse(x.Value.ToString()) == 12345).Start.Row;
   const int balanceColumnIndex = 5;
   sheet.Cells[rowIndex, balanceColumnIndex].Value = 12313M;
   package.Save();
}

I wrote a series about EPPlus on my blog .

This is just a thought - but is your SQL-like string set up correctly?

If you're trying to update the balance for a certain order, you'd want that to be in your SET command.

So, something along the lines of...

UPDATE ['12234$'] SET ['12234$'].[Balance]=12313 WHERE ['12234$'].[Order]=12345

//try this solution

        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Directory.GetCurrentDirectory() + "/swtlist.xlsx;" +
                     @"Extended Properties='Excel 12.0;HDR=Yes;';Persist Security Info=False;";


        using (OleDbConnection connection = new OleDbConnection(connString))
        {
            connection.Open();
            try
            {
                DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                OleDbCommand cmd = new OleDbCommand("UPDATE  [Feuil1$]  SET d='yes' ", connection);

                  cmd.ExecuteNonQuery();
                connection.Close();


            }
            catch (Exception ex) { }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM