Lightswitch import data from Excel to Lightswitch

Discussion in 'Lightswitch Integration' started by JeanM, Mar 5, 2014.

  1. JeanM

    JeanM New Member

    is It possible to import data to Lightswitch application? For example I would like import energy prices for energy calculation (energy billing calculation for subtenets).
  2. JeanM

    JeanM New Member

    After that i would like to use this data in Business Intelligence and Cost Allocation Module.
  3. avreporter

    avreporter Administrator Staff Member

    Yes of course. It is possible. You have to use this code in your application.

    First step: you have to add ImportButton method.
    Second step: You have to add this method you Lightswitch screen.
  4. avreporter

    avreporter Administrator Staff Member

    Private Sub SetupClipboardImportButton()
    AddHandler Me.FindControl("ClipboardImport").ControlAvailable,
    (Function(object1, eventargs1)
    Dim btnClipboardImport As System.Windows.Controls.Button = DirectCast(eventargs1.Control, System.Windows.Controls.Button)
    AddHandler btnClipboardImport.Click,
    (Function(object2, eventargs2)
    Try
    Dim clipboard As Object = System.Windows.Clipboard.GetText
    If Not clipboard Is Nothing Then

    Dim clipboardText As String = clipboard
    Dim clipboardTextRows As String() = clipboardText.Split(vbCrLf)
    'Dim clipboardTextCells As New List(Of String())
    clipboardTextCells.Clear()
    For i = 0 To clipboardTextRows.Count - 1
    If clipboardTextRows(i) Is Nothing OrElse _
    String.IsNullOrEmpty(clipboardTextRows(i).Trim) Then
    ' Empty row, skip
    Continue For
    End If
    Dim cells As String() = clipboardTextRows(i).Split(vbTab)
    Oszlopok = cells.Count
    If cells.Count <> 2 And cells.Count <> 3 Then
    Throw New Exception("Az importálandó táblázatnak 2 vagy 3 oszloppal kell rendelkeznie.")
    Else
    clipboardTextCells.Add(cells)
    End If
    Next
    End If
    Catch ex As Exception
    ShowMessageBox(ex.Message, "Hiba", MessageBoxOption.Ok)
    End Try
    End Function)
    End Function)
    End Sub
  5. avreporter

    avreporter Administrator Staff Member

    Private Sub ClipboardImport_Execute() 'Adatok beimportálása
    Dim hiba As Boolean = False
    For i = 0 To clipboardTextCells.Count - 1
    Gyariszam = clipboardTextCells(i)(0).Trim
    FogyasztasmeroksQuery.Load()
    If FogyasztasmeroksQuery.Count > 0 Then
    LeolvasottOraks.AddNew()
    Try
    LeolvasottOraks.SelectedItem.MeroID = FogyasztasmeroksQuery(0).ID
    LeolvasottOraks.SelectedItem.TimeStamp = New DateTime(Me.OraDate.Year, Me.OraDate.Month, Me.OraDate.Day, 0, 0, 0)
    LeolvasottOraks.SelectedItem.LeolvOra = clipboardTextCells(i)(1)
    If Oszlopok = 3 Then
    Try
    LeolvasottOraks.SelectedItem.LeolvMeddoOra = clipboardTextCells(i)(2)
    Catch ex As Exception
    End Try
    End If
    Catch ex As Exception
    ShowMessageBox(ex.Message)
    hiba = True
    Exit For
    End Try
    'Adat helyeség ellenörzés
    MeroIDLeolvFilter = FogyasztasmeroksQuery(0).ID
    KezdoDatum = LeolvasottOraListaFilter.AddMonths(-3)
    VegDatum = LeolvasottOraListaFilterEnd
    LeolvasottOraksQuery.Load()
    If LeolvasottOraksQuery.Count > 1 Then
    If LeolvasottOraksQuery(0).LeolvOra > clipboardTextCells(i)(1) Then
    If ShowMessageBox("Az alábbi leolvasott érték kisebb, mint az előző óra állás! (Mérőeszköz: " & clipboardTextCells(i)(0) & ") Folytassam az adatok importálását?", "AVReporter", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
    hiba = True
    Exit For
    End If
    End If
    'ShowMessageBox((LeolvasottOraksQuery(0).LeolvOra))
    'ShowMessageBox((LeolvasottOraksQuery(1).LeolvOra))
    If (clipboardTextCells(i)(1) - LeolvasottOraksQuery(0).LeolvOra) > (LeolvasottOraksQuery(0).LeolvOra - LeolvasottOraksQuery(1).LeolvOra) * 2 Then
    If ShowMessageBox("Az utolsó leolvasási időszakban a fogyasztás mértéke jelentősen növekedett az előző időszakhoz képest! Kérem ellenőrizze le, hogy az óraállás megfelelő. (Mérőeszköz: " & clipboardTextCells(i)(0) & ") Folytassam az adatok importálását?", "AVReporter", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
    hiba = True
    Exit For
    End If
    End If
    If Oszlopok = 3 Then
    Try
    If LeolvasottOraksQuery(0).LeolvMeddoOra > clipboardTextCells(i)(2) Then
    If ShowMessageBox("Az alábbi leolvasott érték kisebb, mint az előző óra állás! (Mérőeszköz: " & clipboardTextCells(i)(0) & ") Folytassam az adatok importálását?", "AVReporter", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
    hiba = True
    Exit For
    End If
    End If
    If (clipboardTextCells(i)(2) - LeolvasottOraksQuery(0).LeolvMeddoOra) > (LeolvasottOraksQuery(0).LeolvMeddoOra - LeolvasottOraksQuery(1).LeolvMeddoOra) * 2 Then
    If ShowMessageBox("Az utolsó leolvasási időszakban a fogyasztás mértéke jelentősen növekedett az előző időszakhoz képest! Kérem ellenőrizze le, hogy az óraállás megfelelő. (Mérőeszköz: " & clipboardTextCells(i)(0) & ") Folytassam az adatok importálását?", "AVReporter", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
    hiba = True
    Exit For
    End If
    End If
    Catch ex As Exception
    End Try
    End If
    End If
    Else
    If ShowMessageBox("Az adott mérő eszköz az adatbázisban nem szerepel vagy a név hibás! Mérőeszköz: (" & clipboardTextCells(i)(0) & ") Folytassam az adatok importálását?", "AVReporter", MessageBoxOption.YesNo) = Windows.MessageBoxResult.No Then
    hiba = True
    Exit For
    End If
    End If
    Next
    If hiba = False Then
    Try
    SaveIgen = True
    Save()
    Catch ex As Exception
    ShowMessageBox(ex.Message)
    End Try
    End If
    LeolvasottOraks.Load()

    End Sub
  6. avreporter

    avreporter Administrator Staff Member

    Private Sub Screen_Created()
    SetupClipboardImportButton()
    End Sub
  7. avreporter

    avreporter Administrator Staff Member

    Important!!! You have to copy frist section of screen code:

    Imports System.Windows.Browser
    Imports Microsoft.LightSwitch.Threading

Share This Page