C#中操作excel的做法,求指导
C#中操作excel的做法,求指点 private void btnSave_Click(object sender, EventArgs e){//创建Application
C#中操作excel的做法,求指点
private void btnSave_Click(object sender, EventArgs e) { //创建Application对象 Excel.Application xApp = new Excel.Application(); xApp.Visible = true; //得到WorkBook对象 Excel.Workbook xBook = xApp.Workbooks._Open(@""+file.FileName+"", Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); //指定要操作的Sheet Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1]; //写入数据 for (int i = 4; i < ; i++) { Excel.Range rng3 = xSheet.get_Range("G"+i+"", Missing.Value); rng3.Value2 = "Hello"; } } 我要修改第G列第4行起的内容到数据的最后,问题来了,这个for循环里面i< 什么啊,怎么拿到数据的尾行 C# Excel
[解决办法] 引用: private void btnSave_Click(object sender, EventArgs e) { //创建Application对象 Excel.Application xApp = new Excel.Application(); xApp.Visible = true; //得到WorkBook对象 Excel.Workbook xBook = xApp.Workbooks._Open(@""+file.FileName+"", Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); //指定要操作的Sheet Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1]; //写入数据 for (int i = 4; i < ; i++) { Excel.Range rng3 = xSheet.get_Range("G"+i+"", Missing.Value); rng3.Value2 = "Hello"; } }
我要修改第G列第4行起的内容到数据的最后,问题来了,这个for循环里面i< 什么啊,怎么拿到数据的尾行int usedRow=Worksheet.UsedRange.Rows.Count;
[解决办法] 语句Activesheet.UsedRange.Rows.count返回当前工作表中已使用单元格区域的行数。
语句Activesheet.UsedRange.Columns.count返回当前工作表中已使用单元格区域的列数。