【发布时间】:2011-05-19 12:46:06
【问题描述】:
我正在使用 drop table [SheetName$] 从 Excel 中删除工作表。
这只是清除工作表的数据,不删除工作表。
我尝试过使用 xls 和 xlsx。不适用于两个版本!
OleDbConnection connection = new OleDbConnection();
try
{
connection.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='drop.xlsx";
connection.Open();
OleDbCommand command = new OleDbCommand("Drop Table [MySheetName_1$]", connection);
command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
任何帮助/指针表示赞赏! 谢谢
【问题讨论】:
-
如果 Excel 通过 SQL 语法不支持,则直接使用 Excel API 从应用程序中删除工作表。您必须找到 C# 语法。
Function DeleteSheet(strSheetName As String) As Boolean ' This function deletes a sheet based on the individual name provided ' If the sheet is not found the code moves on. On Error Resume Next Application.DisplayAlerts = False PriorityExcel.Sheets(strSheetName).Delete Application.DisplayAlerts = True End Function -
您能解释一下为什么需要删除工作表吗?我试着想一个可能的场景,但找不到。也许有解决办法?其次,可以在服务器上安装第三方组件吗?
-
@Juliusz 您在使用第三方组件方面是正确的,但到目前为止,由于某些 x 原因,我不能使用第三方组件 bcoz。潜在的场景是:一个master excel有所有学生的记录。每个学生的数据到一张excel表中,数据太多,所以每个班级需要单独的表。现在我想导出几个学生。然后复制excel表并删除不需要的学生表。 ....我希望你明白这一点。