我知道这里已经问过几次这个问题,但是我遵循了建议,但仍然出现错误:External table is not in expected format.
我的应用程序具有DataGrid,可以正确显示excel文件的内容,当我刷新DataGrid时,它会在excel文件中显示添加的行。但是,当我打开excel文件以检查是否添加了行时,它不是-不会保存新行。 / p>
感谢您的帮助。
我的Wpf应用程序在App.config中具有一个连接字符串:
add name="ExcelConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=excel_file\\events2.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES""
我正在使用OLEDB连接excel文件:
public Events()
{
InitializeComponent();
System.Data.OleDb.OleDbConnection excelConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
var connectionString = ConfigurationManager.ConnectionStrings["ExcelConnectionString"].ToString();
excelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
try
{
String sql = null;
excelConnection.Open();
myCommand.Connection = excelConnection;
sql = "Select * from [events$]";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
System.Data.OleDb.OleDbDataAdapter dataAdp = new System.Data.OleDb.OleDbDataAdapter(myCommand);
DataTable dt = new DataTable("events");
dataAdp.Fill(dt);
dgData.ItemsSource = dt.DefaultView;
dataAdp.Update(dt);
excelConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
excelConnection.Close();
}
}