C # 3.0
using Microsoft.Office.Interop.Excel;
Тогда
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass xl = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xlBook;
Microsoft.Office.Interop.Excel.Worksheet xlSheet;
string laPath = System.IO.Path.GetFullPath("C:\\ExcelSheet.xls");
xlBook = (Workbook)xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xlSheet = (Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet.Name = "CIAO";
xlBook.Save();
xl.Application.Workbooks.Close();
C # 4.0 +
Разница: не более System.Reflection.Missing.Value
и использование DLR (Dynamic Language Runtime)
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
Тогда
var excelFile = Path.GetFullPath("C:\\ExcelSheet.xls");
var excel = new Excel.Application();
var workbook = excel.Workbooks.Open(excelFile);
var sheet = (Excel.Worksheet)workbook.Worksheets.Item[1]; // 1 is the first item, this is NOT a zero-based collection
sheet.Name = DateTime.Now.ToString("yyyyMMddHHmmss");
workbook.Save();
excel.Application.Workbooks.Close();