Net-informations.com
SiteMap  | About    

Deleting Excel WorkSheet using C#

Delete excel worksheets programmatically from C#

You can delete any worksheet from Microsoft Excel file. In order to delete a worksheet, you must add a reference to the Microsoft.Office.Interop.Excel assembly, and then you must use classes from that assembly to open a workbook and delete a worksheet.The following program shows how to delete worksheet from an an existing Excel file using C#.

Excel Library

To access the object model from Visual C# .NET, you have to add the Microsoft Excel 12.0 Object Library to you project. In the previous chapter you can see a step by step instruction on how to add Excel library to your project.

How to add Excel Library

Delete worksheet from an excel file

How to delete worksheet from an excel file from C#

In order to delete worksheet from excel file, this program open an existing Excel file and select the worksheet and then delete it.

Excel.Sheets worksheets = xlWorkBook.Worksheets; worksheets[1].Delete();

Delete Excel.Worksheet without prompts

Delete Excel.Worksheet without prompt from C#

DisplayAlerts set to False for suppress prompts and alert messages while a macro is running. When a message need a response from the end user, Microsoft Excel chooses the default response. After you complete the running process, Microsoft Excel sets this property to True, unless you are running cross-process code.

The following source code shows how to programmatically delete Worksheets from Workbooks.




using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                MessageBox.Show("Excel is not properly installed!!");
                return;
            }


            xlApp.DisplayAlerts = false; 
            string filePath = @"d:\test.xlsx";
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            Excel.Sheets worksheets = xlWorkBook.Worksheets;
            worksheets[1].Delete();
            xlWorkBook.Save();
            xlWorkBook.Close();

            releaseObject(worksheets);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Worksheet Deleted!");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.