string path;
OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("未选择文件");
return;
}
path= ofd.FileName;//得到路径和文件名
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+path+"; Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string tableName="";
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < dt.Rows.Count; i++)
{
tableName += dt.Rows[i][2].ToString().Trim();
} string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from ["+tableName+"] ";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet dataSet1 = new DataSet();
myCommand.Fill(dataSet1, "["+tableName+"]");
//以上代码经测试,通过
//以下代码未通过,我想实现最简单的将dataset的修改内容保存入excel文件
OleDbCommand dbcommand = new OleDbCommand();
dbcommand.Connection=conn;
dbcommand.CommandText = "update [" + tableName + "] set F10='aaa' where f1=1";
dbcommand.ExecuteNonQuery();
conn.Close();
//以上代码编译出错,说是“少参数”
OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("未选择文件");
return;
}
path= ofd.FileName;//得到路径和文件名
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+path+"; Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string tableName="";
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < dt.Rows.Count; i++)
{
tableName += dt.Rows[i][2].ToString().Trim();
} string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from ["+tableName+"] ";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet dataSet1 = new DataSet();
myCommand.Fill(dataSet1, "["+tableName+"]");
//以上代码经测试,通过
//以下代码未通过,我想实现最简单的将dataset的修改内容保存入excel文件
OleDbCommand dbcommand = new OleDbCommand();
dbcommand.Connection=conn;
dbcommand.CommandText = "update [" + tableName + "] set F10='aaa' where f1=1";
dbcommand.ExecuteNonQuery();
conn.Close();
//以上代码编译出错,说是“少参数”
解决方案 »
- 新手问【Timeout expired】问题
- 如何改变位图的dpi和大小,我的代码总是异常报出(GDI+)
- C#应用程序中关于MessageBox.Show的问题
- Microsoft.VisualStudio.SourceSafe.Interop.dll中的get_VSSItem方法用法
- 【两百分】两个没有得到答案的简单帖子,现在高分求答案!!!!
- 求个图书查询系统,急用,给100分,谢谢!!
- C#如何设置导出的Excel表里面一列的宽度?????
- 请问各位,做一个类似msn的工具大致需要多少时间...
- 急!!C#中如何运行其它应用程序比如记事本,像VB中的Shell函数。
- TabControl控件数据联动问题?
- 手机客户端运行Socket程序时,无法得到服务器返回值
- 关于读INI文件
string path;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("未选择文件");
return;
}
path= ofd.FileName;//得到路径和文件名
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+path+";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string tableName="";
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < dt.Rows.Count; i++)
{
tableName += dt.Rows[i][2].ToString().Trim();
}
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from ["+tableName+"] ";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet dataSet1 = new DataSet();
myCommand.Fill(dataSet1, "["+tableName+"]"); 至此已将EXCEL文件数据读取到DataSet中
请高手给出,修改某条记录并且保存回原文件的代码,谢谢了,我要用ADO.net的方式做