Some Excel cells remain uninhabited, while C #

I am writing an application that opens an existing file .xlsxand writes to specific cells.

Some cells write correctly, where others just remain empty?

Any ideas?

This is a piece of code

Same code for cells that work and work, except the index changed

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

All I could think is a formatting problem in an Excel file?

+3
source share
2 answers

Anthony was right, I had columns and rows.

+1
source

I have been working in Excel for many years and constantly find such quirks. If you are using .NET 4.0, try the following:

using Excel = Microsoft.Office.Interop.Excel    

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

Otherwise, try the following:

using Excel = Microsoft.Office.Interop.Excel    

//Other Class code

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

A value of 2 seems to work more consistently, so I usually recommend using it.

Hooray!

+1
source

Source: https://habr.com/ru/post/1784807/


All Articles