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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
   |  
public static bool Export(System.Data.DataTable dt, string sheetName,string Header_Text)
        {
            object Missing = System.Reflection.Missing.Value;
            Excel.Application oXL;
            Excel.Workbook oWB;
            Excel.Worksheet oSheet;
            Excel.Range oRange;
 
            try
            {
                // Start Excel and get Application object. 
                oXL = new Excel.Application();
 
                // Set some properties 
                oXL.Visible = false;
                oXL.DisplayAlerts = false;
 
                // Get a new workbook. 
                oWB = oXL.Workbooks.Add(Missing);
 
                // Get the Active sheet 
                oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                oSheet.Name = sheetName;
 
                oSheet.Cells[1, 1] = Header_Text;
 
                oSheet.get_Range("A1", "A1").ColumnWidth = 13;
                oSheet.get_Range("B1", "B1").ColumnWidth = 13;
                oSheet.get_Range("C1", "C1").ColumnWidth = 25;
                oSheet.get_Range("A1", "C1").Font.Size = 14;
 
                oSheet.get_Range("A1", "C1").Font.Bold = true;
                oSheet.get_Range("A1", "C1").Merge(true);
                oSheet.get_Range("A1", "C1").Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
 
                oSheet.get_Range("A3", "C3").Font.Bold = true;
 
                int rowCount = 3;
                for (int i = 1; i < dt.Columns.Count + 1; i++)
                {
                    oSheet.Cells[rowCount, i] = dt.Columns[i - 1].ColumnName;
                }
 
                foreach (DataRow dr in dt.Rows)
                {
                    rowCount += 1;
                    for (int i = 1; i < dt.Columns.Count + 1; i++)
                    {
                        oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
                    }
                }
 
                // Resize the columns 
                //oRange = oSheet.get_Range(oSheet.Cells[1, 1],
                //oSheet.Cells[rowCount, dt.Columns.Count]);
                //oRange.EntireColumn.AutoFit();
 
                // Save the sheet and close 
                oSheet = null;
                oRange = null;
 
                string strParentDirectory = Declaration.lireFichier_Pieces();
                //strParentDirectory = strParentDirectory + "\\Data";
                if (!Directory.Exists(strParentDirectory))
                {
                    Directory.CreateDirectory(strParentDirectory);
                }
                string strFileName = strParentDirectory + "\\Statistics de la date_" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
                if (File.Exists(strFileName))
                {
                    File.Delete(strFileName);
                }
                FileStream file = new FileStream(strFileName, FileMode.Create);
                file.Close();
 
                oWB.SaveAs(strFileName, Excel.XlFileFormat.xlWorkbookNormal,
                    Missing, Missing, Missing, Missing,
                    Excel.XlSaveAsAccessMode.xlExclusive,
                    Missing, Missing, Missing,
                    Missing, Missing);
                oWB.Close(Missing, Missing, Missing);
                oWB = null;
                oXL.Quit();
            }
            catch
            {
                throw;
            }
            finally
            {
                // Clean up
                // NOTE: When in release mode, this does the trick 
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
 
            return true;
        } | 
Partager