我正在尝试在Excel工作簿中重命名工作表。谁能告诉我我缺少或做错了什么吗?
//Execute Excel
Excel.Application ex = new Excel.ApplicationClass();
ex.Visible = true;
ex.DisplayAlerts = false;
//Create workbook
Excel.Workbook wb = ex.Workbooks.Add();
//Create sheets with names
List<string> names = new List<string> {"03 SHEET 03","02 SHEET 02","01 SHEET 01"};
foreach (string name in names)
{
wb.Worksheets.Add().Name = name;
}
Your code example works with little adjustment. I just changed Excel initialization from
new Excel.ApplicationClass()
tonew Excel.Application()
. Here is my full code:Of course, I had to add the reference to
Microsoft.Office.Interop.Excel
library. If you have any other issues, please provide more details about the errors (compile, runtime exception).