您能帮我解决如何将vba中的特定工作表与GemBox链接的问题吗?
我已经安装了gembox电子表格和gembox winform实用程序。
有没有其他方法可以在不使用gembox的情况下链接excel和vba? 我尝试了几个例子,但它有太多错误,我甚至不知道如何解决这个问题。
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.WinFormsUtilities
Imports System.Data.OleDb
Public Class Form1
Public Sub New()
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
InitializeComponent()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openFileDialog = New OpenFileDialog()
openFileDialog.Filter = "XLS files (*.xls, *.xlt)|*.xls;*.xlt|XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm"
openFileDialog.FilterIndex = 2
If (openFileDialog.ShowDialog() = DialogResult.OK) Then
Dim ef = ExcelFile.Load(openFileDialog.FileName)
' Export Excel worksheet to DataGridView control.
DataGridViewConverter.ExportToDataGridView(ef.Worksheets.ActiveWorksheet, Me.DataGridView1, New ExportToDataGridViewOptions() With {.ColumnHeaders = True})
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim saveFileDialog = New SaveFileDialog()
saveFileDialog.Filter = "XLS files (*.xls)|*.xls|XLT files (*.xlt)|*.xlt|XLSX files (*.xlsx)|*.xlsx|XLSM files (*.xlsm)|*.xlsm|XLTX (*.xltx)|*.xltx|XLTM (*.xltm)|*.xltm|ODS (*.ods)|*.ods|OTS (*.ots)|*.ots|CSV (*.csv)|*.csv|TSV (*.tsv)|*.tsv|HTML (*.html)|*.html|MHTML (.mhtml)|*.mhtml|PDF (*.pdf)|*.pdf|XPS (*.xps)|*.xps|BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|WMP (*.wdp)|*.wdp"
saveFileDialog.FilterIndex = 3
If (saveFileDialog.ShowDialog() = DialogResult.OK) Then
Dim ef = New ExcelFile()
Dim ws = ef.Worksheets.Add("Sheet1")
' Import DataGridView control to Excel worksheet.
DataGridViewConverter.ImportFromDataGridView(ws, Me.DataGridView1, New ImportFromDataGridViewOptions() With {.ColumnHeaders = True})
ef.Save(saveFileDialog.FileName)
End If
End Sub