vb.net DataTable内有空栏,如何去除-线上等

如题,
我要如何把空栏去除呢?

图片说明

图片说明

图片说明

感谢

Public Function ExcelToDataTable_Copy(ByVal FilaName As String, ByVal SheetName As String) As DataTable '数据查询
Try
'将数值字串统一的
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlBook = xlApp.Workbooks.Open(FilaName)
xlSheet = xlBook.Worksheets(1)
xlApp.Visible = False
xlApp.Application.Visible = False
Dim data_count As Integer
data_count = xlSheet.UsedRange.Columns.Count
For i = 1 To data_count
xlSheet.Cells(2, i).value = "'" & xlSheet.Cells(2, i).value
xlSheet.Cells(3, i).value = "'" & xlSheet.Cells(3, i).value
xlSheet.Cells(4, i).value = "'" & xlSheet.Cells(4, i).value
xlSheet.Cells(5, i).value = "'" & xlSheet.Cells(5, i).value
xlSheet.Cells(6, i).value = "'" & xlSheet.Cells(6, i).value
xlSheet.Cells(7, i).value = "'" & xlSheet.Cells(7, i).value
xlSheet.Cells(8, i).value = "'" & xlSheet.Cells(8, i).value
xlSheet.Cells(9, i).value = "'" & xlSheet.Cells(9, i).value
Next
xlBook.Save()
xlBook.Close()
xlApp.Quit()
xlApp = Nothing
xlBook = Nothing
xlSheet = Nothing
Dim dt As DataTable
dt = New DataTable
Dim sExcelFile As String
sExcelFile = FilaName
Dim sConnectionString As String = ""
If FilaName.IndexOf(".xlsx") <> -1 Then
Select Case Get_excel_VN()
Case "16.0", "14.0", "12.0"
sConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & sExcelFile & "; Extended Properties='Excel 12.0;HDR=true;IMEX=1;'"
End Select
ElseIf FilaName.IndexOf(".xls") <> -1 Then
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sExcelFile & "; Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"
Else
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sExcelFile & "; Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"
End If
Dim connection As OleDb.OleDbConnection
connection = New OleDb.OleDbConnection(sConnectionString)
connection.Open()
dt = connection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
Dim sql_select_commands As String
sql_select_commands = "Select * from [" & SheetName & "$]"
Dim adp As OleDb.OleDbDataAdapter
adp = New OleDb.OleDbDataAdapter(sql_select_commands, connection)
Dim ds As Data.DataSet
ds = New Data.DataSet()
adp.Fill(ds, SheetName)
dt = ds.Tables(0)
If (connection.State = ConnectionState.Open) Then
connection.Close()
End If

        '除去空行

        'https://ask.csdn.net/questions/759795

        Return dt
    Catch ex As Exception
        MsgBox(ex.Message.ToString())
    End Try
    Return Nothing
End Function

1个回答

你说的是空行?你的数据源怎么读取的,加上判断

比如
select * from table where 公示与授权 <> ""

dt = ds.Tables(0)
前面加上
for each dr as DataRow in ds.Tables(0).Rows.OfType(Of DataRow)()
if dr[0].ToString() = "" then
ds.Tables(0).Rows.Remove(dr)
end if
next

weixin_40187983
weixin_40187983 回复操作员马善福(贵阳专业挖机): 谢谢!
11 个月之前 回复
caozhy
每个人都有一个梦才不会孤单的说话就有天堂 for each dr as DataRow in ds.Tables(0).Rows.OfType(Of DataRow)().ToList()
11 个月之前 回复
weixin_40187983
weixin_40187983 还是有问题:集合已修改:枚举操作可能无法执行
11 个月之前 回复
caozhy
每个人都有一个梦才不会孤单的说话就有天堂 回复weixin_40187983: 看我更新的回答
11 个月之前 回复
weixin_40187983
weixin_40187983 有没有除了公示与授权 <> ""更好的方式,因为我只要资料源一换,栏位名称就会不一样了
11 个月之前 回复
weixin_40187983
weixin_40187983 回复操作员马善福(贵阳专业挖机): 早上好,我贴上了
11 个月之前 回复
caozhy
每个人都有一个梦才不会孤单的说话就有天堂 回复weixin_40187983: 一样的,加上判断,或者你把你的代码贴出来,我帮你修改
11 个月之前 回复
weixin_40187983
weixin_40187983 有可能是<>"" 或是null 这个数据不来自sql,是来自excel的
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问