Inserting multiple textbox data into an Excel file

后端 未结 2 966
情话喂你
情话喂你 2020-12-07 03:15

I want to write a program that saves the text in textbox to an Excel file using a loop because I want to insert multiple text into Excel. I found codes but it only overwrite

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 03:35

    Your main concern is to find the last used row in your excel.

    For that you can use

     Excel.Range usedRange = xlWorkSheet .UsedRange;
     Excel.Range _lastCell= usedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,  
     Type.Missing);
    
      int _lastRow= lastCell.Row; // Gives you the last used row in your Excel sheet
      int _lastCol = lastCell.Column; // Give you the last used column
    
    
    
     _lastRow++; // To get the next row 
    
        for (int i=_lastRow; i<=_lastrow+6; i++) // Set your i value to _lastRow
        {
            xlWorkSheet.Cells[i, 1] = textBox1.Text;
            xlWorkSheet.Cells[i, 2] = textBox2.Text;
            xlWorkSheet.Cells[i, 3] = textBox3.Text;
            xlWorkSheet.Cells[i, 4] = textBox4.Text;
        }
    

提交回复
热议问题