weixin_40187983 2019-05-04 10:15 采纳率: 100%
浏览 476
已采纳

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条回答 默认 最新

  • threenewbee 2019-05-04 11:26
    关注

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

    比如
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥15 在fragment使用okhttp同步上传文件,能不能在fragment销毁后还可以继续上传文件?
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
  • ¥15 关于地板的木纹和图库中的匹配的
  • ¥30 机器学习预测疾病模型流程疑问
  • ¥50 2048Python实现
  • ¥15 使用ads进行低噪放仿真没有结果且不报错
  • ¥15 关于#python#的问题:有偿求一个千寻框架找书机器人插件
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部