Deleting a column present in excel sheet using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajat45
    New Member
    • Mar 2021
    • 1

    Deleting a column present in excel sheet using c#

    Hi,
    can anyone provide me the full code to delete a column present in the excel sheet. (Please let me know the type of project I have to create)
    Kindly help me .

    Regards,
    Rajat
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    Code:
    using Excel = Microsoft.Office.Interop.Excel;
    Precautions when using COM references.
    To work with "Microsoft.Offi ce.Interop.Exce l", you need to have Excel installed.
    For COM references, they will not be released automatically unless you declare "I will not use this Excel anymore".
    System.Runtime. InteropServices .Marshal.Releas eComObject() must be executed, so if the code is not written correctly, the COM object is not release.
    Code:
    void MSExcelColumnDeleteSample()
    {
    	Excel.Application mExcel; // Excel Application Object
    	mExcel = new Excel.Application();
    	try
    	{
    		Excel.Workbook mWorkbook;// Workbook Object
    		string excelName = System.AppDomain.CurrentDomain.BaseDirectory + "\\sample.xlsx";
    		mExcel.Visible =true;
    		mWorkbook = (Excel.Workbook)(mExcel.Workbooks.Open(
    			excelName,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing,
    			Type.Missing 
    		));
    		try
    		{
    			string sheetName = "Sheet1";
    			Excel.Worksheet SheetSample; // Worksheet Object
    			SheetSample = (Excel.Worksheet)mWorkbook.Sheets[
    				getSheetIndex(sheetName, mWorkbook.Sheets)];
    			try
    			{
    				// Column Delete
    				Excel.Range ExcelRange = SheetSample.Range["F:F"];
    				try
    				{
    					ExcelRange.Delete(Type.Missing);
    				}
    				finally
    				{
    					if (null != ExcelRange)
    					{
    						System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelRange);
    					}
    				}
    				// Save
    				mWorkbook.Save();
    				}
    			finally
    			{ 
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(SheetSample);
    			}
    		}
    		finally
    		{
    			// Close
    			mWorkbook.Close(false);
    			System.Runtime.InteropServices.Marshal.ReleaseComObject(mWorkbook);
    		}
    	}
    	finally
    	{
    		if (null != mExcel)
    		{
    			try
    			{
    				mExcel.DisplayAlerts = false;
    				// Quit
    				mExcel.Quit();
    			}
    			finally
    			{
    				System.Runtime.InteropServices.Marshal.ReleaseComObject(mExcel);
    			}
    		}
    	}
    }
    Code:
    // Method that returns the index of the specified worksheet name
    private int getSheetIndex(string sheetName, Excel.Sheets shs)
    {
    	int i = 0;
    	foreach (Excel.Worksheet sh in shs)
    	{
    		if (sheetName == sh.Name)
    		{
    			return i + 1;
    		}
    		i += 1;
    	}
    	return 0;
    }

    Comment

    Working...