简体   繁体   English

OLEDB for EXCEL-删除表[SheetName $]-不删除工作表

[英]OLEDB for EXCEL - Drop Table [SheetName$] - Doen't Delete Sheet

I am using drop table [SheetName$] to delete a worksheet from excel. 我正在使用放置表[SheetName $]从Excel中删除工作表。

This just clears the data of the sheet but does not delete the sheet. 这只会清除工作表中的数据,但不会删除工作表。

I have tried using xls and xlsx. 我曾尝试使用xls和xlsx。 Doesn't work with both versions ! 不适用于两个版本!

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();
}

Any Help / Pointers appreciated ! 任何帮助/指针表示赞赏! Thanks 谢谢

Unfortunately, you cannot delete a worksheet using ADO.NET for Excel. 不幸的是,您不能使用ADO.NET for Excel删除工作表。 Instead, you will need to use the Excel Interop to perform this task. 相反,您将需要使用Excel Interop来执行此任务。 The basic code for the actual DELETE statement would look something like this: 实际的DELETE语句的基本代码如下所示:

using MSExcel = Microsoft.Office.Interop.Excel;

private MSExcel._Application excel;
private MSExcel._Workbook workbook;
private MSExcel._Worksheet worksheet;
private MSExcel.Sheets sheet;

Excelapp.DisplayAlerts = false;
((Excel.Worksheet)workBook.Worksheets[3]).Delete();
Excelapp.DisplayAlerts = true;

This is the basic rundown of how it would look. 这是外观的基本概要。 The DisplayAlerts lines are to fix an issue some people had with deleting a sheet. DisplayAlerts行用于解决某些人在删除工作表时遇到的问题。 Also note that you cannot delete the last sheet in the Excel file . 还要注意, 您不能删除Excel文件中的最后一张纸 That issue will get you if you don't watch it. 如果您不看的话,这个问题会让您受益。

Here are some links to help you out: 以下是一些可以帮助您的链接:

MSDN on deleting sheeting in Excel MSDN有关在Excel中删除工作表

Post discussing the possibility of using ADO.NET to DROP a sheet in Excel 讨论如何使用ADO.NET在Excel中删除工作表的可能性

SO question about deleting a sheet in Excel using the Interop 关于使用Interop在Excel中删除工作表的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM