使用Visual Basic中的LINQ将数据从excel导入Visual Studio [英] Import data from excel into visual studios using LINQ in visual basic

查看:68
本文介绍了使用Visual Basic中的LINQ将数据从excel导入Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用LINQ将数据集从excel导入到可视工作室,以便我可以使用它。有谁知道这个的语法?



谢谢!



我尝试过:



我发现大多数在线教程都给出了C#的语法,但我还没有找到一个适合Visual Basic的教程

解决方案

简而言之:您必须使用 ADO.NET [ ^ ](OleDb)提供者连接到Excel工作表,然后你就可以使用 Linq to DataSet [ ^ ]。

步骤:



1.创建 OleDbConnection [ ^ ]连接到Excel文件( connectionstring [ ^ ])

  Dim  sConnStr  As   String  = 字符串 .Format(  Provider = Microsoft.ACE.OLEDB .12.0;数据源= {0};扩展属性='Excel 12.0; HDR = {1}';  FullFileName.xlsx  YES | NO
Dim oConn As OleDbConnection = 新 OleDbConnection(sConnStr)





2.创建 OleDbCommand [ ^ ]

  Dim  sCommStr  As   String  =   SELECT * FROM [Sheet1 


;
Dim oComm As OleDbCommand = OleDbCommand(sCommStr,oConn)





3.创建OleDbReader [ ^ ]将数据读入DataTable [ ^ ](使用DataTable.Load(IDataReader)方法 [ ^ ])

  Dim  oRdr  As  OleDbReader 
Dim oDt As DataTable = DataTable()
oRdr = oComm.ExecuteReader( )
oDt.Load(oRdr)





4.在数据表对象上使用Linq

< pre lang =vb> Dim myData = oDt.AsEnumerable()_
.Where( Function (x)x.Field ( 字符串)( 名称)= Peter)_
选择功能(x)x)_
.ToList()< span class =code-comment>' 返回一个List(Of DataRow)





更多:

LINQ to DataSet中的查询| Microsoft Docs [ ^ ]



其他资源:

从.NET应用程序访问Microsoft Office数据 [ ^ ]

使用LINQ在Excel 2007中查询表(OpenXML) [ ^ ]

演练:Office编程(C#和Visual Basic)| Microsoft Docs [ ^ ](右侧网站上有一个小型组合框来更改语言)

使用ADO.NET创建一个简单的数据应用程序 [ ^ ]


尝试:Google visual basic linq

前两个答案是:

Visual Basic中的LINQ | Microsoft Docs [ ^ ]

编写你的第一个LINQ查询(Visual Basic)| Microsoft Docs [ ^ ]

您可能需要阅读一些文档。


hello, I am trying to use LINQ to import a dataset from excel to visual studios so that I can work with it. Does anyone know the syntax for this?

Thanks!

What I have tried:

Most of the tutorials online I've found give the syntax for C# but I have yet to find a good one for visual basic

解决方案

In a short: You have to use ADO.NET[^] (OleDb) provider to connect to the Excel sheet, then you'll be able to use Linq to DataSet[^].
Steps to do:

1. Create OleDbConnection[^] to connect to the Excel file (connectionstring[^])

Dim sConnStr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR={1}';", "FullFileName.xlsx", "YES|NO")
Dim oConn As OleDbConnection = New OleDbConnection(sConnStr)



2. Create OleDbCommand[^]

Dim sCommStr As String = "SELECT * FROM [Sheet1


;" Dim oComm As OleDbCommand = New OleDbCommand(sCommStr, oConn)



3. Create OleDbReader[^] to read data into DataTable[^] (use DataTable.Load(IDataReader) method[^])

Dim oRdr As OleDbReader
Dim oDt As DataTable = New DataTable()
oRdr = oComm.ExecuteReader()
oDt.Load(oRdr)



4. Use Linq on datatable object

Dim myData = oDt.AsEnumerable() _
    .Where(Function(x) x.Field(Of String)("Name") = "Peter") _
    .Select(Function(x) x) _
    .ToList() 'returns a List(Of DataRow)



More:
Queries in LINQ to DataSet | Microsoft Docs[^]

Other resources:
Accessing Microsoft Office Data from .NET Applications[^]
Using LINQ to Query Tables in Excel 2007 (OpenXML)[^]
Walkthrough: Office Programming (C# and Visual Basic) | Microsoft Docs[^] (there's small combobox on the right site to change language)
Create a simple data application by using ADO.NET[^]


Try: Google visual basic linq
The first 2 answers are:
LINQ in Visual Basic | Microsoft Docs[^]
Writing Your First LINQ Query (Visual Basic) | Microsoft Docs[^]
You may have some documentation to read.


这篇关于使用Visual Basic中的LINQ将数据从excel导入Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆