从 excel 读取数据并将它们存储在列表中。 (Vb.net)
Read data from excel and store them in the list. (Vb.net)
我有一个 excel sheet 有 2 列。现在我希望第 1 列的所有值都应该存储在一个列表中,比如 ListTime,第 2 列的所有值应该存储在另一个列表中,比如 ListAcceleration。我不知道该怎么做。
提前致谢。
这是个好方法:
Imports Microsoft.Office.Interop
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class FormMain
Private xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
Private xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Private ListTime As New List(Of Double)
Private ListAcceleration As New List(Of Double)
Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
Dim Path As String
Using OFD1 As New CommonOpenFileDialog
OFD1.Title = "Exceldatei auswählen"
OFD1.Filters.Add(New CommonFileDialogFilter("Excel", ".xlsx"))
OFD1.IsFolderPicker = False
OFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD1.ShowDialog = CommonFileDialogResult.Ok Then
Path = OFD1.FileName
Else
Return
End If
End Using
xlWorkBook = xlApp.Workbooks.Open(Path)
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWorkBook.Worksheets("Tabelle1"), Microsoft.Office.Interop.Excel.Worksheet)
Dim xlRange As Microsoft.Office.Interop.Excel.Range = xlWorkSheet.UsedRange
Dim ER As Microsoft.Office.Interop.Excel.Range
For rCnt As Integer = 1 To xlRange.Rows.Count Step 1
ER = CType(xlRange.Cells(rCnt, 1), Microsoft.Office.Interop.Excel.Range)
ListTime.Add(CDbl(ER.Value))
ER = CType(xlRange.Cells(rCnt, 2), Microsoft.Office.Interop.Excel.Range)
ListAcceleration.Add(CDbl(ER.Value))
Next
xlWorkBook.Save()
xlWorkBook.Close()
xlApp.Quit()
If xlWorkSheet IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
If xlWorkBook IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
If xlApp IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If xlWorkBook IsNot Nothing Then xlWorkBook.Close()
If xlApp IsNot Nothing Then xlApp.Quit()
End Sub
End Class
您需要考虑几件事:首先,您必须从 Visual Studio 自己的 NuGet 包管理器下载“Microsoft.Office.Interop.Excel”包。还有包“Microsoft.WindowsAPICodePack.Dialogs”有一个合理的 OpenFileDialog。
如果是 MS Office 文件,使用 System.Runtime.InteropServices.Marshal.ReleaseComObject(..)
释放文件很重要,否则您稍后在桌面上单击它时将无法编辑它。在这种情况下,您将不得不重新启动 PC。所以不要忘记。
哦,顺便说一句:这个词——在我的例子中是“Tabelle1”——在你的语言中会有不同的命名。你必须改变这个。
我有一个 excel sheet 有 2 列。现在我希望第 1 列的所有值都应该存储在一个列表中,比如 ListTime,第 2 列的所有值应该存储在另一个列表中,比如 ListAcceleration。我不知道该怎么做。 提前致谢。
这是个好方法:
Imports Microsoft.Office.Interop
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class FormMain
Private xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
Private xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Private ListTime As New List(Of Double)
Private ListAcceleration As New List(Of Double)
Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
Dim Path As String
Using OFD1 As New CommonOpenFileDialog
OFD1.Title = "Exceldatei auswählen"
OFD1.Filters.Add(New CommonFileDialogFilter("Excel", ".xlsx"))
OFD1.IsFolderPicker = False
OFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD1.ShowDialog = CommonFileDialogResult.Ok Then
Path = OFD1.FileName
Else
Return
End If
End Using
xlWorkBook = xlApp.Workbooks.Open(Path)
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWorkBook.Worksheets("Tabelle1"), Microsoft.Office.Interop.Excel.Worksheet)
Dim xlRange As Microsoft.Office.Interop.Excel.Range = xlWorkSheet.UsedRange
Dim ER As Microsoft.Office.Interop.Excel.Range
For rCnt As Integer = 1 To xlRange.Rows.Count Step 1
ER = CType(xlRange.Cells(rCnt, 1), Microsoft.Office.Interop.Excel.Range)
ListTime.Add(CDbl(ER.Value))
ER = CType(xlRange.Cells(rCnt, 2), Microsoft.Office.Interop.Excel.Range)
ListAcceleration.Add(CDbl(ER.Value))
Next
xlWorkBook.Save()
xlWorkBook.Close()
xlApp.Quit()
If xlWorkSheet IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
If xlWorkBook IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
If xlApp IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If xlWorkBook IsNot Nothing Then xlWorkBook.Close()
If xlApp IsNot Nothing Then xlApp.Quit()
End Sub
End Class
您需要考虑几件事:首先,您必须从 Visual Studio 自己的 NuGet 包管理器下载“Microsoft.Office.Interop.Excel”包。还有包“Microsoft.WindowsAPICodePack.Dialogs”有一个合理的 OpenFileDialog。
如果是 MS Office 文件,使用 System.Runtime.InteropServices.Marshal.ReleaseComObject(..)
释放文件很重要,否则您稍后在桌面上单击它时将无法编辑它。在这种情况下,您将不得不重新启动 PC。所以不要忘记。
哦,顺便说一句:这个词——在我的例子中是“Tabelle1”——在你的语言中会有不同的命名。你必须改变这个。