开发者

Some Excel cells stay unpopulated, with C#

I'm writing an application that opens an existing .xlsx file and writes to certain cells.

Some cells write correctly, where others just stay blank?

开发者_如何学Go

Any ideas?

This is a snippet of code

The same code for the cells that are and arent working, except that the index's changed

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

All that i could think is that there is a formatting issue in the Excel file?


I've been working in Excel for years and find quirks like this all the time. If you are in .NET 4.0 try this:

using Excel = Microsoft.Office.Interop.Excel    

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

Otherwise, try this:

using Excel = Microsoft.Office.Interop.Excel    

//Other Class code

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

Value2 seems to work more consistently, so I generally recommend using it.

Cheers!


Anthony was right, I had my columns and rows switched.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜