gpt4 book ai didi

c# - 一些 Excel 单元格保持未填充状态,使用 C#

转载 作者:行者123 更新时间:2023-11-30 17:19:43 25 4
gpt4 key购买 nike

我正在编写一个打开现有 .xlsx 文件并写入特定单元格的应用程序。

有些单元格写入正确,而其他单元格只是空白?

有什么想法吗?

这是一段代码

工作和不工作的单元格的代码相同,除了索引改变了

oSheet.Cells[3, 15] = "1"; // this doesnt write to the cell 
oSheet.Cells[7, 7] = "1"; // this writes to the cell

我能想到的是 Excel 文件中存在格式问题吗?

最佳答案

我多年来一直在 Excel 中工作,并且一直发现这样的怪癖。如果您使用的是 .NET 4.0,请尝试以下操作:

using Excel = Microsoft.Office.Interop.Excel    

//Other Class code
var range = oSheet.Cells[3, 15];
range.Value2 = "1";

否则,试试这个:

using Excel = Microsoft.Office.Interop.Excel    

//Other Class code

Excel.Range range = (Excel.Rang)oSheet.Cells[3, 15];
range.Value2 = "1";

Value2 似乎更稳定,所以我通常建议使用它。

干杯!

关于c# - 一些 Excel 单元格保持未填充状态,使用 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661737/

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