Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void btnXuatRaExcel_Click(object sender, EventArgs e)
         {
              try
             {

                 Excel.Application objExcelApp = new Excel.Application();
                 Excel.Workbook objExcelWorkbook = objExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                 Excel.Worksheet objSheet = (Excel.Worksheet)objExcelWorkbook.Worksheets[1];
                 //objSheet.Cells.Interior.Color.ToString() = 'blue';
                 objSheet = (Excel.Worksheet)objExcelWorkbook.Sheets[1];
                 objSheet.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
                 objSheet.Cells[2, 5] = "BẢNG ĐIỂM CỦA LỚP";
                 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
                 objSheet.Cells[4, 3] = "Mã Lớp : " + cmbMaLop.SelectedValue.ToString() + "";
                 objSheet.Cells[5, 4] = "Mã Sinh Viên";
                 objSheet.Cells[5, 5] = "Tên Sinh Viên";
                 objSheet.Cells[5, 6] = "Điểm Trung Bình";
                 //select sv.MaSV,TenSV,TrungBinhCuoiKyHe10

                 for (int i = 0; i < dgvKetQua.Rows.Count ; i++)
                 {
                     for (int j = 0; j < dgvKetQua.Columns.GetColumnCount(DataGridViewElementStates.Displayed); j++)
                     {
                         objSheet.Cells[i + 6, j + 4] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
                         objSheet.Columns.AutoFit();
                     }
                 }
                 objExcelApp.Visible = true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }