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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
   |          Dim rowsTotal, colsTotal As Short
        Dim I, j, iC As Short
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        Dim xlApp As New Microsoft.Office.Interop.Excel.Application
        Dim excelBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add
        Dim excelWorksheet As Microsoft.Office.Interop.Excel.Worksheet = CType(excelBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
        Dim excelWorksheet2 As Microsoft.Office.Interop.Excel.Worksheet = CType(excelBook.Worksheets(2), Microsoft.Office.Interop.Excel.Worksheet)
 
        ' Define a title for the message box.
        Dim title = "Exportation"
        Dim response = MsgBox("Exportation des données, veuillez patienter.", , title)
 
        Try
            xlApp.Visible = True
            rowsTotal = Form2.DataGridView1.RowCount - 1
            colsTotal = Form2.DataGridView1.Columns.Count - 1
 
 
            With excelWorksheet
                .Name = "Données"
                .Cells.Select()
                .Cells.Delete()
                For iC = 0 To colsTotal
                    .Cells(1, iC + 1).Value = Form2.DataGridView1.Columns(iC).HeaderText
                    .Cells(1, iC + 1).Interior.ColorIndex = 3
                Next
 
                With excelWorksheet.Cells.Range(excelWorksheet.Cells(1, 1), excelWorksheet.Cells(1, colsTotal + 1))
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).ColorIndex = 0
                End With
 
                For I = 0 To rowsTotal - 1
                    For j = 0 To colsTotal
                        .Cells(I + 2, j + 1).value = Form2.DataGridView1.Rows(I).Cells(j).Value
                    Next j
                Next I
 
                With excelWorksheet.Cells.Range(excelWorksheet.Cells(2, 1), excelWorksheet.Cells(rowsTotal + 1, colsTotal + 1))
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).ColorIndex = 0
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).ColorIndex = 0
                End With
 
                .Rows("1:1").Font.FontStyle = "Bold"
                .Rows("1:1").Font.Size = 10
                .Cells.Columns.AutoFit()
            End With
 
            'create chart
            Dim chartPage As Microsoft.Office.Interop.Excel.Chart
            Dim myChart As Microsoft.Office.Interop.Excel.ChartObject
            Dim chartRange As Microsoft.Office.Interop.Excel.Range
 
msgbox("jusque là ça marche nickel ensuite ça plante sur myChart = .ChartObjects.Add(0, 0, 600, 450)") 
            excelWorksheet2.Select()
 
            With excelWorksheet2
                .Name = "Graphe"
                myChart = .ChartObjects.Add(0, 0, 600, 450)
            End With
 
            Dim misValue As Object = System.Reflection.Missing.Value
            xlApp = New Microsoft.Office.Interop.Excel.Application
            excelBook = xlApp.Workbooks.Add(misValue)
 
            '   xlCharts = excelWorksheet2.ChartObjects
            '   myChart = excelWorksheet2.ChartObjects.Add(0, 0, 600, 450)
 
            chartPage = myChart.Chart
            chartRange = excelWorksheet.Range(excelWorksheet.Cells(1, 3), excelWorksheet.Cells(rowsTotal + 1, colsTotal + 1))
            chartPage.SetSourceData(Source:=chartRange, PlotBy:=Microsoft.Office.Interop.Excel.XlRowCol.xlColumns)
            chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine
 
            Dim NameFile As String
            If cyclename = "" Or cycleshort = "" Then
                If Directory.Exists(Application.StartupPath.ToString() & "\" & "enregistrement manuel") = False Then
                    Directory.CreateDirectory(Application.StartupPath.ToString() & "\" & "enregistrement manuel")
                End If
                NameFile = Application.StartupPath.ToString() & "\" & "enregistrement manuel" & "\" & "_axe" & axe & "_manu" & "_" & Date.Now.Hour & "h" & Date.Now.Minute & ".xls"
                If File.Exists(NameFile) Then
                    NameFile = Application.StartupPath.ToString() & "\" & "enregistrement manuel" & "\" & "_axe" & axe & "_manu" & "_" & Date.Now.Hour & "h" & Date.Now.Minute & "min" & Date.Now.Second & ".xls"
                End If
            Else
                If Directory.Exists(Application.StartupPath.ToString() & "\" & cyclename) = False Then
                    Directory.CreateDirectory(Application.StartupPath.ToString() & "\" & cyclename)
                End If
                NameFile = (Application.StartupPath.ToString() & "\" & cyclename & "\" & cycleshort & "_axe" & axe & "_auto" & "_" & Date.Now.Hour & "h" & Date.Now.Minute & ".xls")
                If File.Exists(NameFile) Then
                    NameFile = Application.StartupPath.ToString() & "\" & cyclename & "\" & cycleshort & "_axe" & axe & "_auto" & "_" & Date.Now.Hour & "h" & Date.Now.Minute & "min" & Date.Now.Second & ".xls"
                End If
            End If
 
            excelWorksheet2.Cells(1, 1).select()
            excelWorksheet2.SaveAs(NameFile)
            excelBook.Close(False)
            ' xlApp.Quit()
            releaseObject(xlApp)
            releaseObject(excelBook)
            releaseObject(excelWorksheet)
 
            Dim style = MsgBoxStyle.MsgBoxSetForeground
            MsgBox("Exportation terminée avec succès.", style)
 
        Catch ex As Exception
            MsgBox("Export Excel Error " & ex.Message)
        Finally
            'RELEASE ALLOACTED RESOURCES   
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing
        End Try
        Return (True) | 
Partager