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

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

[VB.NET] โค้ดการใช้งาน XlsIO ของ Syncfusion เพื่อทำการแปลงไฟล์ Excel ให้เป็น PDF และ PNG

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

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5971




Syncfusion Excel library (Essential XlsIO) เป็นไลบรารี .NET Excel ที่มีประสิทธิภาพสูง ที่ช่วยให้ผู้ใช้สามารถสร้าง, อ่าน หรือแก้ไขเอกสาร Excel ในแอพพลิเคชั่น .NET โดยที่ไม่ต้องใช้ Microsoft Office ว่าง่ายๆก็คือเป็นการใช้ Excel Engine ของตัว Syncfusion เองครับ สำหรับโค้ดชุดนี้จะเป็นการโหลดไฟล์ Excel เข้าสู่ Spreadsheet ของตัว Syncfusion จากนั้นทำการแปลงให้เป็นเอกสาร PDF ซึ่งก็เป็นของ Syncfusion เหมือนกัน และจากไฟล์ PDF ก็สามารถแปลงไฟล์ต่อไปเป็นกราฟิค โดยตัวอย่างนี้แอดมินเลือกใช้ไฟล์ PNG และสำหรับการโหลดไฟล์กราฟิคเข้าสู่ PictureBox Control แอดมินเลือกใช้ Stream เพื่อป้องกัน Process ที่ค้างอยู่ครับผม ...


ดาวน์โหลดของฟรี Syncfusion Essemtial รุ่น Community ได้ที่นี่ (Version 17.1.0.47)... ย้ำอีกทีว่าฟรี 100% แน่นอนครับ


เริ่มต้นด้วยการ Add References



มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Syncfusion.XlsIO
  2. Imports Syncfusion.Pdf
  3. Imports Syncfusion.ExcelToPdfConverter
  4. Imports Syncfusion.Pdf.Parsing
  5. Imports System.Drawing.Imaging
  6. Imports System.IO

  7. Public Class frmSyncfusionMain
  8.     '// File
  9.     Dim strFileName As String = String.Empty
  10.     Dim XlsFileName As String()

  11.     ' / ---------------------------------------------------------------
  12.     ' / Load Excel.
  13.     ' / ---------------------------------------------------------------
  14.     Private Sub btnLoadXLS_Click(sender As System.Object, e As System.EventArgs) Handles btnLoadXLS.Click
  15.         Me.TabControlAdv1.SelectedIndex = 0
  16.         '/ Open File Dialog in Run Time.
  17.         Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
  18.         Try
  19.             ' / Initialized Open File Dialog.
  20.             With dlgOpenFile
  21.                 .InitialDirectory = MyPath(Application.StartupPath)
  22.                 .Title = "Select MS Excel File"
  23.                 .Filter = "MS Excel Files (*.xlsx;*.xls)|*.xlsx;*xls"
  24.                 .FilterIndex = 1
  25.                 .RestoreDirectory = True
  26.             End With
  27.             '/ If the OK button is selected.
  28.             If dlgOpenFile.ShowDialog() = DialogResult.OK Then
  29.                 '// For save
  30.                 strFileName = dlgOpenFile.FileName
  31.                 Spreadsheet1.Open(strFileName)
  32.                 '// Xlsfilename(0) = "D:\SyncfusionFile\SyncfusionFile\GettingStarted"
  33.                 '// Xlsfilename(1) = "xlsx"
  34.                 XlsFileName = Split(strFileName, ".")
  35.             End If
  36.         Catch ex As Exception
  37.             MessageBox.Show(ex.Message)
  38.         End Try

  39.     End Sub

  40.     ' / ---------------------------------------------------------------
  41.     ' / Convert Excel To PDF.
  42.     ' / ---------------------------------------------------------------
  43.     Private Sub btnXlsToPdf_Click(sender As System.Object, e As System.EventArgs) Handles btnXlsToPdf.Click
  44.         If strFileName.Length = 0 Then
  45.             MessageBox.Show("You did not load the Excel file.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  46.             Return
  47.         End If
  48.         Spreadsheet1.Save()
  49.         '//
  50.         Me.TabControlAdv1.SelectedIndex = 1
  51.         '//
  52.         Try
  53.             Using excelEngine As ExcelEngine = New ExcelEngine()
  54.                 Dim application As IApplication = excelEngine.Excel
  55.                 application.DefaultVersion = ExcelVersion.Excel2013
  56.                 '// Load Excel to Workbook.
  57.                 Dim workbook As IWorkbook = application.Workbooks.Open(strFileName, ExcelOpenType.Automatic)

  58.                 '/ Open the Excel document to convert
  59.                 Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
  60.                 '/ Initialize the PDF document
  61.                 Dim pdfDocument As PdfDocument = New PdfDocument()
  62.                 '/ Convert Excel document into PDF document
  63.                 pdfDocument = converter.Convert()
  64.                 '/ Save the PDF file
  65.                 pdfDocument.Save(XlsFileName(0) & ".pdf")

  66.             End Using
  67.             Me.PdfViewerControl1.Load(XlsFileName(0) + ".pdf", "")

  68.         Catch ex As Exception
  69.             MessageBox.Show(ex.Message)
  70.         End Try
  71.     End Sub

  72.     ' / ---------------------------------------------------------------
  73.     ' / Convert PDF To Image File.
  74.     ' / ---------------------------------------------------------------
  75.     Private Sub btnPdfToImage_Click(sender As System.Object, e As System.EventArgs) Handles btnPdfToImage.Click
  76.         '// Trap Error if Nothing on PdfViewerControl1.
  77.         If IsNothing(PdfViewerControl1.LoadedDocument) Then Exit Sub
  78.         Me.TabControlAdv1.SelectedIndex = 2
  79.         Call ClearPictureBox(PictureBox1)
  80.         Try
  81.             '/ Loaded input PDF file
  82.             Dim loadedDocument As New PdfLoadedDocument(XlsFileName(0) & ".pdf")
  83.             '/ Exporting specify page index as image
  84.             Dim image As Bitmap = loadedDocument.ExportAsImage(0)
  85.             '/ Save the image as PNG format
  86.             image.Save(XlsFileName(0) & ".png", ImageFormat.Png)
  87.             '/ Close the document
  88.             loadedDocument.Close(True)
  89.             '// Use Stream to show picture.
  90.             Call ShowPicture(XlsFileName(0) & ".png")
  91.         Catch ex As Exception
  92.             MessageBox.Show(ex.Message)
  93.         End Try
  94.     End Sub

  95.     ' / -----------------------------------------------------------------------------
  96.     ' / Use Steam instead IO.
  97.     ' / -----------------------------------------------------------------------------
  98.     Sub ShowPicture(PicName As String)
  99.         Dim imgDB As Image
  100.         Dim streamPic As Stream
  101.         '// Get the name of the image file from the database.
  102.         '// Verify that the image file meets the specified location.
  103.         If System.IO.File.Exists(PicName.ToString) Then
  104.             ' Because when deleting the image file is locked, it can not be removed.
  105.             ' The file is closed after the image is loaded, so you can delete the file if you need to
  106.             streamPic = File.OpenRead(PicName.ToString)
  107.             imgDB = Image.FromStream(streamPic)
  108.             PictureBox1.Image = imgDB
  109.             streamPic.Dispose()
  110.         End If
  111.     End Sub

  112.     Private Sub ClearPictureBox(ByRef pb As PictureBox)
  113.         pb.Image = Nothing
  114.         pb.BackColor = Color.Empty
  115.         pb.Invalidate()
  116.     End Sub

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

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

  133.     Private Sub frmSyncfusionMain_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  134.         Me.Dispose()
  135.         Application.Exit()
  136.     End Sub

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



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

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

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

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

4

กระทู้

13

โพสต์

102

เครดิต

Member

Rank: 2

เครดิต
102
โพสต์ 2019-11-15 12:17:06 | ดูโพสต์ทั้งหมด

file:///C:/Users/komen/Pictures/Clip.jpgผมคลิ๊กลิ้งค์ download แล้ว มัน ขึ้น แบบนี้ครับ
สมัคร สมาชิกแล้วครัรบ

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

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

x

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5971
 เจ้าของ| โพสต์ 2019-11-15 14:01:08 | ดูโพสต์ทั้งหมด

ลิ้งค์สำหรับสมัครสมาชิกใหม่ครับ Syncfusion Community license


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

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

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

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

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

GMT+7, 2024-3-29 00:34 , Processed in 0.328763 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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