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
| Public Class Direct
Private Sub Direct_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Coca As String() = {"Coca-Cola", "1,50", Stock.Stock_CocaCola, ASCourdimanche.VenteTotal_CocaCola, ASCourdimanche.VenteTotal_CocaCola * 1.5}
Tab.Rows.Add(Coca)
Dim IceTea As String() = {"Ice-Tea", "1,50", Stock.Stock_IceTea, ASCourdimanche.VenteTotal_IceTea, ASCourdimanche.VenteTotal_IceTea * 1.5}
Tab.Rows.Add(IceTea)
Dim Orangina As String() = {"Orangina", "1,50", Stock.Stock_Orangina, ASCourdimanche.VenteTotal_Orangina, ASCourdimanche.VenteTotal_Orangina * 1.5}
Tab.Rows.Add(Orangina)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Creating a Excel object.
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
Try
worksheet = workbook.ActiveSheet
worksheet.Name = "ExportedFromDatGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'Loop through each row and read value from each column.
For i As Integer = 0 To Tab.Rows.Count - 2
For j As Integer = 0 To Tab.Columns.Count - 1
' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
If cellRowIndex = 1 Then
worksheet.Cells(cellRowIndex, cellColumnIndex) = Tab.Columns(j).HeaderText
Else
worksheet.Cells(cellRowIndex, cellColumnIndex) = Tab.Rows(i).Cells(j).Value.ToString()
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
'Getting the location and file name of the excel to save from user.
Dim saveDialog As New SaveFileDialog()
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
saveDialog.FilterIndex = 2
If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export Successful")
End If
Catch ex As System.Exception
MessageBox.Show(ex.Message)
Finally
excel.Quit()
workbook = Nothing
excel = Nothing
End Try
End Sub
End Class |
Partager