excel表格如何删除指定的表
private void btn_Select_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Excel文件|*.xls";//配置打开文件筛选器 openFileDialog1.Title = "选择Excel文件";//配置打开对话框标题 openFileDialog1.Multiselect = false;//配置打开对话框中只能单选 if (openFileDialog1.ShowDialog() == DialogResult.OK)//判定是否选择了文件 { txt_Path.Text = openFileDialog1.FileName;//在文本框中显示Excel文件名 CBoxBind();//对下拉列表举办数据绑定 } } private void btn_Delete_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();//实例化Excel工具 object missing = Missing.Value;//获取缺少的object范例值 //打开指定的Excel文件 Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(txt_Path.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[cbox_SheetName.Text]).Delete();//删除选择的事情表 MessageBox.Show("事情表删除乐成!"); excel.Application.DisplayAlerts = false;//不显示提示对话框 workbook.Save();//生存事情表 CBoxBind();//对下拉列表举办数据绑定 System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");//实例化历程工具 foreach (System.Diagnostics.Process p in excelProcess) p.Kill();//封锁历程 } private void CBoxBind()//对下拉列表举办数据绑定 { cbox_SheetName.Items.Clear();//清空下拉列表项 //毗连Excel数据库 OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txt_Path.Text + ";Extended Properties=Excel 8.0"); olecon.Open();//打开数据库毗连 System.Data.DataTable DTable = olecon.GetSchema("Tables");//实例化表工具 DataTableReader DTReader = new DataTableReader(DTable);//实例化表读取工具 while (DTReader.Read())//轮回读取 { cbox_SheetName.Items.Add(DTReader["Table_Name"].ToString().Replace('$',' ').Trim());//将事情表名添加到下拉列表中 } DTable = null;//清空表工具 DTReader = null;//清空表读取工具 olecon.Close();//封锁数据库毗连 cbox_SheetName.SelectedIndex = 0;//配置下拉列表默认选项为第一项 }//来自CodeGo.net。
,抖客教程网