Vba импорт из excel в excel

Vba импорт из excel в excel

textual

Imports System.Data.OleDb Imports Microsoft.Office.Interop.Excel   Public Class Form1       Public Function GetDataFromExcel(ByVal a_sFilepath As String) As DataSet         Dim ds As New DataSet()         Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & a_sFilepath & ";Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:Engine Type=37")         Dim bspr As New BindingSource         Dim sheetname As String         Try             cn.Open()         Catch ex As OleDbException             Console.WriteLine(ex.Message)         Catch ex As Exception             Console.WriteLine(ex.Message)         End Try           Dim dt As New System.Data.DataTable()         dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)         If dt IsNot Nothing OrElse dt.Rows.Count > 0 Then             For sheet_count As Integer = 0 To dt.Rows.Count - 1                 Try                     sheetname = dt.Rows(sheet_count)("table_name").ToString()                     Dim da As New OleDbDataAdapter("SELECT * FROM [" & sheetname & "]", cn)                     da.Fill(ds, sheetname)                 Catch ex As DataException                     Console.WriteLine(ex.Message)                 Catch ex As Exception                     Console.WriteLine(ex.Message)                 End Try             Next         End If         DataGridView1.DataSource = bspr         bspr.DataSource = ds         bspr.DataMember = "" & sheetname & ""         cn.Close()         Return ds     End Function       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         GetDataFromExcel(System.IO.Directory.GetCurrentDirectory + "\n.xlsx")     End Sub End Class



Источник: studassistent.ru


Добавить комментарий