gpt4 book ai didi

c# - 在 C# 中读取 Excel 文件总是导致 System.__ComObject?

转载 作者:行者123 更新时间:2023-11-30 14:41:04 25 4
gpt4 key购买 nike

这是我用来读取 xls 文件的代码:

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

MessageBox.Show(excelSheet.Cells[1,1].ToString());

这会产生一个消息框:

System.__ComObject

不确定发生了什么,非常感谢任何帮助,谢谢!

最佳答案

使用范围

Microsoft.Office.Interop.Excel.Range range =(Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1,1];
string cellValue =range.Value.ToString();

关于c# - 在 C# 中读取 Excel 文件总是导致 System.__ComObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com