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 SINH VIÊN";
                 objSheet.Cells[5, 3] = "Mã Học Phần";
                 objSheet.Cells[5, 4] = "Mã Nhóm Học Phần";
                 objSheet.Cells[5, 5] = "Tên Học Phần";
                 objSheet.Cells[5, 6] = "Điểm Thi";
                 objSheet.Cells[5, 7] = "Điểm Quá Trình";
                 objSheet.Cells[5, 8] = "Điểm Xếp Loại";
                 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
                 objSheet.Cells[4, 3] = "Mã SV : " + txtMaSV.Text + "";
                 objSheet.Cells[9,10] = "ĐIỂM TỔNG KẾT";
                 objSheet.Cells[10,10] = "Hệ 10 : "+txtHe10.Text+"";
                 objSheet.Cells[11,10] = "Hệ 4 : "+txtHe4.Text+"";
                 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 + 3] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
                         objSheet.Columns.AutoFit();
                     }
                 }
                 objExcelApp.Visible = true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }