如何使用C#更新Excel文件中的Excist数据 [英] How Do I Update Excist Data In A Excel File Using C#

查看:90
本文介绍了如何使用C#更新Excel文件中的Excist数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一款可以更新excel表格数据的软件。



例子



my excel文件有一张名为Sheet1的表格

它包含



名为姓名,年龄,地址的主题

,在名称下,他们的名字叫做'Promod'。



i想要更改该名称并保存文件。



这是我的代码。





  private   void  button5_Click( object  sender,EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null ;
MyConnection = new System.Data.OleDb.OleDbConnection( provider = Microsoft.Jet.OLEDB.4.0;数据源='d:\\ enquiry1 / inquiry-Excel.xls';扩展属性= Excel 8.0;);
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = 更新[Sheet1 $] set name ='New Name'where name = promod;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

解决方案

set name ='New Name'where name = promod ;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}


您好



试试这个

使用C#使用Excel [ ^ ]


尝试



http://csharp.net-informations.com/excel / CSHARP-Excel的OLEDB-U pdate.htm [ ^ ]

i have developed a software which can update data on excel sheet.

example

my excel file has a sheet called "Sheet1"
and it contains

Topics called Names, age, address
and under name their is a name called 'Promod'.

i want to change that name and save the doc.

here is my code.


private void button5_Click(object sender, EventArgs e)
       {
           try
           {
               System.Data.OleDb.OleDbConnection MyConnection;
               System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
               string sql = null;
               MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='d:\\enquiry1/enquiry-Excel.xls';Extended Properties=Excel 8.0;");
               MyConnection.Open();
               myCommand.Connection = MyConnection;
               sql = "Update [Sheet1$] set name = 'New Name' where Name=promod";
               myCommand.CommandText = sql;
               myCommand.ExecuteNonQuery();
               MyConnection.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }

解决方案

set name = 'New Name' where Name=promod"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }


Hi

Try this
Working with Excel Using C#[^]


try

http://csharp.net-informations.com/excel/csharp-excel-oledb-update.htm[^]


这篇关于如何使用C#更新Excel文件中的Excist数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆