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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
//Verifions si le datagrid est vid ou non
if (dtGridInsertion.ColumnCount == 0)
{
MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
}
if (dtGridInsertion.RowCount == 0)
{
MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
}
//Créons un DataSet pour recuperer les données
DataSet myDs = new DataSet();
DataSet myDs2 = new DataSet();
//On ajoute des tables au ds
myDs.Tables.Add();
myDs2.Tables.Add();
//On ajoute des colonnes aux tables
for (int i = 0; i <= dtGridInsertion.ColumnCount - 1; i++)
{ myDs.Tables[0].Columns.Add(); }
//On ajoute des lignes aux tables
DataRow myDrw;
for (int i = 0; i <= dtGridInsertion.RowCount - 1; i++)
{
myDrw = myDs.Tables[0].NewRow();
for (int j = 0; j <= dtGridInsertion.ColumnCount - 1; j++)
{
myDrw[j] = dtGridInsertion.Rows[i].Cells[j].Value;
}
myDs.Tables[0].Rows.Add(myDrw);
}
Microsoft.Office.Interop.Excel.ApplicationClass myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wBook;
Microsoft.Office.Interop.Excel.Worksheet wSheet;
object oMissing = System.Reflection.Missing.Value;
wBook = myExcel.Workbooks.Add(oMissing);
wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
System.Data.DataTable myDt = myDs.Tables[0];
//System.Data.DataColumn myDc;
//System.Data.DataRow myDr;
int col = 0; int row = 0;
foreach (DataColumn Dc in myDt.Columns)
{//code,date_insertion,annonceur,marque,produit,support,message,langue,actionmedia,duree,cout
col = col + 1;
if (col == 1) Dc.ColumnName = "CODE";
if (col == 2) Dc.ColumnName = "DATE INSERTION";
if (col == 3) Dc.ColumnName = "ANNONCEUR";
if (col == 4) Dc.ColumnName = "MARQUE";
if (col == 5) Dc.ColumnName = "PRODUIT";
if (col == 6) Dc.ColumnName = "SUPPORT";
if (col == 7) Dc.ColumnName = "MESSAGE";
if (col == 8) Dc.ColumnName = "LANGUE";
if (col == 9) Dc.ColumnName = "ACTION MEDIA";
if (col == 10) Dc.ColumnName = "DUREE";
if (col == 11) Dc.ColumnName = "COUT";
myExcel.Cells[1, col] = Dc.ColumnName;
}
foreach (DataRow Dr in myDt.Rows)
{
row = row + 1;
col = 0;
foreach (DataColumn Dc in myDt.Columns)
{
col = col + 1;
myExcel.Cells[row + 1, col] = Dr[Dc.ColumnName];
}
}
wSheet.Columns.AutoFit();
string myFileName = "C:\\export.xls";
Boolean myFileOpen = false;
try
{
System.IO.FileStream fileTemp = System.IO.File.OpenWrite(myFileName);
myFileOpen = true;
fileTemp.Close();
}
catch (Exception myEx)
{ myFileOpen = false; MessageBox.Show("Erreur fichier deja ouvert!!! \n\t " + myEx.Message, "Erreur Export"); }
if (System.IO.File.Exists(myFileName)) System.IO.File.Delete(myFileName);
wBook.SaveAs(myFileName, oMissing, oMissing, oMissing, oMissing, oMissing, XlSaveAsAccessMode.xlExclusive, oMissing, oMissing, oMissing, oMissing, oMissing);
myExcel.Workbooks.Open(myFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
myExcel.Visible = true; |
Partager