well, other solution mentioned here did not work for me. I got this solution.
Step 1: Create a copy of the source file (i.e. TempFile )
Step 2: Copy desired sheet of source file to TempFile
Step 3: Delete the source file
Step 4: Rename TempFile to Source File.
Note: You will need the "Microsoft.Office.Interop.Excel" package from
Nuget for this solution. Also, add using Excel = Microsoft.Office.Interop.Excel;
static void Main(string[] args)
{
Excel.Application excelApp;
string sourceFileName = "Original.xlsx"; //Source excel file
string tempFileName = "temp.xlsx";
string folderPath = @"C:\FodlerPath\";
string sourceFilePath = System.IO.Path.Combine(folderPath, sourceFileName);
string destinationFilePath = System.IO.Path.Combine(folderPath, tempFileName);
System.IO.File.Copy(sourceFilePath,destinationFilePath,true);
/************************************************************************************/
excelApp = new Excel.Application();
Excel.Workbook wbSource, wbTarget;
Excel.Worksheet currentSheet;
wbSource = excelApp.Workbooks.Open(sourceFilePath);
wbTarget = excelApp.Workbooks.Open(destinationFilePath);
currentSheet = wbSource.Worksheets["Sheet1"]; //Sheet which you want to copy
currentSheet.Name = "TempSheet"; //Give a name to destination sheet
currentSheet.Copy(wbTarget.Worksheets[1]); //Actual copy
wbSource.Close(false);
wbTarget.Close(true);
excelApp.Quit();
System.IO.File.Delete(sourceFilePath);
System.IO.File.Move(destinationFilePath, sourceFilePath);
}