How can I export data to CSV file and clipboard? For example: I would like to use energy price export from AVReporter WEBPublisher.
You can do it, you have to above Visual Basic code your application. First step: Add method your screen: Export and ClipboardExport
Next Step, create event for click Private Sub SetupExportButton() 'Export CSV AddHandler Me.FindControl("Export").ControlAvailable, (Function(object1, eventargs1) Dim btnExport As System.Windows.Controls.Button = DirectCast(eventargs1.Control, System.Windows.Controls.Button) AddHandler btnExport.Click, (Function(object2, eventargs2) Dim csv As String = getCSV(",") Dim dialog = New System.Windows.Controls.SaveFileDialog() dialog.Filter = "CSV (*.csv)|*.csv" Try If dialog.ShowDialog() = True Then Using stream As New StreamWriter(dialog.OpenFile(), System.Text.Encoding.UTF8) stream.Write(csv) stream.Close() End Using End If Catch ex As Exception 'ShowMessageBox("Hiba a mentéskor!", "Hiba", MessageBoxOption.Ok) End Try End Function) End Function) End Sub Private Sub SetupClipboardExportButton() 'Export Clipboard AddHandler Me.FindControl("ClipboardExport").ControlAvailable, (Function(object1, eventargs1) Dim btnClipboardExport As System.Windows.Controls.Button = DirectCast(eventargs1.Control, System.Windows.Controls.Button) AddHandler btnClipboardExport.Click, (Function(object2, eventargs2) Dim csv As String = getCSV(vbTab) Try System.Windows.Clipboard.SetText(csv) Catch ex As Exception End Try End Function) End Function) End Sub End Function
Next step create setup for button event, you have to copy this code in screen_created method: Private Sub Screen_Created() SetupExportButton() SetupClipboardExportButton() End Sub
Important!!! Copy first section of your screen code: Imports System.Windows.Browser Imports Microsoft.LightSwitch.Threading
Finally: you have to create Table creator function for CSV and clipboard export. Private Function getCSV(delimiter As String) As String Dim csv As New System.Text.StringBuilder() Try csv.AppendFormat("Villamos elsz. csoport" & delimiter & "Érvénnyesség kezdete" & delimiter & "Elosztói teljesítménydíj (Ft/kW/hó)" & delimiter & "Elosztói alapdíj (Ft/csatl.pont/hó)" & delimiter & "Rend. szolg. díja (Ft/kWh)" & delimiter & "Általános átviteli díj (Ft/kWh)" & delimiter & "Elosztói forgalmi díj (Ft/kWh)" & delimiter & "Elosztói veszteség díj (Ft/kWh)" & delimiter & "Elosztói menetrend kiegyensúlyozási díj (Ft/kWh)" & delimiter & "Induktív meddőenergia díj (Ft/kVArh)" & delimiter & "Teljesítmény túllépési díj (Ft/kW/hó)" & delimiter & "Átadott vill. en. egységár (Ft/kWh)" & delimiter & "Támogatások egységára (Ft/kWh)" & delimiter & "Átadott vill. energia tény. egységár (Ft/kWh)" & delimiter & "Támogatások tény. egységára (Ft/kWh)" & delimiter & "Megjegyzés" & delimiter & "Módosító" & delimiter & "Módosítás ideje" & delimiter & System.Environment.NewLine) For Each item As VillRHD In VillRHDs csv.AppendFormat(item.VillelszamolasiBesor.BesorolasNeve & delimiter & item.Datum & delimiter & item.ElosztoiTeljesitmenydij & delimiter & item.ElosztoiAlapdij & delimiter & item.RendszerSzolgDija & delimiter & item.AltAtviteliDij & delimiter & item.ElosztoiForgDij & delimiter & item.ElosztoiVesztDij & delimiter & item.EloszMenetrendEgy & delimiter & item.MeddoWattosDij & delimiter & item.TeljesitTullepesiDij & delimiter & item.AtadottVillEnEgysegar & delimiter & item.TamogatásokEgysegara & delimiter & item.AtadottVillEnEgysegarTeny & delimiter & item.TamogatásokEgysegaraTeny & delimiter & item.Megjegyzes & delimiter & item.ModUser & delimiter & item.ModTime & delimiter & System.Environment.NewLine, item) Next Return csv.ToString(0, csv.Length - 1) Catch ex As Exception End Try End Function
GETCSV function exports Lightswitch query data to a string, we can define separator. The separator is "," for CSV file and for Cliboard vbTab. It is practical solution, because we can easily modify for customer requirements.