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
| private void btnExcel_Click(object sender, EventArgs e)
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
//Start exccel
oXL = new Excel.Application();
oXL.Visible = true;
//new workbook
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
//Table header nomDesChamps
int i = 1;
foreach (var item in nomDesChamps)
{
oSheet.Cells[1, i] = item;
i++;
}
//Format de A1 a Z1
oSheet.get_Range("A1", "Z1").Font.Bold = true;
oSheet.get_Range("A1", "Z1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
oSheet.get_Range("A1", "Z1").Cells.EntireColumn.AutoFit();
//Création du tableau
string[,] answer = new string[26,reslutatRequete.Count];
for (int iRow = 0; iRow < reslutatRequete.Count; iRow++)
{
string item = reslutatRequete[iRow];
string[] recup = item.Split('-');
for (int iCol = 0; iCol < recup.Count(); iCol++)
{
answer[iRow, iCol] = recup[iCol];
}
}
//fill de A2 a
oSheet.get_Range("A2", "Z" + (reslutatRequete.Count() + 1).Value2 = answer;
//Sur que c'est visible et donne le controle
oXL.Visible = true;
oXL.UserControl = true;
} |