{"id":448,"date":"2013-10-23T23:00:23","date_gmt":"2013-10-24T03:00:23","guid":{"rendered":"http:\/\/binaryworld.net\/blogs\/?p=448"},"modified":"2013-10-25T12:02:10","modified_gmt":"2013-10-25T16:02:10","slug":"how-to-export-grid-data-as-excel-in-lightswitch-web-application","status":"publish","type":"post","link":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/","title":{"rendered":"How to export grid data as excel in LightSwitch Web Application"},"content":{"rendered":"<p>Someone recently asked me how to export LightSwitch grid to excel format. Well sounds easy in Desktop Mode coz there is inbuilt option when you publish application in Desktop mode but what about when application is published as web app?<\/p>\n<p>I am going to show two different techniques First one is export as true Excel file and second technique is export as csv format but save as *.xls so it can open comma separated file in excel&#8230;<\/p>\n<p>&#8211; To use below code create a button on your search screen and name it ExportCSV (or something else but I am using that name in this example)<br \/>\n&#8211; Right click on your button and click Edit Execute code and it will open code window.<br \/>\n&#8211; In my case screen name was SearchOrdersSearchQuery so place some initialize code such as Adding Event Handler<br \/>\n&#8211; Couple of things to notice in below code. First we register some event handlers, second we use<br \/>\nstream As New StreamWriter(dialog.OpenFile()) rather than using file paths because in web mode you don&#8217;t have access to host machine file system direct way for security reason so LightSwitch Provides you access to Stream where you can write data.<\/p>\n<h2>Technique-1: Export as\u00a0CSV &#8211; comma\u00a0separated file and Save as Excel<\/h2>\n<pre class=\"brush: vbnet; gutter: true; first-line: 1; highlight: []; html-script: false\">Imports System.Text\r\n\r\nNamespace LightSwitchApplication\r\n    Public Class SearchOrdersSearchQuery\r\n        Private Sub SearchOrdersSearchQuery_Created()\r\n            Dim CSVButton = Me.FindControl(&quot;ExportCSV&quot;)\r\n            AddHandler CSVButton.ControlAvailable, AddressOf ExportCSV_ControlAvailable\r\n        End Sub\r\n\r\n        Private Sub ExportCSV_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)\r\n            RemoveHandler Me.FindControl(&quot;ExportCSV&quot;).ControlAvailable, AddressOf ExportCSV_ControlAvailable\r\n            Dim Button = DirectCast(e.Control, Button)\r\n            AddHandler Button.Click, AddressOf ExportCSV_Click\r\n        End Sub\r\n\r\n        Private Sub ExportCSV_Click(sender As Object, e As System.Windows.RoutedEventArgs)\r\n            Dim collectionProperty As Microsoft.LightSwitch.Details.Client.IScreenCollectionProperty = Me.Details.Properties.OrdersSearchQuery\r\n            Dim intPageSize = collectionProperty.PageSize\r\n            &#039;Get the Current PageSize and store to variable\r\n            collectionProperty.PageSize = 0\r\n\r\n            Dim dialog = New SaveFileDialog()\r\n            dialog.Filter = &quot;Excel (*.xls)|*.xls&quot;\r\n            If dialog.ShowDialog() = True Then\r\n\r\n                Using stream As New StreamWriter(dialog.OpenFile())\r\n                    Dim csv As String = GetCSV()\r\n                    stream.Write(csv)\r\n                    stream.Close()\r\n                    Me.ShowMessageBox(&quot;Excel File Created Successfully. &quot; &amp; vbCrLf &amp; &quot;NOTE: When you open excel file and if you receive prompt about invalid format then just click yes to continue.&quot;, &quot;Excel Export&quot;, MessageBoxOption.Ok)\r\n                End Using\r\n            End If\r\n            collectionProperty.PageSize = intPageSize\r\n            &#039;Reset the Current PageSize\r\n        End Sub\r\n\r\n        &#039;\/\/Selected columns\r\n        &#039;Private Function GetCSV() As String\r\n        &#039;    Dim csv As New StringBuilder()\r\n        &#039;    csv.AppendFormat(&quot;Heading1 , Heading2, Heading3&quot; &amp; &quot;,&quot; &amp; System.Environment.NewLine)\r\n        &#039;    For Each c In Me.EvacuationRegistrations\r\n        &#039;        csv.AppendFormat(Convert.ToString(c.first_name) &amp; &quot;,&quot; &amp; Convert.ToString(c.last_name) &amp; &quot;,&quot; &amp; Convert.ToString(c.location) &amp; &quot;,&quot; &amp; System.Environment.NewLine, c)\r\n        &#039;    Next\r\n        &#039;    Return csv.ToString(0, (csv.Length - 1))\r\n        &#039;End Function\r\n\r\n        Private Function GetCSV() As String\r\n            Dim csv As New StringBuilder()\r\n\r\n            Dim i As Integer = 0\r\n\r\n            For Each orderRow In OrdersSearchQuery\r\n                &#039;\/\/HEADER\r\n                If i = 0 Then\r\n                    Dim c As Integer = 0\r\n                    For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                        If c &gt; 0 Then\r\n                            csv.Append(vbTab)\r\n                        End If\r\n                        c = c + 1\r\n                        csv.Append(prop.DisplayName)\r\n                    Next\r\n                End If\r\n                csv.AppendLine(&quot;&quot;)\r\n\r\n                &#039;\/\/DATA ROWS\r\n\r\n                Dim c1 As Integer = 0\r\n                For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                    If c1 &gt; 0 Then\r\n                        csv.Append(vbTab)\r\n                    End If\r\n                    c1 = c1 + 1\r\n                    csv.Append(prop.Value)\r\n                Next\r\n                i = i + 1\r\n            Next\r\n\r\n            If csv.Length &gt; 0 Then\r\n                Return csv.ToString(0, csv.Length - 1)\r\n            Else\r\n                Return &quot;&quot;\r\n            End If\r\n        End Function\r\n\r\n        &#039;\/\/Selected columns\r\n        Private Function GetCSV_old() As String\r\n            Dim csv As New StringBuilder()\r\n            Dim i As Integer = 0\r\n\r\n            For Each a In OrdersSearchQuery\r\n                If i = 0 Then\r\n                    csv.AppendFormat(&quot;OrderId&quot; &amp; &quot;,&quot; &amp; &quot;OrderDate&quot; &amp; System.Environment.NewLine, a)\r\n                End If\r\n                csv.AppendFormat(a.OrderID &amp; &quot;,&quot; &amp; a.OrderDate &amp; System.Environment.NewLine, a)\r\n                i = i + 1\r\n            Next\r\n\r\n            If csv.Length &gt; 0 Then\r\n                Return csv.ToString(0, csv.Length - 1)\r\n            Else\r\n                Return &quot;&quot;\r\n            End If\r\n        End Function\r\n\r\n    End Class\r\n\r\nEnd Namespace<\/pre>\n<p>Once you run LightSwitch application you can click on button &#8220;ExportToCVS&#8221; and it will generate tab separated file which can be opened in Excel.<\/p>\n<p>Below post was very useful where I got this trick.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"LBYhyiIhPw\"><p><a href=\"https:\/\/hgminerva.wordpress.com\/2012\/05\/28\/how-to-export-data-to-csv-using-microsoft-lightswitch-web-application\/\">How to export data to CSV using Microsoft Lightswitch Web&nbsp;Application.<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to export data to CSV using Microsoft Lightswitch Web&nbsp;Application.&#8221; &#8212; H.G. Minerva\" src=\"https:\/\/hgminerva.wordpress.com\/2012\/05\/28\/how-to-export-data-to-csv-using-microsoft-lightswitch-web-application\/embed\/#?secret=BWVLgNxOv7#?secret=LBYhyiIhPw\" data-secret=\"LBYhyiIhPw\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p><a href=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export-button-click.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-452\" title=\"lightswitch-excel-web-export-button-click\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export-button-click.jpg\" alt=\"\" width=\"877\" height=\"525\" srcset=\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export-button-click.jpg 877w, https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export-button-click-300x179.jpg 300w\" sizes=\"(max-width: 877px) 100vw, 877px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"lightswitch-excel-web-export\" src=\"http:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export.jpg\" alt=\"\" width=\"458\" height=\"374\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Technique-2: Export as\u00a0true Excel file (Use ExcelWriter Class)<\/h2>\n<p>Here is full code which shows how to export data as true excel file (Binary format) which cant be viewed in text editor.<\/p>\n<pre class=\"brush: vbnet; gutter: true; first-line: 1\">Imports System.Text\r\n\r\nNamespace LightSwitchApplication\r\n    Public Class SearchOrdersSearchQuery\r\n\r\n        Private Sub SearchOrdersSearchQuery_Created()\r\n            Dim CSVButton = Me.FindControl(&quot;ExportToExcel&quot;)\r\n            AddHandler CSVButton.ControlAvailable, AddressOf ExportCSV_ControlAvailable\r\n        End Sub\r\n\r\n        Private Sub SearchClick_Execute()\r\n            OrdersSearchQuery.Refresh()\r\n        End Sub\r\n\r\n        Private Sub ExportCSV_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)\r\n            RemoveHandler Me.FindControl(&quot;ExportToExcel&quot;).ControlAvailable, AddressOf ExportCSV_ControlAvailable\r\n            Dim Button = DirectCast(e.Control, Button)\r\n            AddHandler Button.Click, AddressOf ExportCSV_Click\r\n        End Sub\r\n\r\n        Dim EXPORT_FORMAT As String = &quot;EXCEL&quot; &#039;\/\/ or CSV\r\n        Private Sub ExportCSV_Click(sender As Object, e As System.Windows.RoutedEventArgs)\r\n            If EXPORT_FORMAT = &quot;CSV&quot; Then\r\n                ExportAsCSV()\r\n            Else\r\n                ExportAsExcel()\r\n            End If\r\n        End Sub\r\n\r\n        Sub ExportAsExcel()\r\n            Dim collectionProperty As Microsoft.LightSwitch.Details.Client.IScreenCollectionProperty = Me.Details.Properties.OrdersSearchQuery\r\n            Dim intPageSize = collectionProperty.PageSize\r\n            &#039;Get the Current PageSize and store to variable\r\n            collectionProperty.PageSize = 0\r\n\r\n\r\n            &#039;Dim stream As New FileStream(&quot;demo.xls&quot;, FileMode.OpenOrCreate)\r\n            &#039;Dim writer As New ExcelWriter(stream)\r\n            &#039;writer.BeginWrite()\r\n            &#039;writer.WriteCell(0, 0, &quot;ExcelWriter Demo&quot;)\r\n            &#039;writer.WriteCell(1, 0, &quot;int&quot;)\r\n            &#039;writer.WriteCell(2, 0, &quot;double&quot;)\r\n            &#039;writer.WriteCell(1, 1, 10)\r\n            &#039;writer.WriteCell(2, 1, 1.5)\r\n            &#039;writer.WriteCell(3, 0, &quot;empty&quot;)\r\n            &#039;writer.WriteCell(3, 1)\r\n            &#039;writer.EndWrite()\r\n            &#039;stream.Close()\r\n\r\n\r\n            Dim dialog = New SaveFileDialog()\r\n            dialog.Filter = &quot;Excel (*.xls)|*.xls&quot;\r\n            If dialog.ShowDialog() = True Then\r\n                Using fileStream As Stream = dialog.OpenFile()\r\n                    Using writer1 As New ExcelWriter(fileStream)\r\n                        Dim csv As String = GetCSV()\r\n                        writer1.BeginWrite()\r\n                        Dim rowsWritten As Integer = WriteToExcel(writer1)\r\n                        writer1.EndWrite()\r\n                        Me.ShowMessageBox(&quot;Excel File Created Successfully. Total &quot; &amp; rowsWritten &amp; &quot; written.&quot;, &quot;Excel Export&quot;, MessageBoxOption.Ok)\r\n                    End Using\r\n                End Using\r\n            End If\r\n            collectionProperty.PageSize = intPageSize\r\n            &#039;Reset the Current PageSize\r\n        End Sub\r\n        Sub ExportAsCSV()\r\n            Dim collectionProperty As Microsoft.LightSwitch.Details.Client.IScreenCollectionProperty = Me.Details.Properties.OrdersSearchQuery\r\n            Dim intPageSize = collectionProperty.PageSize\r\n            &#039;Get the Current PageSize and store to variable\r\n            collectionProperty.PageSize = 0\r\n\r\n            Dim dialog = New SaveFileDialog()\r\n            dialog.Filter = &quot;Text Files (*.csv)|*.csv&quot;\r\n            If dialog.ShowDialog() = True Then\r\n\r\n                Using stream As New StreamWriter(dialog.OpenFile())\r\n                    Dim csv As String = GetCSV()\r\n                    stream.Write(csv)\r\n                    stream.Close()\r\n                    Me.ShowMessageBox(&quot;CSV File Created Successfully. &quot; &amp; vbCrLf &amp; &quot;NOTE: When you open excel file and if you receive prompt about invalid format then just click yes to continue.&quot;, &quot;Excel Export&quot;, MessageBoxOption.Ok)\r\n                End Using\r\n            End If\r\n            collectionProperty.PageSize = intPageSize\r\n            &#039;Reset the Current PageSize\r\n        End Sub\r\n        &#039;\/\/Selected columns\r\n        &#039;Private Function GetCSV() As String\r\n        &#039;    Dim csv As New StringBuilder()\r\n        &#039;    csv.AppendFormat(&quot;Heading1 , Heading2, Heading3&quot; &amp; &quot;,&quot; &amp; System.Environment.NewLine)\r\n        &#039;    For Each c In Me.EvacuationRegistrations\r\n        &#039;        csv.AppendFormat(Convert.ToString(c.first_name) &amp; &quot;,&quot; &amp; Convert.ToString(c.last_name) &amp; &quot;,&quot; &amp; Convert.ToString(c.location) &amp; &quot;,&quot; &amp; System.Environment.NewLine, c)\r\n        &#039;    Next\r\n        &#039;    Return csv.ToString(0, (csv.Length - 1))\r\n        &#039;End Function\r\n\r\n        Private Function GetCSV() As String\r\n            Dim csv As New StringBuilder()\r\n\r\n            Dim i As Integer = 0\r\n\r\n            For Each orderRow In OrdersSearchQuery\r\n                &#039;\/\/HEADER\r\n                If i = 0 Then\r\n                    Dim c As Integer = 0\r\n                    For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                        If c &gt; 0 Then\r\n                            csv.Append(vbTab)\r\n                        End If\r\n                        c = c + 1\r\n                        csv.Append(prop.DisplayName)\r\n                    Next\r\n                End If\r\n                csv.AppendLine(&quot;&quot;)\r\n\r\n                &#039;\/\/DATA ROWS\r\n\r\n                Dim c1 As Integer = 0\r\n                For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                    If c1 &gt; 0 Then\r\n                        csv.Append(vbTab)\r\n                    End If\r\n                    c1 = c1 + 1\r\n                    csv.Append(prop.Value)\r\n                Next\r\n                i = i + 1\r\n            Next\r\n\r\n            If csv.Length &gt; 0 Then\r\n                Return csv.ToString(0, csv.Length - 1)\r\n            Else\r\n                Return &quot;&quot;\r\n            End If\r\n        End Function\r\n\r\n        &#039;\/\/Selected columns\r\n        Private Function GetCSV_old() As String\r\n            Dim csv As New StringBuilder()\r\n            Dim i As Integer = 0\r\n\r\n            For Each a In OrdersSearchQuery\r\n                If i = 0 Then\r\n                    csv.AppendFormat(&quot;OrderId&quot; &amp; &quot;,&quot; &amp; &quot;OrderDate&quot; &amp; System.Environment.NewLine, a)\r\n                End If\r\n                csv.AppendFormat(a.OrderID &amp; &quot;,&quot; &amp; a.OrderDate &amp; System.Environment.NewLine, a)\r\n                i = i + 1\r\n            Next\r\n\r\n            If csv.Length &gt; 0 Then\r\n                Return csv.ToString(0, csv.Length - 1)\r\n            Else\r\n                Return &quot;&quot;\r\n            End If\r\n        End Function\r\n\r\n        Private Function WriteToExcel(writer As ExcelWriter) As Integer\r\n            &#039;writer.WriteCell(0, 0, &quot;ExcelWriter Demo&quot;)\r\n            &#039;writer.WriteCell(1, 0, &quot;int&quot;)\r\n            &#039;writer.WriteCell(1, 1, 10)\r\n            &#039;writer.WriteCell(2, 0, &quot;double&quot;)\r\n            &#039;writer.WriteCell(2, 1, 1.5)\r\n            &#039;writer.WriteCell(3, 0, &quot;empty&quot;)\r\n            &#039;writer.WriteCell(3, 1)\r\n\r\n            Dim rowNum As Integer = 0\r\n\r\n            For Each orderRow In OrdersSearchQuery\r\n                &#039;\/\/HEADER\r\n                If rowNum = 0 Then\r\n                    WriteExcelHeaderRow(orderRow, writer)\r\n                End If\r\n\r\n                &#039;\/\/DATA ROWS\r\n                WriteExcelDataRow(rowNum, orderRow, writer)\r\n\r\n                rowNum = rowNum + 1\r\n            Next\r\n\r\n            Return rowNum\r\n        End Function\r\n\r\n        Private Sub WriteExcelHeaderRow(orderRow As Order, writer As ExcelWriter)\r\n            Dim colNum1 As Integer = 0\r\n            For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                writer.WriteCell(0, colNum1, prop.DisplayName)\r\n                colNum1 = colNum1 + 1\r\n            Next\r\n        End Sub\r\n\r\n        Private Sub WriteExcelDataRow(rowNum As Integer, orderRow As Order, writer As ExcelWriter)\r\n            Dim colNum2 As Integer = 0\r\n            For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()\r\n                Select Case prop.PropertyType.Name\r\n                    Case &quot;Int32&quot;, &quot;Int64&quot;\r\n                        writer.WriteCell(rowNum, colNum2, Convert.ToInt64(prop.Value))\r\n                    Case &quot;Double&quot;, &quot;Decimal&quot;\r\n                        writer.WriteCell(rowNum, colNum2, Convert.ToDouble(prop.Value))\r\n                    Case Else\r\n                        writer.WriteCell(rowNum, colNum2, Convert.ToString(prop.Value))\r\n                End Select\r\n\r\n\r\n                colNum2 = colNum2 + 1\r\n            Next\r\n        End Sub\r\n    End Class\r\n\r\n    &#039;&#039;&#039; &lt;summary&gt;\r\n    &#039;&#039;&#039; Produces Excel file without using Excel\r\n    &#039;&#039;&#039; &lt;\/summary&gt;\r\n    Public Class ExcelWriter\r\n        Implements IDisposable\r\n\r\n        Private flagStarted As Boolean\r\n\r\n        Private stream As Stream\r\n        Private writer As BinaryWriter\r\n\r\n        Private clBegin As UShort() = {&amp;H809, 8, 0, &amp;H10, 0, 0}\r\n        Private clEnd As UShort() = {&amp;HA, 0}\r\n\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Initializes a new instance of the &lt;see cref=&quot;ExcelWriter&quot;\/&gt; class.\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;stream&quot;&gt;The stream.&lt;\/param&gt;\r\n        Public Sub New(stream As Stream)\r\n            Me.stream = stream\r\n            writer = New BinaryWriter(stream)\r\n        End Sub\r\n\r\n        Private Sub WriteUshortArray(value As UShort())\r\n            For i As Integer = 0 To value.Length - 1\r\n                writer.Write(value(i))\r\n            Next\r\n        End Sub\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Writes the text cell value.\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;row&quot;&gt;The row.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;col&quot;&gt;The col.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;value&quot;&gt;The string value.&lt;\/param&gt;\r\n        Public Sub WriteCell(row As Integer, col As Integer, value As String)\r\n            Dim clData As UShort() = {&amp;H204, 0, 0, 0, 0, 0}\r\n            Dim iLen As Integer = value.Length\r\n            Dim plainText As Byte() = StringToAscii(value) &#039;\/\/Encoding.ASCII.GetBytes(value) \/\/Becuase SilverLight doesnt have ASCII Encoding I wrote little function\r\n            clData(1) = CUShort(8 + iLen)\r\n            clData(2) = CUShort(row)\r\n            clData(3) = CUShort(col)\r\n            clData(5) = CUShort(iLen)\r\n            WriteUshortArray(clData)\r\n            writer.Write(plainText)\r\n        End Sub\r\n\r\n        Private Function StringToAscii(s As String) As Byte()\r\n            Dim retval As Byte() = New Byte(s.Length - 1) {}\r\n            For ix As Integer = 0 To s.Length - 1\r\n                Dim ch As Char = s(ix)\r\n                If AscW(ch) &lt;= &amp;H7F Then\r\n                    retval(ix) = CByte(AscW(ch))\r\n                Else\r\n                    retval(ix) = CByte(AscW(&quot;?&quot;c))\r\n                End If\r\n            Next\r\n            Return retval\r\n        End Function\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Writes the integer cell value.\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;row&quot;&gt;The row number.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;col&quot;&gt;The column number.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;value&quot;&gt;The value.&lt;\/param&gt;\r\n        Public Sub WriteCell(row As Integer, col As Integer, value As Integer)\r\n            Dim clData As UShort() = {&amp;H27E, 10, 0, 0, 0}\r\n            clData(2) = CUShort(row)\r\n            clData(3) = CUShort(col)\r\n            WriteUshortArray(clData)\r\n            Dim iValue As Integer = (value &lt;&lt; 2) Or 2\r\n            writer.Write(iValue)\r\n        End Sub\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Writes the double cell value.\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;row&quot;&gt;The row number.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;col&quot;&gt;The column number.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;value&quot;&gt;The value.&lt;\/param&gt;\r\n        Public Sub WriteCell(row As Integer, col As Integer, value As Double)\r\n            Dim clData As UShort() = {&amp;H203, 14, 0, 0, 0}\r\n            clData(2) = CUShort(row)\r\n            clData(3) = CUShort(col)\r\n            WriteUshortArray(clData)\r\n            writer.Write(value)\r\n        End Sub\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Writes the empty cell.\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;row&quot;&gt;The row number.&lt;\/param&gt;\r\n        &#039;&#039;&#039; &lt;param name=&quot;col&quot;&gt;The column number.&lt;\/param&gt;\r\n        Public Sub WriteCell(row As Integer, col As Integer)\r\n            Dim clData As UShort() = {&amp;H201, 6, 0, 0, &amp;H17}\r\n            clData(2) = CUShort(row)\r\n            clData(3) = CUShort(col)\r\n            WriteUshortArray(clData)\r\n        End Sub\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Must be called once for creating XLS file header\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        Public Sub BeginWrite()\r\n            flagStarted = True\r\n            WriteUshortArray(clBegin)\r\n        End Sub\r\n\r\n        &#039;&#039;&#039; &lt;summary&gt;\r\n        &#039;&#039;&#039; Ends the writing operation, but do not close the stream\r\n        &#039;&#039;&#039; &lt;\/summary&gt;\r\n        Public Sub EndWrite()\r\n            WriteUshortArray(clEnd)\r\n            writer.Flush()\r\n            flagStarted = False\r\n        End Sub\r\n\r\n#Region &quot;IDisposable Support&quot;\r\n        Private disposedValue As Boolean &#039; To detect redundant calls\r\n\r\n        &#039; IDisposable\r\n        Protected Overridable Sub Dispose(disposing As Boolean)\r\n            If Not Me.disposedValue Then\r\n                If disposing Then\r\n                    &#039; TODO: dispose managed state (managed objects).\r\n                End If\r\n\r\n                If writer IsNot Nothing Then\r\n                    writer.Dispose()\r\n                End If\r\n\r\n                &#039; TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.\r\n                &#039; TODO: set large fields to null.\r\n            End If\r\n            Me.disposedValue = True\r\n        End Sub\r\n\r\n        &#039; TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.\r\n        &#039;Protected Overrides Sub Finalize()\r\n        &#039;    &#039; Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.\r\n        &#039;    Dispose(False)\r\n        &#039;    MyBase.Finalize()\r\n        &#039;End Sub\r\n\r\n        &#039; This code added by Visual Basic to correctly implement the disposable pattern.\r\n        Public Sub Dispose() Implements IDisposable.Dispose\r\n            &#039; Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.\r\n            Dispose(True)\r\n            GC.SuppressFinalize(Me)\r\n        End Sub\r\n#End Region\r\n\r\n    End Class\r\n\r\nEnd Namespace<\/pre>\n<p>Hope you find this useful !!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone recently asked me how to export LightSwitch grid to excel format. Well sounds easy in Desktop Mode coz there is inbuilt option when you publish application in Desktop mode but what about when application is published as web app? &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\"> <span class=\"screen-reader-text\">How to export grid data as excel in LightSwitch Web Application<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":454,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76,65],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog\" \/>\r\n<meta property=\"og:description\" content=\"Someone recently asked me how to export LightSwitch grid to excel format. Well sounds easy in Desktop Mode coz there is inbuilt option when you publish application in Desktop mode but what about when application is published as web app? &hellip; How to export grid data as excel in LightSwitch Web Application Read More &raquo;\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\" \/>\r\n<meta property=\"og:site_name\" content=\"BinaryWorld Blog\" \/>\r\n<meta property=\"article:published_time\" content=\"2013-10-24T03:00:23+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-10-25T16:02:10+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg\" \/>\r\n\t<meta property=\"og:image:width\" content=\"458\" \/>\r\n\t<meta property=\"og:image:height\" content=\"374\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\r\n<meta name=\"author\" content=\"Binary World\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binary World\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\",\"url\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\",\"name\":\"How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog\",\"isPartOf\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg\",\"datePublished\":\"2013-10-24T03:00:23+00:00\",\"dateModified\":\"2013-10-25T16:02:10+00:00\",\"author\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\"},\"breadcrumb\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage\",\"url\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg\",\"contentUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg\",\"width\":\"458\",\"height\":\"374\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryworld.net\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to export grid data as excel in LightSwitch Web Application\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\",\"url\":\"https:\/\/binaryworld.net\/blogs\/\",\"name\":\"BinaryWorld Blog\",\"description\":\"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\",\"name\":\"Binary World\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"caption\":\"Binary World\"},\"description\":\"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.\",\"url\":\"https:\/\/binaryworld.net\/blogs\/author\/admin\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/","og_locale":"en_US","og_type":"article","og_title":"How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog","og_description":"Someone recently asked me how to export LightSwitch grid to excel format. Well sounds easy in Desktop Mode coz there is inbuilt option when you publish application in Desktop mode but what about when application is published as web app? &hellip; How to export grid data as excel in LightSwitch Web Application Read More &raquo;","og_url":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/","og_site_name":"BinaryWorld Blog","article_published_time":"2013-10-24T03:00:23+00:00","article_modified_time":"2013-10-25T16:02:10+00:00","og_image":[{"width":"458","height":"374","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg","type":"image\/jpeg"}],"author":"Binary World","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binary World","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/","url":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/","name":"How to export grid data as excel in LightSwitch Web Application - BinaryWorld Blog","isPartOf":{"@id":"https:\/\/binaryworld.net\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage"},"image":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage"},"thumbnailUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg","datePublished":"2013-10-24T03:00:23+00:00","dateModified":"2013-10-25T16:02:10+00:00","author":{"@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0"},"breadcrumb":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#primaryimage","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg","contentUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2013\/10\/lightswitch-excel-web-export1.jpg","width":"458","height":"374"},{"@type":"BreadcrumbList","@id":"https:\/\/binaryworld.net\/blogs\/how-to-export-grid-data-as-excel-in-lightswitch-web-application\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryworld.net\/blogs\/"},{"@type":"ListItem","position":2,"name":"How to export grid data as excel in LightSwitch Web Application"}]},{"@type":"WebSite","@id":"https:\/\/binaryworld.net\/blogs\/#website","url":"https:\/\/binaryworld.net\/blogs\/","name":"BinaryWorld Blog","description":"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0","name":"Binary World","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","caption":"Binary World"},"description":"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.","url":"https:\/\/binaryworld.net\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/448"}],"collection":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":0,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media\/454"}],"wp:attachment":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}