DataSet向excl导出数据,简单问题
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();
//以上代码编译出错,说是“少参数”
[解决办法]
最实在的解决办法:
DataSet.WriteXml(...);