ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4091|ตอบกลับ: 0

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

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6072




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


การเรียกใช้งาน Spreadsheet จากกลุ่มเครื่องมือ (Toolbox)



References ที่ต้องนำมาใช้งาน ...



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

  15. Imports Microsoft.VisualBasic
  16. Imports Syncfusion.Windows.Forms.Spreadsheet
  17. Imports Syncfusion.XlsIO

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

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

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

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

  54.     Private Sub frmReadWriteExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  55.         MsgBox(Chr(92))
  56.         '// ComboBox
  57.         With cmbCol
  58.             '// Load ASCII Code from 65 to 9, "A" is 65 (Base 10)
  59.             For i As Byte = 65 To 90
  60.                 cmbCol.Items.Add(Chr(i))
  61.             Next
  62.             cmbCol.SelectedIndex = 0
  63.             For i As Byte = 1 To 100
  64.                 cmbRow.Items.Add(i)
  65.             Next
  66.             cmbRow.SelectedIndex = 0
  67.         End With
  68.         'spreadsheet.FormulaBarVisibility = False
  69.     End Sub

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

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

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

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

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

  110. End Class
คัดลอกไปที่คลิปบอร์ด

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

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

3

กระทู้

17

โพสต์

265

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
265
โพสต์ 2019-2-12 16:52:10 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-5-5 22:49 , Processed in 0.204383 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้