c# - 无法将 'System.__ComObject' 转换为 'System.Web.UI.WebControls.ListItem

标签 c# asp.net excel

下面的代码给了我错误:cannot convert COM object of type 'System.__ComObject' to class type 'System.Web.UI.WebControls.ListItem .

Excel.Application app = new Excel.Application();
Excel.Workbook workbk;

workbk = app.Workbooks.Open(savePath);
//ArrayList a = new ArrayList();
foreach (var list in workbk.Sheets)
{
    ListBox1.Items.Add((ListItem)list);
}

为什么我会收到错误消息?

最佳答案

正如错误所暗示的,来自 Sheets 的工作表集合不是 ListItem .您需要在两者之间进行某种转换(例如,获取单元格值、工作表名称或其他内容)

关于c# - 无法将 'System.__ComObject' 转换为 'System.Web.UI.WebControls.ListItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657094/

相关文章:

c# - OpenSSL.NET 将 Ruby 示例移植到 C#(来自 RailsCasts 143 paypal-security)

c# - 在 RedirectToAction 中传递对象

excel - 用于永久启用 MACROS 的 QTP 代码

excel - 比较 Excel 中的 2 个列表?

c# - InvalidCastException : Cannot cast from source type to destination type.(Unity c#,foreach 列表循环)

c# - 如何在.net 上为WebClient 设置超时?

c# - 使用 xsd.exe (VS2010) 从 xsd 生成 C# 代码时出现 StackOverFlowException

php - JavaScript - 赋值 - 数组的数组

javascript - 检测页面是否在新选项卡中打开

python - win32com 在使用 python 写入日期值时遇到问题