c#excel插入检查工作表是否存在

using (DbCommand command = oconn.CreateCommand()) { command.CommandText = "CREATE TABLE [Sheet2$] (F1 number, F2 char(255), F3 char(128))"; command.ExecuteNonQuery(); for (int i = 1; i <= 20; i++) { //now we insert the values into the existing sheet...no new sheet is added. command.CommandText = "INSERT INTO [Sheet2$] (F1, F2, F3) VALUES(1,\"Fake Record\",\"Fake Record\")"; command.ExecuteNonQuery(); } } 

我正在使用此代码在Excel文件表中插入一些logging。 我想要做的是看看例如Sheet1是否存在,然后创build工作表2并在那里插入这些logging。 如果它只find工作表3,那么我想要创build另一个工作表4,并在那里插入logging等。 我怎样才能做到这一点?

尝试检查数据模式的表格:

 DataTable dtSchema = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); DataRow[] dr = dtSchema.Select("TABLE_NAME = 'Sheet1$'"); bool exist = dr == null || dr.Length == 0; 

我们可以在这里find相同的解决scheme: https : //mudassarkhan.wordpress.com/tag/c-excel/