0

I am getting a problem whilst inserting textbox data into an excel file... the error which keeps showing is given below. What changes should I make in order to successfully insert my data into the xls file?

ERROR: Property, indexer, or event 'Range' is not supported by the language; try directly calling accessor method 'Microsoft.Office.Interop.Excel._Worksheet.get_Range(object, object)'

private void btnAdd_Click(object sender, EventArgs e)
{
 int _lastRow = xlWorkSheet.Range["A"+xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1 ;
            xlWorkSheet.Cells[_lastRow, 1] = textBox1.Text;
            xlWorkSheet.Cells[_lastRow, 2] = textBox2.Text;
            xlWorkSheet.Cells[_lastRow, 3] = textBox3.Text;
            xlWorkSheet.Cells[_lastRow, 4] = textBox4.Text;

The error occurs on the following line:

int _lastRow = xlWorkSheet.Range["A"+xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1 ;

opalenzuela
  • 3,139
  • 21
  • 41
akk
  • 45
  • 1
  • 1
  • 6

1 Answers1

0

If you are looking to find row,column of last filled cell . Then you should use UsedRange

int lastRow = xlWorkSheet.UsedRange.Row;

int lastCol = xlWorkSheet.UsedRange.Column;

for setting certain value in Your excel for ex. If you want to set value of textBox1 into cell "B4" then use following code.

 xlWorkSheet.get_Range("B4", Type.Missing).Value2 = textBox1.Text;
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116