在将DataGridView数据导出为ex​​cel时遇到一些问题

时间:2015-11-30 16:31:08

标签: vb.net excel datagridview export-to-excel

我有DataGridView1以下标题。

VENCDITEMUPCRTLNUMRTLPRICEPROMO1FROM1TO1PROMO2FROM2TO2PROMO3FROM3TO3

使用DatePicker1,我正在过滤DataGridView1以仅显示DatePicker1.Value.Date = FROM1 OR FROM2 OR FROM3

的行
DirectCast(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = String.Format("[FROM1] = '{0:M/d/yyyy}' OR [FROM2] = '{0:M/d/yyyy}' OR [FROM3] = '{0:M/d/yyyy}'", DatePicker.Value.Date)

这是我被卡住的部分。 我需要使用这个伪代码将数据导出到excel(xlsx或csv)。

if DatePicker1.Value.Date = FROM1 then 
    If PROMO1.Contains("/") Then
        Dim words As String() = PROMO1.Split(New Char() {"/"c})
        'If promo1 contains / (ex: 4/5.00) then
        RTLNUM = word(0) 'RTLNUM = 4
        RTLPRICE = word(1) 'RTLPRICE = 5.00
        export = VENCD, ITEMUPC, RTLNUM, RTLPRICE, PROMO1, FROM1, TO1

elseif DatePicker1.Value.Date = FROM2 then 
    If PROMO2.Contains("/") Then
        Dim words As String() = PROMO2.Split(New Char() {"/"c})
        RTLNUM = word(0)
        RTLPRICE = word(1)
        export = VENCD, ITEMUPC, RTLNUM, RTLPRICE, PROMO2, FROM2, TO2

elseif DatePicker1.Value.Date = FROM3 then 
    If PROMO3.Contains("/") Then
        Dim words As String() = PROMO3.Split(New Char() {"/"c})
        RTLNUM = word(0)
        RTLPRICE = word(1)
        export = VENCD, ITEMUPC, RTLNUM, RTLPRICE, PROMO3, FROM3, TO3

就工作代码而言,这是我到目前为止所拥有的,

1)此代码需要很长时间才能完成,

2)它没有完全遍历DataGridView1(如导出的211/236)

        For item = 0 To DataGridView1.Rows.Count - 1 Step 1

            Dim VENCD As String = DataGridView1.Rows(item).Cells(0).Value.ToString()
            Dim ITEMUPC As String = DataGridView1.Rows(item).Cells(1).Value.ToString()
            Dim RTLNUM As String = DataGridView1.Rows(item).Cells(2).Value.ToString()
            Dim RTLPRICE As String = DataGridView1.Rows(item).Cells(3).Value.ToString()
            Dim PROMO1 As String = DataGridView1.Rows(item).Cells(4).Value.ToString()
            Dim FROM1 As String = DataGridView1.Rows(item).Cells(5).Value.ToString()
            Dim TO1 As String = DataGridView1.Rows(item).Cells(6).Value.ToString()
            Dim PROMO2 As String = DataGridView1.Rows(item).Cells(7).Value.ToString()
            Dim FROM2 As String = DataGridView1.Rows(item).Cells(8).Value.ToString()
            Dim TO2 As String = DataGridView1.Rows(item).Cells(9).Value.ToString()
            Dim PROMO3 As String = DataGridView1.Rows(item).Cells(10).Value.ToString()
            Dim FROM3 As String = DataGridView1.Rows(item).Cells(11).Value.ToString()
            Dim TO3 As String = DataGridView1.Rows(item).Cells(12).Value.ToString()

            If FROM1 = DatePicker1.Value.Date.ToString Then
                If PROMO1.Contains("/") Then
                    Dim words As String() = PROMO1.Split(New Char() {"/"c})
                        outputstring = VENCD & "," & ITEMUPC & "," & words(0) & "," & words(1) & "," & FROM1 & "," & TO1 & ", 20"
                        outputFile.WriteLine(outputstring)
                Else
                    outputstring = VENCD & "," & ITEMUPC & "," & RTLNUM & "," & RTLPRICE & "," & FROM1 & "," & TO1 & ", 20"
                    outputFile.WriteLine(outputstring)
                End If

            ElseIf FROM2 = DatePicker1.Value.Date.ToString Then
                If PROMO2.Contains("/") Then
                    Dim words As String() = PROMO2.Split(New Char() {"/"c})
                        outputstring = VENCD & "," & ITEMUPC & "," & words(0) & "," & words(1) & "," & FROM2 & "," & TO2 & ", 20"
                        outputFile.WriteLine(outputstring)
                Else
                    outputstring = VENCD & "," & ITEMUPC & "," & RTLNUM & "," & RTLPRICE & "," & FROM2 & "," & TO2 & ", 20"
                    outputFile.WriteLine(outputstring)
                End If

            ElseIf FROM3 = DatePicker1.Value.Date.ToString Then
                If PROMO3.Contains("/") Then
                    Dim words As String() = PROMO3.Split(New Char() {"/"c})
                        outputstring = VENCD & "," & ITEMUPC & "," & words(0) & "," & words(1) & "," & FROM3 & "," & TO3 & ", 20"
                        outputFile.WriteLine(outputstring)
                Else
                    outputstring = VENCD & "," & ITEMUPC & "," & RTLNUM & "," & RTLPRICE & "," & FROM3 & "," & TO3 & ", 20"
                    outputFile.WriteLine(outputstring)
                End If
            End If
        Next

任何帮助将不胜感激。

0 个答案:

没有答案