Home » excel » c# – How to name a new Excel sheet I made and place it at the end of all the sheets?

c# – How to name a new Excel sheet I made and place it at the end of all the sheets?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I figured out how to make a new Excel sheet in C#.
And I do it this way:

private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
appExcel.Worksheets.Add();

But I have no idea how to give a name to the Excel sheet I made, nor how to place it at the end instead of the beginning.

I tried this:

sheet1 = appExcel.Worksheets.Add();
sheet1.name = "test";

But this didn’t work.
I also looked in the Add function, but I can NOT fill the name in this way: appExcel.Worksheets.Add("test");

My code:

excel_init("C:\Users\me\Desktop\excel2.xlsx");

private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
        private static Workbook newWorkbook_First = null;
        private static _Worksheet objsheet = null;

static void excel_init(String path)
{
    appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

    if (System.IO.File.Exists(path))
    {
        // then go and load this into excel
        newWorkbook_First = appExcel.Workbooks.Open(path, true, true,5); // does NOT make 5 excel sheet???
        objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
    }
    else
    {
        try
        {
            appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            appExcel.Visible = true;
            newWorkbook_First = appExcel.Workbooks.Add(1);
            objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook_First.Sheets[1];
        }
        catch (Exception e)
        {
            Console.Write("Error");
        }
        finally
        {
        }
    }
}
How to&Answers:

If you look at the documentation for Worksheets.Add you get:

Object Add(
    Object Before,
    Object After,
    Object Count,
    Object Type
)

So the Add method both creates the new worksheet and specifies various parameters about how to place it in the Worksheets collection.

  • Before Type: System.Object Optional Object. An object that specifies the sheet before which the new sheet is added.
  • After Type: System.Object Optional Object. An object that specifies the sheet after which the new sheet is added.
  • Count Type: System.Object Optional Object. The number of sheets to be added. The default value is one.
  • Type Type: System.Object Optional Object. Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet.

So all you need to do is something like:

var newSheet = appExcel.Worksheets.Add(null, appExcel.Worksheets(appExcel.Worksheets.Count), 1, XlSheetType.xlWorksheet);    
newSheet.Name = "myWorkSheet";

This will create and return one worksheet after the last worksheet and then set the name.

Note: Worksheets are not 0 based.

Update:

So as per my hackery in the comments this should instead be:

 var newSheet = (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
 newSheet.Name = "myWorkSheet";

or just

var newSheet = appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet) as Worksheet;
newSheet.Name = "myWorkSheet";