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
{
}
}
}