Imports System.IO
Imports System.Text
Imports FarPoint.Win.Spread
Imports FarPoint.Win.Spread.Design
Imports System.Windows.Forms

Public Class ExcelUserControl
    Private _ExcelInfo As String
    Private _FileName As String = ""
    Private _EnterMoveNextControl As Boolean
    Public Sub New()
        InitializeComponent()
    End Sub
    Public ReadOnly Property FpSpreadControl() As FpSpread
        Get
            Return Me.SP
        End Get
    End Property
    ReadOnly Property FileName As String
        Get
            Return _FileName
        End Get
    End Property
    Public Property ExcelInfo As String
        Get
            Return _ExcelInfo
        End Get
        Set(ByVal value As String)
            _ExcelInfo = value
        End Set
    End Property
    Public Property EnterMoveNextControl As Boolean
        Get
            Return _EnterMoveNextControl
        End Get
        Set(value As Boolean)
            _EnterMoveNextControl = value
        End Set
    End Property

    Private Sub ExcelUserControl_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        SP.Width = Me.Width
        SPFormula.Width = Me.Width
        SP.Height = Me.Height - SPFormula.Height - 35
    End Sub
    Private Sub ImportXLS_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ImportXLS.ItemClick
        Me.OpenFileDialog1.Filter = "Excel (*.xls) |*.xls;*.xlsx"
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.SP.OpenExcel(Me.OpenFileDialog1.FileName)
            _FileName = Me.OpenFileDialog1.FileName
        End If
    End Sub
    Private Sub ExportXLS_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles ExportXLS.ItemClick
        'Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        Me.SaveFileDialog1.Filter = "Excel (*.xls) |*.xls;*.xlsx"
        If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.SP.SaveExcel(Me.SaveFileDialog1.FileName)
        End If
    End Sub
    Private Sub TrimColumns_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles TrimColumns.ItemClick
        Me.SP.ActiveSheet.ColumnCount = Me.SP.ActiveSheet.ActiveColumnIndex + 1
    End Sub
    Private Sub TrimRows_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles TrimRows.ItemClick
        Me.SP.ActiveSheet.RowCount = Me.SP.ActiveSheet.ActiveRowIndex + 1
    End Sub
    Private Sub BarButtonItem5_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        SetGridLine()
    End Sub
    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.OpenFileDialog1.Filter = "Spread (*.xml) |*.xml"
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.SP.Open(Me.OpenFileDialog1.FileName)
            _FileName = Me.OpenFileDialog1.FileName
        End If
    End Sub
    Private Sub BarButtonItem7_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.SaveFileDialog1.Filter = "Spread (*.xml) |*.xml"
        If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.SP.Save(Me.SaveFileDialog1.FileName, False)
        End If
    End Sub
    Private Sub BarButtonItem8_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem8.ItemClick
        Dim printset As New FarPoint.Win.Spread.PrintInfo()
        printset.ShowGrid = False
        printset.UseMax = True
        printset.ShowPrintDialog = True
        SP.ActiveSheet.PrintInfo = printset

        SP.PrintSheet(SP.ActiveSheet)
    End Sub
    Private Sub BarButtonItem9_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem9.ItemClick
        SetFont()
    End Sub
    Private Sub BarButtonItem10_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem10.ItemClick
        SetColor("Any")
    End Sub
    Private Sub FontToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles FontToolStripMenuItem.Click
        SetFont()
    End Sub
    Private Sub SetFont()
        FontDialog1.ShowColor = True
        If (FontDialog1.ShowDialog() = DialogResult.OK) Then
            Dim _range As FarPoint.Win.Spread.Model.CellRange() = SP.ActiveSheet.GetSelections
            For Each _CellRange As FarPoint.Win.Spread.Model.CellRange In _range
                For ik = _CellRange.Column To _CellRange.Column + _CellRange.ColumnCount - 1
                    For jk = _CellRange.Row To _CellRange.Row + _CellRange.RowCount - 1
                        Dim _ActCell As FarPoint.Win.Spread.Cell = SP.ActiveSheet.Cells(jk, ik)
                        _ActCell.Font = FontDialog1.Font
                        _ActCell.ForeColor = FontDialog1.Color
                    Next
                Next
            Next
        End If
    End Sub
    Private Sub SetColor(_Object As String)
        Dim _ColorDialog As New Windows.Forms.ColorDialog
        Select Case _Object
            Case "CellBackColor"
                If (_ColorDialog.ShowDialog() = DialogResult.OK) Then
                    Dim _range As FarPoint.Win.Spread.Model.CellRange() = SP.ActiveSheet.GetSelections
                    For Each _CellRange As FarPoint.Win.Spread.Model.CellRange In _range
                        For ik = _CellRange.Column To _CellRange.Column + _CellRange.ColumnCount - 1
                            For jk = _CellRange.Row To _CellRange.Row + _CellRange.RowCount - 1
                                Dim _ActCell As FarPoint.Win.Spread.Cell = SP.ActiveSheet.Cells(jk, ik)
                                _ActCell.BackColor = _ColorDialog.Color
                            Next
                        Next
                    Next
                End If

            Case "BorderColor"
                If (_ColorDialog.ShowDialog() = DialogResult.OK) Then

                End If

        End Select
    End Sub
    Private Sub SP_CellClick(sender As System.Object, e As FarPoint.Win.Spread.CellClickEventArgs) Handles SP.CellClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Cursor = New Cursor(Cursor.Current.Handle)
            ContextMenuStrip1.Show(New System.Drawing.Point(Cursor.Position))
        End If
    End Sub
    Private Sub ShowHideGridToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ShowHideGridToolStripMenuItem.Click
        SetGridLine()
    End Sub
    Private Sub SetGridLine()
        Dim HGridLine As FarPoint.Win.Spread.GridLine
        Dim VGridLine As FarPoint.Win.Spread.GridLine
        If Me.SP.ActiveSheet.HorizontalGridLine.Type = GridLineType.None Then
            HGridLine = New FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.Flat, System.Drawing.Color.LightGray)
            VGridLine = New FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.Flat, System.Drawing.Color.LightGray)
        Else
            HGridLine = New FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.None, System.Drawing.Color.Transparent)
            VGridLine = New FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.None, System.Drawing.Color.Transparent)
        End If

        Me.SP.ActiveSheet.HorizontalGridLine = HGridLine
        Me.SP.ActiveSheet.VerticalGridLine = VGridLine
    End Sub
    Private Sub BackColorToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BackColorToolStripMenuItem.Click
        SetColor("CellBackColor")
    End Sub
    Private Sub BorderToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BorderToolStripMenuItem.Click
        FarPoint.Win.Spread.Design.ExternalDialogs.BorderEditor(SP)
    End Sub
    Private Sub BarButtonItem11_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem11.ItemClick
        Dim printset As New FarPoint.Win.Spread.PrintInfo()
        printset.ShowGrid = False
        printset.UseMax = True
        'printset.ShowPrintDialog = True
        printset.Preview = True
        SP.ActiveSheet.PrintInfo = printset

        SP.PrintSheet(SP.ActiveSheet)


    End Sub
    Private Sub CellTypeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CellTypeToolStripMenuItem.Click
        FarPoint.Win.Spread.Design.ExternalDialogs.CellTypeEditor(SP)
    End Sub
    Private Sub BarButtonItem12_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem12.ItemClick
        Dim _FpPdf As FarPoint.Win.Spread.PrintInfo = New FarPoint.Win.Spread.PrintInfo
        Dim _SaveDialog As New SaveFileDialog
        _SaveDialog.Filter = "PDF (*.pdf) |*.pdf"
        _SaveDialog.DefaultExt = ".pdf"
        _SaveDialog.FileName = "Munkalap"

        If (_SaveDialog.ShowDialog() = DialogResult.OK) Then


            SP.ActiveSheet.ColumnHeader.Visible = False
            SP.ActiveSheet.RowHeader.Visible = False

            _FpPdf.PdfFileName = _SaveDialog.FileName
            '_FpPdf.PdfWriteMode = PdfWriteMode.New
            '_FpPdf.PdfWriteTo = PdfWriteTo.File
            _FpPdf.PrintToPdf = True
            '_FpPdf.PaperSize = New System.Drawing.Printing.PaperSize("A4", 8, 11)
            '_FpPdf.Centering = Centering.Both
            _FpPdf.Preview = False

            SP.ActiveSheet.PrintInfo = _FpPdf
            SP.PrintSheet(SP.ActiveSheet)
            'SP.SafePrint(SP, 0)

            SP.ActiveSheet.ColumnHeader.Visible = True
            SP.ActiveSheet.RowHeader.Visible = True
        End If
    End Sub
End Class
