private void Torles_Click(object sender, EventArgs e) { if (Path.GetExtension(Properties.Settings.Default.ExcelFajlHelye) == ".csv") { MessageBox.Show("Csak .xlsx és .xlsm fájllal működik!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (FileMethods.IsExcelComptaible(Properties.Settings.Default.ExcelFajlHelye)) { if (FileMethods.IsFileLocked(Properties.Settings.Default.ExcelFajlHelye) == false) { torlesFolyamatban.Visible = true; torlesFolyamatban.BringToFront(); Excel.Workbook xlWorkbook = xlWorkbooks.Open(Properties.Settings.Default.ExcelFajlHelye); Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[Properties.Settings.Default.oldalszam + 1]; Excel.Range xlRange = xlWorksheet.UsedRange; int totalRows = ExcelApp.GetMinimalUsedRangeAddress(xlWorksheet); int totalColumns = xlRange.Columns.Count; int szam = kivSor; Excel.Range delRange = xlWorksheet.Rows[szam]; delRange.EntireRow.Delete(Type.Missing); for (int i = szam; i < totalRows; i++) { xlWorksheet.Cells[i, 1].Value = i - 1; } string megjegyzes = (megjegyzesek.Text); xlWorksheet.Cells[szam, totalColumns].Value = megjegyzes; try { xlWorkbook.Save(); } catch { MessageBox.Show("Nem sikerült a mentés! Ellenőrizze hogy be van-e zárva a fájl.", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); } FileMethods.DisposeExcelInstance(xlWorkbook, xlWorksheet); torlesFolyamatban.Visible = false; this.Hide(); } else { MessageBox.Show("Nem elérhető a fájl. Zárja be a szerkesztés miatt!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } }
public bool UjTanulo(string excelHelye) { if (excelHelye == "" || excelHelye == null) { MessageBox.Show("Válaszd ki az olvasni kívánt excel fájlt!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } int totalRows = 1; megjegyzesek.Text = ""; List <string> adatTipusok = new List <string>(); if (FileMethods.IsExcelComptaible(Properties.Settings.Default.ExcelFajlHelye)) { FileMethods.FajlOlvasas(); bool szabadE = FileMethods.IsFileLocked(Properties.Settings.Default.ExcelFajlHelye); if (szabadE == true) { MessageBox.Show("Nem elérhető a fájl. Zárja be a szerkesztés miatt!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } Excel._Workbook xlWorkbook = xlWorkbooks.Open(Properties.Settings.Default.ExcelFajlMasolata); Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[Properties.Settings.Default.oldalszam + 1]; Excel.Range xlRange = xlWorksheet.UsedRange; totalRows = ExcelApp.GetMinimalUsedRangeAddress(xlWorksheet); int totalColumns = xlRange.Columns.Count; kivSor = totalRows + 1; int szam = 1; var sb = new StringBuilder(); for (int ColumnNum = 1; ColumnNum <= totalColumns + 1; ColumnNum++) { string row = Convert.ToString(xlWorksheet.Cells[szam, ColumnNum].Text); if (row == null) { row = ""; } sb.Append("đ"); sb.Append(row); } FileMethods.DisposeExcelInstance(xlWorkbook, xlWorksheet); string[] Sorelemek = sb.ToString().Split('đ'); for (int j = 1; j < Sorelemek.Length; j++) { adatTipusok.Add(Sorelemek[j]); } } else { MessageBox.Show("Csak .xlsx, vagy .xlsm fájlnál működik a funkció!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } dataGridView1.Rows.Clear(); for (int i = 0; i < adatTipusok.Count - 2; i++) { var index = dataGridView1.Rows.Add(); dataGridView1.Rows[index].Cells[0].Value = adatTipusok[i]; } try { dataGridView1.Rows[0].Cells[1].Value = totalRows; } catch { MessageBox.Show("Semmilyen adattípus nincs megadva a táblázatban!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); Visible = false; return(false); } if (Properties.Settings.Default.ujTanuloIdo) { string ido = DateTime.Now.ToString("MM/dd/yyyy"); string ev = ido.Substring(8, 4) + "."; string honap = ido.Substring(0, 2) + "."; string nap = ido.Substring(4, 2) + "."; megjegyzesek.Text = ("Hozzáadás dátuma: " + ev + honap + nap); } dataGridView1.Rows[0].ReadOnly = true; dataGridView1.Columns[0].ReadOnly = true; dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.FromArgb(224, 224, 224); dataGridView1.Rows[0].DefaultCellStyle.SelectionBackColor = Color.FromArgb(224, 224, 224); dataGridView1.Rows[0].DefaultCellStyle.SelectionForeColor = Color.Black; Application.DoEvents(); BringToFront(); Show(); return(true); }
private void Szerkesztes_Click(object sender, EventArgs e) { if (dataGridView1.Rows[1].Cells[1].Value == null || dataGridView1.Rows[1].Cells[1].Value.ToString() == "") { MessageBox.Show("Nem írt be nevet a tanulónak!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } mentesFolyamatban.Visible = true; mentesFolyamatban.BringToFront(); if (FileMethods.IsExcelComptaible(Properties.Settings.Default.ExcelFajlHelye)) { if (FileMethods.IsFileLocked(Properties.Settings.Default.ExcelFajlHelye) == false) { Excel.Workbook xlWorkbook = xlWorkbooks.Open(Properties.Settings.Default.ExcelFajlHelye); Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[Properties.Settings.Default.oldalszam + 1]; Excel.Range xlRange = xlWorksheet.UsedRange; int totalRows = ExcelApp.GetMinimalUsedRangeAddress(xlWorksheet); int totalColumns = xlRange.Columns.Count; string sorszam = dataGridView1.Rows[0].Cells[1].Value.ToString(); int szam = 0; try { szam = Convert.ToInt32(sorszam) + 1; } catch (Exception) { FileMethods.DisposeExcelInstance(xlWorkbook, xlWorksheet); MessageBox.Show("Hiba a sorszám olvasásnál!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } int count = 0; szam = kivSor; while (count < dataGridView1.RowCount - 1) { string data; if (dataGridView1.Rows[count].Cells[1].Value == null) { data = ""; } else { data = (dataGridView1.Rows[count].Cells[1].Value.ToString()); } xlWorksheet.Cells[szam, count + 1].Value = data; count++; } xlWorksheet.Columns.RowHeight = 15; string megjegyzes = (megjegyzesek.Text); xlWorksheet.Cells[szam, totalColumns].Value = megjegyzes; try { xlWorkbook.Save(); } catch { MessageBox.Show("Nem sikerült a mentés! Ellenőrizze hogy be van-e zárva a fájl.", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); } FileMethods.DisposeExcelInstance(xlWorkbook, xlWorksheet); } else { MessageBox.Show("Nem elérhető a fájl. Zárja be a szerkesztés miatt!", "Figyelmeztetés", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } mentesFolyamatban.Visible = false; }