Imports Microsoft.Office.Interop
Imports System.IO

Public Class frmImport_Report

    Private Sub frmImport_Report_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            Dim Path As String = Application.UserAppDataPath.ToString & "\config\Datagrid\"
            '_Datagrid.SaveC1_DG(VWGERAET_MELDUNG_FEHLTDG, Path, "VWGERAET_MELDUNG_FEHLTDG.xml")
            '_Datagrid.SaveC1_DG(VWSUPPLY_OHNE_ZODG, Path, "VWSUPPLY_OHNE_ZO.xml")
            '_Datagrid.SaveC1_DG(VWSILENT_SUPPLIESDG, Path, "VWSILENT_SUPPLIESDG.xml")
            '_Datagrid.SaveC1_DG(VWSUPPLIES_NICHT_DEF_DG, Path, "VWSUPPLIES_NICHT_DEF_DG.xml")
            Save_GridLayout_Geraete()
            Save_Datagrid(TBVERTRAG_NOTEXISTDataGridView, "TBVERTRAG_NOTEXISTDataGridView")

            Save_Datagrid(VWNotInListDataGridView, "VWNotInListDataGridView")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Speichern Gridlayouts:")
        End Try
    End Sub
    Sub Save_GridLayout_Geraete()
        Dim XMLPath = ClassHelper.GetSelectionGridName("GridControlGeraetVerfolgung")
        GridControlSilentDevices.MainView.SaveLayoutToXml(XMLPath)
    End Sub
    Sub Load_GridLayout_Geraete()
        Try
            Dim XMLPath = ClassHelper.GetSelectionGridName("GridControlGeraetVerfolgung")
            If File.Exists(XMLPath) Then
                GridControlSilentDevices.MainView.RestoreLayoutFromXml(XMLPath)
                GridViewSilentDevices.ClearColumnsFilter()
                'grvwSelection.ClearGrouping()
                GridViewSilentDevices.ClearSelection()
                GridViewSilentDevices.OptionsView.ShowPreview = False
            End If
        Catch ex As Exception

        End Try

    End Sub
    Private Sub frmImport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim schritt As String
        Try
            schritt = "VWNotInListTableAdapter"
            Me.VWNotInListTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWNotInListTableAdapter.Fill(Me.DS_Reporting.VWNotInList)
            schritt = "TBVERTRAG_NOTEXISTTableAdapter"
            Me.TBVERTRAG_NOTEXISTTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.TBVERTRAG_NOTEXISTTableAdapter.Fill(Me.DS_Reporting.TBVERTRAG_NOTEXIST)
            schritt = "VWGERAET_MELDUNG_FEHLTTableAdapter"
            Me.VWGERAET_MELDUNG_FEHLTTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWGERAET_MELDUNG_FEHLTTableAdapter.Fill(Me.DS_Reporting.VWGERAET_MELDUNG_FEHLT)
            schritt = "VWSUPPLY_OHNE_ZOTableAdapter"
            Me.VWSUPPLY_OHNE_ZOTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWSUPPLY_OHNE_ZOTableAdapter.Fill(Me.DS_Reporting.VWSUPPLY_OHNE_ZO)
            schritt = "VWSILENT_SUPPLIESTableAdapter"
            Me.VWSILENT_SUPPLIESTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWSILENT_SUPPLIESTableAdapter.Fill(Me.DS_Reporting.VWSILENT_SUPPLIES)
            schritt = "VWSUPPLIES_NICHT_DEFTableAdapter"
            Me.VWSUPPLIES_NICHT_DEFTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWSUPPLIES_NICHT_DEFTableAdapter.Fill(Me.DS_Reporting.VWSUPPLIES_NICHT_DEF)
            schritt = "VWSILENT_GERAETTableAdapter"
            Me.VWSILENT_GERAETTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.VWSILENT_GERAETTableAdapter.Fill(Me.DS_Reporting.VWSILENT_GERAET)

            Me.Cursor = Cursors.WaitCursor
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Laden der Formulardaten:")
        End Try

        Try
            Load_Datagrid(TBVERTRAG_NOTEXISTDataGridView, "TBVERTRAG_NOTEXISTDataGridView")
            Load_Datagrid(VWNotInListDataGridView, "VWNotInListDataGridView")
            Dim Path As String = Application.UserAppDataPath.ToString & "\config\Datagrid\"

            '_Datagrid.LoadC1_DG(VWGERAET_MELDUNG_FEHLTDG, Path, "VWGERAET_MELDUNG_FEHLTDG.xml")
            '_Datagrid.LoadC1_DG(VWSUPPLY_OHNE_ZODG, Path, "VWSUPPLY_OHNE_ZO.xml")
            '_Datagrid.LoadC1_DG(VWSILENT_SUPPLIESDG, Path, "VWSILENT_SUPPLIESDG.xml")
            '_Datagrid.LoadC1_DG(VWSUPPLIES_NICHT_DEF_DG, Path, "VWSUPPLIES_NICHT_DEF_DG.xml")
            Load_GridLayout_Geraete()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Laden Gridlayouts:")
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub tsbExcel_GeraeteNotinList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbExcel_GeraeteNotinList.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "VWNotInList", "Geräte nicht in Liste")
        Cursor = Cursors.Default
    End Sub
    Public Sub XLS_Mit_Filter(ByVal vDataset As DataSet, ByVal vds_dt As String, ByVal vSheet_Name As String)
        Dim dt As DataTable

        'To manipulate an Excel workbook, the code needs to instantiate an Excel Application object, 
        'and then use the object model to add a new workbook, change the name (to Northwind Customers in this example), and do other UI chores.

        'Create an instance of Excel 2003, add a workbook, 
        'and let the user know what's happening
        Dim xl As New Excel.Application
        xl.Workbooks.Add()
        xl.ActiveSheet.Name = vSheet_Name
        xl.Visible = True
        xl.Range("A1").Value = "Exportiere Daten aus Datenbank...."

        'Once Excel is loaded and running, the code needs a reference to the table in the DataSet ds. 
        'Then it loops through the Columns collection of that DataTable object and places the field names 
        'in the first row of the worksheet. This example uses a Customers table and a Customers column heading.

        Try
            xl.ScreenUpdating = False

            'Start with the Customers table
            dt = vDataset.Tables(vds_dt) '"VWDWD_EXP_KPI"

            'Add the column headings for the Customers
            Dim dc As DataColumn
            Dim iCols As Int32 = 0
            For Each dc In dt.Columns
                xl.Range("A1").Offset(0, iCols).Value = dc.ColumnName
                iCols += 1
            Next

            'All that's left is to loop through the rows in the table and use the ItemArray property of the DataRow 
            'object to write the contents of each data row to a row of the worksheet. This is about as close as Excel 
            'comes to supporting a DataSet object directly.

            'Add the data
            Dim iRows As Int32
            For iRows = 0 To dt.Rows.Count - 1
                xl.Range("A2").Offset(iRows).Resize(1, iCols).Value = dt.Rows(iRows).ItemArray()
            Next
        Catch ex As Exception

            'The remaining code in the procedure updates the UI and applies built-in formatting to the worksheet.

        Finally
            xl.ScreenUpdating = True
        End Try
        'Make the sheet pretty
        With xl.ActiveSheet.Range("A1")
            .AutoFilter()
            .AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatSimple) 'xlRangeAutoFormatSimple
        End With

        xl = Nothing
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "TBVERTRAG_NOTEXIST", "Vertrag nicht gepflegt")
        Cursor = Cursors.Default
    End Sub

    Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbExcel_Geraetemeldung.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim saveFileDialog1 As New SaveFileDialog()
            With saveFileDialog1
                .Filter = "Excel-Mappe (*.xls)|*.xls|All files (*.*)|*.*"
                .FilterIndex = 1
                .RestoreDirectory = True
                .Title = "Excel-Speicherort wählen:"
            End With
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                'VWGERAET_MELDUNG_FEHLTDG.ExportToExcel(saveFileDialog1.FileName)
                MsgBox("Export nach Excel erfolgreich abgeschlossen!", MsgBoxStyle.Information, "Erfolg:")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei tsbExcel.Click:")
        End Try

        Me.Cursor = Cursors.Default
    End Sub
    Sub Load_Datagrid(ByVal _Datagrid As DataGridView, ByVal _settings_String As String)
        Dim settings() As String
        If My.Settings(_settings_String) <> String.Empty Then
            settings = My.Settings(_settings_String).Split("$"c)
            For idx As Integer = 0 To _Datagrid.Columns.Count - 1
                Dim sv() As String = settings(idx).Split("/"c)
                _Datagrid.Columns(idx).DisplayIndex = CInt(sv(0))
                _Datagrid.Columns(idx).Width = CInt(sv(1))
            Next
        End If
    End Sub
    Sub Save_Datagrid(ByVal _Datagrid As DataGridView, ByVal _settings_String As String)
        Dim settings As String = String.Empty
        For idx As Integer = 0 To _Datagrid.Columns.Count - 1
            settings += _Datagrid.Columns(idx).DisplayIndex.ToString()
            settings += "/"
            settings += _Datagrid.Columns(idx).Width.ToString
            settings += "$"
        Next
        My.Settings(_settings_String) = settings
    End Sub

    Private Sub ToolStripButton19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton19.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "VWSUPPLY_OHNE_ZO", "Supplies ohne ZO")
        Cursor = Cursors.Default
    End Sub
    Private Sub ToolStripButton29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton29.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "VWSILENT_SUPPLIES", "Silent Supplies")
        Cursor = Cursors.Default
    End Sub

    Private Sub ToolStripButton34_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton34.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "VWSUPPLIES_NICHT_DEF", "Nicht definierte Supplies")
        Cursor = Cursors.Default
    End Sub

    Private Sub ToolStripButton24_Click_1(sender As System.Object, e As System.EventArgs) Handles ToolStripButton24.Click
        Me.Cursor = Cursors.WaitCursor
        XLS_Mit_Filter(Me.DS_Reporting, "VWSILENT_GERAET", "SilentDevices")
        Cursor = Cursors.Default
    End Sub
End Class