thongkorn โพสต์ 2019-2-10 20:06:16

[VB.NET] การใช้งาน Spreadsheet หรือตาราง Excel ด้วย XlsIO ของฟรีจากค่าย Syncfusion

http://www.g2gnet.com/webboard/images/vbnet/SyncfusionXlsIO.png


ของฟรีที่ทรงประสิทธิภาพจากค่าย Syncfusion ในรุ่น Community นั่นคือ Essential XlsIO เป็นคลาสไลบรารี ในกลุ่มตระกูล .NET ที่สามารถใช้ในการสร้าง และแก้ไขไฟล์ Microsoft Excel โดยการใช้ภาษา C#, VB.NET และโค้ด C ++ ซึ่ง XlsIO มีรูปแบบที่อำนวยความสะดวกในการเข้าถึง และจัดการสเปรดชีตโดยไม่ต้องพึ่งพา Microsoft Office COM Libraries หรือ Microsoft Office ว่าง่ายๆคือ ใช้งาน Excel โดยไม่ต้องติดตั้ง MS Office ... ดาวน์โหลดได้ฟรีที่นี่ ...


การเรียกใช้งาน Spreadsheet จากกลุ่มเครื่องมือ (Toolbox)
http://www.g2gnet.com/webboard/images/vbnet/SyncfusionXlsIOSheet.png


References ที่ต้องนำมาใช้งาน ...
http://www.g2gnet.com/webboard/images/vbnet/SyncfusionXlsIOref.png


มาดูโค้ดฉบับเต็มกันเถอะ ...
#Region "About"
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: Test SpreadSheet of Syncfusion Community.
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
#End Region

Imports Microsoft.VisualBasic
Imports Syncfusion.Windows.Forms.Spreadsheet
Imports Syncfusion.XlsIO

Public Class frmSpreadExcel
    '// Data Path
    Dim strPathData As String = MyPath(Application.StartupPath)
    '// File
    Dim strFileName As String = ""

    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with "\"
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
      '/ MessageBox.Show(AppPath);
      AppPath = AppPath.ToLower()
      '/ Return Value
      MyPath = AppPath.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\")
      '// If not found folder then put the \ (BackSlash) at the end.
      If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
      ' ประกาศใช้งาน Open File Dialog ในแบบ Run Time   
      Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()

      ' / ตั้งค่าการใช้งาน Open File Dialog
      With dlgOpenFile
            .InitialDirectory = MyPath(Application.StartupPath)
            .Title = "เลือกไฟล์ MS Excel"
            .Filter = "MS Excel Files (*.xlsx;*.xls)|*.xlsx;*xls"
            .FilterIndex = 1
            .RestoreDirectory = True
      End With
      ' หากเลือกปุ่ม OK หลังจากการ Browse ...
      If dlgOpenFile.ShowDialog() = DialogResult.OK Then
            '// For save
            strFileName = dlgOpenFile.FileName
            spreadsheet.Open(strFileName)
      End If
    End Sub

    Private Sub frmReadWriteExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      MsgBox(Chr(92))
      '// ComboBox
      With cmbCol
            '// Load ASCII Code from 65 to 9, "A" is 65 (Base 10)
            For i As Byte = 65 To 90
                cmbCol.Items.Add(Chr(i))
            Next
            cmbCol.SelectedIndex = 0
            For i As Byte = 1 To 100
                cmbRow.Items.Add(i)
            Next
            cmbRow.SelectedIndex = 0
      End With
      'spreadsheet.FormulaBarVisibility = False
    End Sub

    Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
      Dim excelEngine As ExcelEngine = New ExcelEngine
      Dim application As IApplication = excelEngine.Excel
      Dim sheet As IWorksheet = spreadsheet.Workbook.Worksheets(0)
      '//
      Dim range As IRange = sheet.Range(Chr(34) + cmbCol.Text + cmbRow.Text + Chr(34))
      range.Text = txtData.Text
      spreadsheet.Focus()
      range.Activate()
      '// Focus to active cell.
      Me.spreadsheet.ActiveGrid.CurrentCell.MoveCurrentCell(CInt(cmbRow.Text), CInt(cmbCol.SelectedIndex) + 1)
      SendKeys.Send("{F2}")
      txtData.Clear()
    End Sub

    Private Sub txtData_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtData.KeyPress
      If Trim(txtData.Text).Length = 0 Or Trim(txtData.Text) = "" Then Return
      '// Press key ENTER
      If e.KeyChar = Chr(13) Then
            '// No Beep
            e.Handled = True
            '//
            Call btnAccept_Click(sender, e)
      End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
      Me.Close()
    End Sub

    Private Sub frmReadWriteExcel_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
      '// Save the workbook to disk in xlsx format.
      If strFileName.Length = 0 Then
            MessageBox.Show("You did not load the Excel file.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
      End If
      spreadsheet.Save()
      MessageBox.Show("Save Excel file successfully.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

anuyut1995 โพสต์ 2019-2-12 16:52:10

ขอบคุณครับ
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การใช้งาน Spreadsheet หรือตาราง Excel ด้วย XlsIO ของฟรีจากค่าย Syncfusion