![](/img/trans.png)
[英]Error trying to write a value to a cell in an Excel worksheet in C#
[英]Weird error when trying to change worksheet name in c#
這是引發錯誤的代碼:
string[] sheetNames = new string[] {"Raw"};
for (int s = 1; s <= sheetNames.Count(); s++)
{
Excel.Worksheet newSheet = new Excel.Worksheet();
newSheet.Name = sheetNames[s];
newWorkbook.Worksheets.Add(newSheet);
}
我將Unable to cast COM object of type 'WorksheetClass' to interface type '_Worksheeet'
異常。 我不知道那是什么意思。
感謝所有幫助
問題是創建Worksheet
對象的方式。 您需要先定義一個Application
對象,然后調用類似以下內容: Application.WorkSheets.Add()
示例:
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWb = xlApp.Workbooks.Add() as Excel.Workbook;
Excel.Worksheet xlSheet = xlWb.Sheets[1] as Excel.Worksheet;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.