问题描述
大家好,
我正在尝试更新excel文件。我使用OLEDB连接从excel读取数据,现在我想用它来更新数据。
我可以将数据从excel导入到datagridview。因此,如果无论如何都要将datagridview中的所有数据更新为excel,那就太棒了。我愿意接受任何建议。
这是我的代码:
Hello everyone,
I'm trying to update an excel file. I used OLEDB connection for read data from excel and now i want to use it for update the data.
I can import data from excel to datagridview. So if there is anyway to update all data from datagridview to excel, it would be great. I'm open for any suggestion.
Here is my code :
OleDbConnection baglan = new OleDbConnection();
baglan.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + yeniDosya + "; Extended Properties = 'Excel 8.0;IMEX=1;'";
baglan.Open();
string sit = sheetadi;
sit = sit.TrimEnd('\'');
sit = sit.TrimStart('\'');
string shtsrg = sit + "K" + i + ":K" + i;
string excel = String.Format("UPDATE {0} SET F1=" + dataGridView1.Rows[i].Cells[9].Value.ToString() + "", shtsrg);
MessageBox.Show(excel);
OleDbCommand cmd = new OleDbCommand(excel, baglan);
cmd.ExecuteNonQuery();
让我简单解释一下:
yenidosya:是excel文件名
sheetadi:是床单名称(我是从其他地方带的)
在sheetadi我有'开始和结束。所以我修剪它
i是我的列号,它可以改变参数
with shtsrg我想合并sheetname和列号(它也是我的表名)
我使用excel字符串制作表格参数
我正在尝试使用这种方法:
[]
但它给了我一个错误:
Let me explain simply :
yenidosya : is excel files name
sheetadi : is sheets name ( i am taking it from somewhere else )
In sheetadi i have ' at start and end. so i trim it
i is my column number and it can change parametric
with shtsrg i want to merge sheetname and column number ( it's also my table name )
I used excel string for make table name parametric
I'm trying to use this method :
How to update single cell of excel sheet using oledb connection, | The ASP.NET Forums[^]
But it gives me an error :
Syntax error in UPDATE statement
您对Syntax错误在哪里或哪个块有问题有什么建议吗?
我尝试过:
[]
推荐答案
这篇关于更新excel:UPDATE语句中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!