簡體   English   中英

嘗試在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM