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 62 63
|
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.*/
Range r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
foreach (DataGridViewColumn ch in dgView.Columns)
{
if (ch.Visible && ch.CellType.Name != "DataGridViewImageCell")
{
r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
// r = exsheet.Range[Convert.ToChar(65 + i).ToString() + "1", Missing.Value];
//exsheet.Cells[2, i + 1] = ch.HeaderText.Trim();
exsheet.Cells[1, i + 1] = ch.HeaderText.Trim();
r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
r.Font.Bold = true;
r.Font.Color = XlRgbColor.rgbBlue;
r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
r.EntireColumn.AutoFit();
i++;
}
}
//j = 3;
j = 2;
foreach (DataGridViewRow uneLigne in dgView.Rows)
{
i = 1;
foreach (DataGridViewColumn uneColonne in dgView.Columns)
{
if (uneColonne.Visible && uneColonne.CellType.Name != "DataGridViewImageCell")
{
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)
{
MessageBox.Show(ex.Message);
} |
Partager