Imports System.IO
Imports Microsoft.Office.Interop.Excel
Public Class frmShowData
    Private BRG As New cBrgDb

    Dim BRG_GRN = ""
    Dim ART = ""
    Dim von As Date
    Dim bis As Date

    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Sub New(ART, BRG_GRN, von, bis)
        InitializeComponent()
        Me.ART = ART
        Me.BRG_GRN = BRG_GRN
        Me.von = von
        Me.bis = bis
    End Sub

    Private Sub DataGridView1_DataSourceChanged(sender As Object, e As EventArgs) Handles dgv.DataSourceChanged
        dgv.AutoSize = True
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        genCSV_startWithExcel(dgv)
    End Sub


    Public Sub genCSV_startWithExcel(dgv As DataGridView)
        Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\Monitoring\tmp\" ' My.Computer.FileSystem.GetTempFileName
        If Not My.Computer.FileSystem.DirectoryExists(sPath) Then
            My.Computer.FileSystem.CreateDirectory(sPath)
        End If
        Dim fn As String = sPath & "tmp_" & Now.ToString("ddMMyyyyHHmmss") & ".csv"
        Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(fn, False)
        Dim clmns As String = ""
        For i = 0 To dgv.ColumnCount - 1
            clmns &= dgv.Columns(i).HeaderText.ToString().Replace(";", ",") & ";"
        Next
        outFile.WriteLine(clmns)
        For i = 0 To dgv.RowCount - 1
            clmns = ""
            For j = 0 To dgv.ColumnCount - 1
                clmns &= dgv(j, i).Value.ToString().Replace(";", ",") & ";"
            Next
            outFile.WriteLine(clmns)
        Next
        outFile.Close()
        Try

            p.StartInfo.FileName = "Excel.exe"
            p.StartInfo.Arguments = fn
            p.EnableRaisingEvents = True
            p.Start()
            AddHandler p.Exited, AddressOf cleartmp 'Löscht die Temp-Dateien

        Catch ex As Exception
            MsgBox("Excel konnte nicht gestartet werden!" & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub
    Private WithEvents p As New Process

    Sub cleartmp(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Nach Beenden des Programmes werden alle temporären Dateien gelöscht
        Dim dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\Monitoring\tmp\"
        If IO.Directory.Exists(dir) Then
            For Each file As String In IO.Directory.GetFiles(dir) ' Ermittelt alle Dateien des Ordners 
                Try
                    My.Computer.FileSystem.DeleteFile(file)
                Catch ex As Exception : MsgBox(ex.Message) : End Try
            Next
        End If
    End Sub

    Sub initDGV()
        Select Case ART
            Case "BRG"
                Select Case tbcntr.SelectedIndex
                    Case 0 'DY
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKtoDAKOSYDetails004(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN)
                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Format = "N0"
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                    Case 1 'BLUEJAY
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKtoAtlas(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN)

                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("veopos_rohmas").DefaultCellStyle.Format = "N2"
                            dgv.Columns("veoerz_sicbtg").DefaultCellStyle.Format = "N2"
                            dgv.Columns("veoerz_basbtg").DefaultCellStyle.Format = "N2"
                            dgv.Columns("veopos_rohmas").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("veoerz_sicbtg").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("veoerz_basbtg").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                    Case 2 'EZOLL DHF
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKtoUnionEZoll(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN, " AND DatumBestimmungErreicht IS NULL ")
                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("GarantieWert").DefaultCellStyle.Format = "N2"
                            dgv.Columns("Gesamtrohmasse").DefaultCellStyle.Format = "N2"
                            dgv.Columns("GarantieWert").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("Gesamtrohmasse").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                    Case 3 'EZOLL TELOTEC
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKtoTELOTECDetails(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN)
                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("Betrag").DefaultCellStyle.Format = "N2"
                            dgv.Columns("Betrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                    Case 4 'NCTS_TR  
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKt_NCTS_TR_Details(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN)
                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Format = "N0"
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                    Case 5 'NCTS_FREMD
                        dgv.DataSource = BRG.loadDgvBySqlFromBrgKt_NCTS_FREMD_Details(datVON.Value.ToShortDateString, datBIS.Value.ToShortDateString, "50", "50", BRG_GRN)
                        If dgv.Columns.Count > 0 Then
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Format = "N2"
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Format = "N0"
                            dgv.Columns("nctsSA_AbgabenBetrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtRohmasse").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                            dgv.Columns("ncts_GesamtAnzahlPackstuecke").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                        End If
                End Select
            Case "AUFSCHUB"
        End Select
        
        Label2.Text = "Anzahl: " & dgv.RowCount
    End Sub

    Private Sub frmShowData_Load(sender As Object, e As EventArgs) Handles Me.Load
        If ART <> "" Then
            Me.datVON.Value = von
            Me.datBIS.Value = bis

            If BRG_GRN <> "" Then
                initDGV()
            End If
        Else
            Me.datVON.Visible = False
            Me.datBIS.Visible = False
            Me.tbcntr.Visible = False


        End If

    End Sub

    Private Sub tbcntr_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbcntr.SelectedIndexChanged
        initDGV()
    End Sub
End Class