将文本框数据插入xls文件

时间:2013-10-15 08:58:41

标签: c#

我在将文本框数据插入excel文件时出现问题...下面给出了一直显示的错误。为了成功将数据插入xls文件,我应该做些哪些更改?

错误: 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;

错误发生在以下行:

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

1 个答案:

答案 0 :(得分:0)

如果您要查找最后一个填充单元格的行,列。然后你应该使用UsedRange

int lastRow = xlWorkSheet.UsedRange.Row;

int lastCol = xlWorkSheet.UsedRange.Column;

用于在excel中为ex设置特定值。如果要将textBox1的值设置为单元格“B4”,请使用以下代码。

 xlWorkSheet.get_Range("B4", Type.Missing).Value2 = textBox1.Text;