博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VB.net 操作 execl
阅读量:5914 次
发布时间:2019-06-19

本文共 3667 字,大约阅读时间需要 12 分钟。

  '加载Excel数据

    Private Function LoadExcelData(ByVal Path As String)
        txtMsg.Text = "Excel数据导入中..."
        Dim msgError As String = ""
        Dim miss As Object = System.Reflection.Missing.Value
        Dim wss As Excel.Sheets
        Dim ws As Excel.Worksheet = Nothing
        Dim excelApp As Excel.Application = New Excel.Application
        Dim wbs As Excel.Workbooks = excelApp.Workbooks
        Dim wb As Excel.Workbook = wbs.Open(Path, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss)
        Dim sheetName As String
        Dim dr As DataRow '创建行
        dt.Columns.Add("SheetName")
        dt.Columns.Add("P/ONO")
        dt.Columns.Add("Size1")
        dt.Columns.Add("Size2")
        dt.Columns.Add("Size3")
        dt.Columns.Add("Quantity")
        Try
            excelApp.Visible = False
            wss = wbs(1).Worksheets
            For k As Integer = 1 To wss.Count '得到不同的表
                ws = wss.Item(k) '得到不同表
                Dim PONO As String
                Dim rowNum As Integer = ws.UsedRange.Cells.Rows.Count '行数
                Dim colNum As Integer = ws.UsedRange.Cells.Columns.Count '列数
                sheetName = ws.Name '得到表名
                For i As Integer = 1 To rowNum  '循环行
                    Dim Flag As String = ws.Range(ws.Cells(i, 1), ws.Cells(i, 1)).Text.ToString().Trim() 'i=28
                    If Flag = "Vendor P/O No. :" Then
                        PONO = ws.Range(ws.Cells(i, 3), ws.Cells(i, 3)).Text.ToString().Trim() '得到PONO
                    End If
                    '-------------------    ------------------------------------------------
                    If Flag = "Size" Then  '表示找到Size的时候
                        Dim Size1RowValue As String  'Size1列值
                        Dim Size2RowValue As String  'Size2列值
                        Dim Size3RowValue As String  'Size3列值
                        Dim QuantityValue As String 'Quanty数量
                        For j As Integer = 1 To colNum '列数
                            If ws.Range(ws.Cells(i, j + 1), ws.Cells(i, j + 1)).Text.ToString().Trim() <> "" Then
                                Size1RowValue = ws.Range(ws.Cells(i, j + 1), ws.Cells(i, j + 1)).Text.ToString().Trim() 'XS S M L XL EL
                            End If
                            '---------Size2Value----------------------
                            If ws.Range(ws.Cells(i + 1, 1), ws.Cells(i + 1, 1)).Text.ToString().Trim() <> "Quantity" And ws.Range(ws.Cells(i + 1, j + 1), ws.Cells(i + 1, j + 1)).Text.ToString().Trim() <> "" Then
                                Size2RowValue = ws.Range(ws.Cells(i + 1, j + 1), ws.Cells(i + 1, j + 1)).Text.ToString().Trim() '表示Size2的值
                            End If
                            '--------Size3Value-----------------------
                            If ws.Range(ws.Cells(i + 2, 1), ws.Cells(i + 2, 1)).Text.ToString().Trim() <> "Quantity" And ws.Range(ws.Cells(i + 2, j + 1), ws.Cells(i + 2, j + 1)).Text.ToString().Trim() <> "" Then
                                Size3RowValue = ws.Range(ws.Cells(i + 2, j + 1), ws.Cells(i + 2, j + 1)).Text.ToString().Trim() '表示Size3的值
                            End If
                            '-------------------QuantityValue------------
                            For h As Integer = i To rowNum '要找到Quantity
                                If ws.Range(ws.Cells(h, 1), ws.Cells(h, 1)).Text.ToString().Trim() = "Quantity" Then
                                    QuantityValue = ws.Range(ws.Cells(h, j + 1), ws.Cells(h, j + 1)).Text.ToString().Trim() '得到相应的数量
                                    Exit For '找到之后退出
                                End If
                            Next
                            dr = dt.NewRow '创建行
                            If QuantityValue = "" Then
                                QuantityValue = Nothing
                            End If
                            dr("P/ONO") = PONO
                            dr("SheetName") = sheetName 'execl表名
                            dr("Size1") = Size1RowValue '即一行的值
                            dr("Size2") = Size2RowValue '即二行的值
                            dr("Size3") = Size3RowValue '即三行的值
                            dr("Quantity") = QuantityValue '即数量值
                            If Size1RowValue <> Nothing And Size2RowValue <> " " And Size3RowValue <> " " Then
                                dt.Rows.Add(dr)
                            End If
                            Size1RowValue = Nothing
                            Size2RowValue = Nothing
                            Size3RowValue = Nothing
                            QuantityValue = Nothing
                        Next
                    End If
                Next
            Next
            dgvSizeLabel.DataSource = dt
            dgvSizeLabel.DataBind()
            If dt.Rows.Count > 0 Then
                txtMsg.Text = "Excel数据导入成功"
            End If
        Catch ex As Exception
            'Throw ex
            txtMsg.Text = "读取Excel发生错误"
            'dgvSizeLabel.DataSource = ds.Tables("temp")
        End Try
        ''wb.Close(True)
        ''wbs.Close()
        ''excelApp.Workbooks.Close()
        'excelApp.Workbooks.Close()
        'excelApp.Quit()
        'System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
        'GC.Collect()
        NAR(sheetName)
        wb.Close(False)
        NAR(wbs)
        NAR(wb)
        excelApp.Quit()
        NAR(excelApp)
    End Function

转载地址:http://nzwvx.baihongyu.com/

你可能感兴趣的文章
class类名的管理
查看>>
LeetCode:Rectangle Area
查看>>
文本查询
查看>>
查看帐号授权信息
查看>>
小程序(四):模板
查看>>
【转】Java - printf
查看>>
jquery获取元素到屏幕底的可视距离
查看>>
ENDNOTE使用方法(转发)
查看>>
计算机数制和运算的一点总结.
查看>>
UML系列 (五) 为什么要用UML建模之建模的重要性
查看>>
框架是什么,框架有什么用(转)
查看>>
集成测试
查看>>
对于I/O流中解压中遇到的问题
查看>>
问答项目---用户注册的那些事儿(JS验证)
查看>>
Android进阶篇-百度地图获取地理信息
查看>>
返回前一页并刷新页面方法
查看>>
2.3 InnoDB 体系架构
查看>>
不定宽高垂直居中分析
查看>>
项目管理学习笔记之二.工作分解
查看>>
C# PPT 为形状设置三维效果
查看>>