Imports C1.Silverlight.FlexGrid
Imports C1.Silverlight.Excel
Imports System.IO

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        ' populate the grid
        Dim products = Product.GetProducts(250)
        products(0).Price = -10
        products(1).Price = -10
        C1FlexGrid1.ItemsSource = products


    End Sub

    Private Sub btnExport_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnExport.Click
        Dim dlg = New SaveFileDialog()
        dlg.DefaultExt = "xlsx"
        dlg.Filter = "Excel Workbook (*.xlsx)|*.xlsx|" & "HTML File (*.htm;*.html)|*.htm;*.html|" & "Comma Separated Values (*.csv)|*.csv|" & "Text File (*.txt)|*.txt"
        If dlg.ShowDialog().Value Then
            Using s = dlg.OpenFile()
                ' dlg.FilterIndex is not working properly (SL bug) so we check
                ' the file extension to chose the proper save format.
                Dim ext = System.IO.Path.GetExtension(dlg.SafeFileName).ToLower()
                Select Case ext
                    Case ".htm", ".html"
                        C1FlexGrid1.Save(s, C1.Silverlight.FlexGrid.FileFormat.Html, SaveOptions.Formatted)
                    Case ".csv"
                        C1FlexGrid1.Save(s, C1.Silverlight.FlexGrid.FileFormat.Csv, SaveOptions.Formatted)
                    Case ".txt"
                        C1FlexGrid1.Save(s, C1.Silverlight.FlexGrid.FileFormat.Text, SaveOptions.Formatted)
                    Case Else
                        Save(s, C1FlexGrid1)
                End Select
            End Using
        End If
    End Sub

    Public Sub Save(ByVal stream As Stream, ByVal flexgrid As C1FlexGrid)
        ' create the book to save
        Dim book = New C1XLBook()
        book.Sheets.Clear()
        Dim xlSheet = book.Sheets.Add("Sheet1")
        ExcelExport.ExcelFilter.Save(flexgrid, xlSheet)
        

        ' save the book
        book.Save(stream, C1.Silverlight.Excel.FileFormat.OpenXml)
    End Sub
    'Private Sub _btnSaveText_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    '    Dim dlg = New SaveFileDialog()
    '    dlg.DefaultExt = "txt"
    '    dlg.Filter = "Text files (*.txt)|*.txt"
    '    If dlg.ShowDialog().Value Then
    '        Using s = dlg.OpenFile()
    '            'C1FlexGrid1.Save(s, FileFormat.Text, SaveOptions.Formatted)
    '        End Using
    '    End If
    'End Sub
    'Private Sub _btnSaveHtml_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    '    Dim dlg = New SaveFileDialog()
    '    dlg.DefaultExt = "html"
    '    dlg.Filter = "HTML files (*.htm;*.html)|*.htm;*.html"
    '    If dlg.ShowDialog().Value Then
    '        Using s = dlg.OpenFile()
    '            'C1FlexGrid1.Save(s, FileFormat.Html, SaveOptions.Formatted)
    '        End Using
    '    End If
    'End Sub
    'Private Sub _btnSavePdf_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    '    Dim dlg = New SaveFileDialog()
    '    dlg.DefaultExt = "pdf"
    '    dlg.Filter = "PDF files (*.pdf)|*.pdf"
    '    If dlg.ShowDialog().Value Then
    '        Using s = dlg.OpenFile()
    '            'C1FlexGrid1.SavePdf(s)
    '        End Using
    '    End If
    'End Sub
End Class