string adresse = dataGridView1.CurrentRow.Cells["ADRESSE_CLI"].Value.ToString(); string nom = dataGridView1.CurrentRow.Cells["NOM_CLI"].Value.ToString(); string prenom = dataGridView1.CurrentRow.Cells["PRENOM_CLI"].Value.ToString(); //expliquation pour les lignes d en haut // string Nom de la variable = dataGridView1.CurrentRow.Cells["Nom du champs"].Value.ToString //En gros on fait un changement de type de données //Rajoute le texte dans les tables. oSheet.Cells[1, 1] = nom + " " + prenom; oSheet.Cells[2, 1] = adresse; oSheet.Cells[5, 6] = "LA poste ";
oSheet.Cells[1, 1] // Sert a definir la cellule on tu ecrie
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionstring[] toto = {"toto","tatat"}; bool succes; //valeur pour savoir si le demarage a marché GestionExcel MonExcel = new GestionExcel(out succes);//demarrage d'excel MonExcel.OuvrirFichierExistant("C:\exemple.xls","Feuill1");//ouvre le fichier existant à la feuille 1 MonExcel.ChangerDeFeuilleActive("Feuill2"); //actvie la feuille 2 MonExcel.EcrireLigne(toto, 1);//ecrit le contenu de toto à la ligne 1 MonExcel.SelectionRange("C2", "C2");//selectionne la case C2 MonExcel.EcrireCellule("J'écris dans C3", "C3");
MonExcel.OuvrirFichierExistant(@"C:\exemple.xls","Feuil1");//ouvre le fichier existant à la feuille 1
private void Click_Exporter(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application oXL; Microsoft.Office.Interop.Excel._Workbook oWB; Microsoft.Office.Interop.Excel._Worksheet oSheet; Microsoft.Office.Interop.Excel.Range oRng; object M = System.Reflection.Missing.Value; int i = 0; int j = 0; int k = 0; try { //Start Excel and get Application object. oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; //Get a new workbook. //oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(System.Reflection.Missing.Value)); oWB = oXL.Workbooks.Open(Application.StartupPath + @"/FicheEleves.xls", M, M, M, M, M, M, M, M, M, M, M, M, M, M); oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet; foreach (DataGridViewColumn col in dgvEleves.Columns) { if (oSheet.Cells[3, 1] != null) { oRng = oSheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", M); //oSheet.Cells[3, i + 1] = col.Name.Trim(); oRng.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic; oRng.Font.Bold = true; //oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value); //oRng.EntireColumn.AutoFit(); i++; } } oSheet.Cells[4, 1] = "N°"; oSheet.Cells[4, 2] = "MATRICULE"; oSheet.Cells[4, 3] = "NOM"; oSheet.Cells[4, 4] = "PRENOMS"; oSheet.Cells[4, 5] = "NE LE"; oSheet.Cells[4, 6] = "LIEU"; oSheet.Cells[4, 7] = "SEXE"; j = 5; foreach (DataGridViewRow uneLigne in dgvEleves.Rows) { i = 2; int h = 0; if (oSheet.Cells[5, 1] != null) { oRng = oSheet.get_Range(Convert.ToChar(65 + h).ToString() + j.ToString(), M); for (k = 0; k < dgvEleves.Rows.Count - 1; k++) { oSheet.Cells[k + 5, 1] = k + 1; //col.Name.Trim(); oRng.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic; oRng.Font.Bold = true; //oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value); oRng.EntireColumn.AutoFit(); //k++; } } foreach (DataGridViewColumn uneColonne in dgvEleves.Columns) { if (oSheet.Cells[j, i] != null) { oRng = oSheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), M); if (uneLigne.Cells[uneColonne.Name].Value != null) oSheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim(); oRng.Font.Bold = true; //oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value); oRng.EntireColumn.AutoFit(); i++; } } oSheet.Columns.AutoFit(); j++; } } catch (FileNotFoundException Lg) { MessageBox.Show(Lg.Message, "Erreur"); } }
public static class MaClasse { public static void ExportGrille(DataGridView grille) { //ici tout ton code } }
MaClasse.ExportGrille(dgvEleves);