【发布时间】:2020-12-08 16:44:07
【问题描述】:
我有一个包含 120 列和大约 800 行数据的电子表格。我在网上找到了下面的代码,使用 ClosedXml 效果很好。
而不是使用导出所有行,我只想抓取 cols 1 和 2(及其标题)以及 col 的 15 到 46 及其相应的标题。
有谁知道 ClosedXml 是否作为调用/程序来提取特定列,还是我需要将所需的列放在一个数组中,然后从那里提取所需的内容?我已经把谷歌穿在外面看:)。
谢谢
Protected Sub ImportExcel(sender As Object, e As EventArgs)
'Open the Excel file using ClosedXML.
Using workBook As New XLWorkbook(FileUpload1.PostedFile.InputStream)
'Read the first Sheet from Excel file.
Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
'Create a new DataTable.
Dim dt As New DataTable()
'Loop through the Worksheet rows.
Dim firstRow As Boolean = True
For Each row As IXLRow In workSheet.Rows()
'Use the first row to add columns to DataTable.
If firstRow Then
For Each cell As IXLCell In row.Cells()
dt.Columns.Add(cell.Value.ToString())
Next
firstRow = False
Else
'Add rows to DataTable.
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells()
dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
End If
GridView1.DataSource = dt
GridView1.DataBind()
Next
End Using
End Sub
【问题讨论】:
标签: vb.net datagridview closedxml