1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| * #region ExporterDataGridVersExcel Surchargé Type #1
* ///<summary>
* ///Permet d'exporter un DataGrid vers excel
* ///</summary>
* /// <param name="dgView">Data Grid Source des données à Exporter vers Excel</param>
* ///<param name="unFichier">Fichier Excel de destination des données</param>
* ///<param name="strEnteteDeFichier">Libellé de l'en-tête du fichier à générer</param>
* public void ExporterDataGridVersExcel(DataGridView dgView, String unFichier, string strEnteteDeFichier)
* {
* int i = 0;
* int j = 0;
* try
* {
* ExcelApplication excel = new ExcelApplication();
* Workbook exbook = (Workbook)excel.Workbooks.Add(Missing.Value);
* Worksheet exsheet = (Worksheet)excel.ActiveSheet;
* //Double[] Totaux= new Double[4];
*
* //Mise en forme de l'en-tête de la feuille Excel
* exsheet.Cells[1, 1] = strEnteteDeFichier;
* Range r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
* r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
* r.Font.Bold = true;
* r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
* r.EntireColumn.AutoFit();//Fin de la mise en forme de l'en-tête.
*
* foreach (DataGridViewColumn ch in dgView.Columns)
* {
* r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
* exsheet.Cells[2, i + 1] = ch.Name.Trim();
* r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
* r.Font.Bold = true;
* r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
* r.EntireColumn.AutoFit();
* i++;
* }
* j = 3;
*
* foreach (DataGridViewRow uneLigne in dgView.Rows)
* {
* i = 1;
* foreach (DataGridViewColumn uneColonne in dgView.Columns)
* {
* r = exsheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), Missing.Value);
* exsheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim();
* r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
* r.EntireColumn.AutoFit();
* i++;
* }
* exsheet.Columns.AutoFit();
* j++;
* }
* exsheet.SaveAs(unFichier, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
* excel.Quit();
* }
* catch (Exception ex)
* {
* throw (ex);
* }
* }//ExporterDataGridVersExcel
* #endregion //ExporterDataGridVersExcel |
Partager