468,114 Members | 2,095 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

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
1 Week Ago #1
1 2891
SioSio
238 128KB
Expand|Select|Wrap|Line Numbers
  1. using Excel = Microsoft.Office.Interop.Excel;
Precautions when using COM references.
To work with "Microsoft.Office.Interop.Excel", 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.ReleaseComO bject() must be executed, so if the code is not written correctly, the COM object is not release.
Expand|Select|Wrap|Line Numbers
  1. void MSExcelColumnDeleteSample()
  2. {
  3.     Excel.Application mExcel; // Excel Application Object
  4.     mExcel = new Excel.Application();
  5.     try
  6.     {
  7.         Excel.Workbook mWorkbook;// Workbook Object
  8.         string excelName = System.AppDomain.CurrentDomain.BaseDirectory + "\\sample.xlsx";
  9.         mExcel.Visible =true;
  10.         mWorkbook = (Excel.Workbook)(mExcel.Workbooks.Open(
  11.             excelName,
  12.             Type.Missing,
  13.             Type.Missing,
  14.             Type.Missing,
  15.             Type.Missing,
  16.             Type.Missing,
  17.             Type.Missing,
  18.             Type.Missing,
  19.             Type.Missing,
  20.             Type.Missing,
  21.             Type.Missing,
  22.             Type.Missing,
  23.             Type.Missing,
  24.             Type.Missing,
  25.             Type.Missing 
  26.         ));
  27.         try
  28.         {
  29.             string sheetName = "Sheet1";
  30.             Excel.Worksheet SheetSample; // Worksheet Object
  31.             SheetSample = (Excel.Worksheet)mWorkbook.Sheets[
  32.                 getSheetIndex(sheetName, mWorkbook.Sheets)];
  33.             try
  34.             {
  35.                 // Column Delete
  36.                 Excel.Range ExcelRange = SheetSample.Range["F:F"];
  37.                 try
  38.                 {
  39.                     ExcelRange.Delete(Type.Missing);
  40.                 }
  41.                 finally
  42.                 {
  43.                     if (null != ExcelRange)
  44.                     {
  45.                         System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelRange);
  46.                     }
  47.                 }
  48.                 // Save
  49.                 mWorkbook.Save();
  50.                 }
  51.             finally
  52.             { 
  53.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(SheetSample);
  54.             }
  55.         }
  56.         finally
  57.         {
  58.             // Close
  59.             mWorkbook.Close(false);
  60.             System.Runtime.InteropServices.Marshal.ReleaseComObject(mWorkbook);
  61.         }
  62.     }
  63.     finally
  64.     {
  65.         if (null != mExcel)
  66.         {
  67.             try
  68.             {
  69.                 mExcel.DisplayAlerts = false;
  70.                 // Quit
  71.                 mExcel.Quit();
  72.             }
  73.             finally
  74.             {
  75.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(mExcel);
  76.             }
  77.         }
  78.     }
  79. }
Expand|Select|Wrap|Line Numbers
  1. // Method that returns the index of the specified worksheet name
  2. private int getSheetIndex(string sheetName, Excel.Sheets shs)
  3. {
  4.     int i = 0;
  5.     foreach (Excel.Worksheet sh in shs)
  6.     {
  7.         if (sheetName == sh.Name)
  8.         {
  9.             return i + 1;
  10.         }
  11.         i += 1;
  12.     }
  13.     return 0;
  14. }
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by =?Utf-8?B?Sm9zaW4gSm9obg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.