vb.net 讀取Excel
1、定義excel操作變數
|
dim objexcelfile as excel.application
dim objworkbook as excel.workbook
dim objimportsheet as excel.worksheet
|
2、開啟excel程式,並開啟目標excel檔案
|
set objexcelfile = new excel.application
objexcelfile.displayalerts = false
set objworkbook = objexcelfile.workbooks.open(strfilename)
set objimportsheet = objworkbook.sheets(1)
|
3、獲取excel有效區域的行和列數
|
intlastcolnum = objimportsheet.usedrange.columns.count
intlastrownum = objimportsheet.usedrange.rows.count
|
4、逐行讀取excel中資料
|
由於前兩行為header部分,所以需要從第三行讀取
如果第1到第10個單元格的值均為空或空格,則視為空行
for intcounti = 3 to intlastrownum
check if empty data row
blnnullrow = true
for inti = 1 to 10
if trim$(objimportsheet.cells(intcounti, inti).value) <> "" then
blnnullrow = false
end if
next inti
若不是空行,則進行讀取動作,否則繼續向後遍歷excel中的行
if blnnullrow = false then
獲取單元格中的資料,做有效性check,並將合法資料建立為實體存入物件陣列中
objimportsheet.cells(intcounti, 1).value
……
end if next intcounti
|
5、退出excel程式,並關閉excel相關操作物件
|
objexcelfile.quit
set objworkbook = nothing
set objimportsheet = nothing
set objexcelfile = nothing |
2
Public Shared Function PFn_ExcelToDataTable(FileName As String) As DataTable
Dim dtRetrunTable As New DataTable
Dim strConn As String = ("Provider=Microsoft.Ace.OleDb.12.0;" & "data source=") + FileName & ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'"
Dim conn As New OleDb.OleDbConnection(strConn)
conn.Open()
Dim myDataSet As DataSet = New DataSet
Dim strExcel = "SELECT * FROM [sheet1$]"
Using da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strExcel, conn)
Try
da.Fill(myDataSet)
dtRetrunTable = myDataSet.Tables(0)
Catch ex As Exception
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
conn = Nothing
End If
da.Dispose()
End Try
End Using
Return dtRetrunTable
End Function
相關文章
- Laravel讀取ExcelLaravelExcel
- excel 資料讀取Excel
- Python讀取Excel表格PythonExcel
- PHPExcel讀取excel資料PHPExcel
- JavaPoi建立與讀取ExcelJavaExcel
- NPOI讀取Excel官方demoExcel
- C#讀取Excel文件C#Excel
- WinForm讀取Excel檔案ORMExcel
- 前端讀取excel檔案前端Excel
- 讀取和儲存Excel表Excel
- python對Excel的讀取PythonExcel
- POI 分批讀取Excel資料Excel
- 前端讀取Excel表中資料前端Excel
- PHPExcel讀取excel檔案示例PHPExcel
- 用 (Excel) VBA 讀取 OneNote!Excel
- PHP讀取Excel之Spreadsheet_Excel_ReaderPHPExcel
- easypoi 讀取 Excel 簡單應用Excel
- C#讀取Excel方法總結C#Excel
- H5以及Node讀取excelH5Excel
- Excel上傳並讀取資料Excel
- 讀取本地Excel檔案生成echartsExcelEcharts
- csv和excel讀取和下載Excel
- PIO讀取excel文件,java實現ExcelJava
- QTP讀取Excel資料的方法QTExcel
- hutool分批次讀取excel資料Excel
- pandas讀取excel亂碼報錯Excel
- Maatwebsite\Excel 讀取帶公式的excel檔案得到值呢?WebExcel公式
- Excel 讀取圖片並獲取儲存路徑Excel
- java讀取Excel匯入匯出工具JavaExcel
- POI 使用SAX讀取大資料Excel大資料Excel
- 【Python】讀取excel並輸出到txtPythonExcel
- phpexcel讀取excel的xls xlsx csv格式PHPExcel
- python讀取和生成excel檔案PythonExcel
- java讀取excel為物件並進行讀寫操作JavaExcel物件
- C#/VB.NET 獲取Excel中圖片所在的行、列座標位置C#Excel
- C#/VB.NET 將Html轉為ExcelC#HTMLExcel
- python讀取excel所有資料(cmd介面)PythonExcel
- EasyExcel 輕鬆靈活讀取Excel內容Excel