vb.net导出到excel

网了查了一堆资料,自己也写出了到出到excel的方法.记下来.

Try

            Dim errMsg As String = ""
            Dim CheckedCount As Integer = Me.GpDetailListView.CheckedItems.Count
            If CheckedCount < 1 Then
                MsgBox("请选择要导出的项!", MsgBoxStyle.Information, "提示")
                Return
            End If
            Console.WriteLine("选择数量:" & CheckedCount.ToString)

            Me.ExportButton.Text = "正在导出..."
            Me.ExportButton.Enabled = False
            Me.PictureBox1.Visible = True

            If Not My.Computer.FileSystem.DirectoryExists("ExportedExcel") Then
                My.Computer.FileSystem.CreateDirectory("ExportedExcel")
            End If

            Dim strNow As String = Format(Now(), "yyyy_MM_dd_HHmmss").ToString
            Dim strFileName As String = My.Computer.FileSystem.CurrentDirectory & "\ExportedExcel\ExportedGP" & strNow & ".xlsx"
            'System.IO.File.Create(strFileName)

            Dim excelApp = New Excel.Application ' CreateObject("Excel.Application")
            excelApp.Visible = False
            excelApp.DisplayAlerts = False
            Dim excelWorkbook As Excel.Workbook
            Dim misValue As Object = System.Reflection.Missing.Value
            excelWorkbook = excelApp.Workbooks.Add(misValue)
            Dim excelSheet As Excel.Worksheet = excelWorkbook.Worksheets(1)
            excelSheet.Name = "工票信息"
            Dim excelRange As Excel.Range
            excelSheet.Cells(1, 1) = "员工"
            excelSheet.Cells(1, 2) = "产品类别"
            excelSheet.Cells(1, 3) = "工位"
            excelSheet.Cells(1, 4) = "数量"
            excelRange = excelSheet.Cells(1, 1)
            excelRange.Font.FontStyle = FontStyle.Bold
            excelRange.Font.Size = 12
            excelRange = excelSheet.Cells(1, 2)
            excelRange.Font.FontStyle = FontStyle.Bold
            excelRange.Font.Size = 12
            excelRange = excelSheet.Cells(1, 3)
            excelRange.Font.FontStyle = FontStyle.Bold
            excelRange.Font.Size = 12
            excelRange = excelSheet.Cells(1, 4)
            excelRange.Font.FontStyle = FontStyle.Bold
            excelRange.Font.Size = 12
            excelSheet.

            For i As Integer = 0 To CheckedCount - 1
                Dim CheckItemId As String = Me.GpDetailListView.CheckedItems(i).SubItems.Item(4).Text
                For j As Integer = 0 To Me.dtGpInfo.Rows.Count - 1
                    Dim dtRowId As String = Me.dtGpInfo.Rows(j).Item("id").ToString
                    If CheckItemId = dtRowId Then
                        Dim cnName As String = Me.dtGpInfo.Rows(j).Item("cnName").ToString
                        Dim prdType As String = Me.dtGpInfo.Rows(j).Item("prdType").ToString
                        Dim posName As String = Me.dtGpInfo.Rows(j).Item("posName").ToString
                        Dim count As String = Me.dtGpInfo.Rows(j).Item("count").ToString

                        excelSheet.Cells(j + 2, 1) = cnName
                        excelSheet.Cells(j + 2, 2) = prdType
                        excelSheet.Cells(j + 2, 3) = posName
                        excelSheet.Cells(j + 2, 4) = count
                    End If
                Next
            Next
            excelWorkbook.SaveAs(strFileName)
            excelApp.Quit()
            Me.ExportButton.Text = "导出到Excel"
            Me.ExportButton.Enabled = True
            Me.PictureBox1.Visible = False
            MsgBox("导出成功:" & strFileName, MsgBoxStyle.MsgBoxSetForeground, "导出成功")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "导出异常")
            Me.ExportButton.Text = "导出到Excel"
            Me.ExportButton.Enabled = True
            Me.PictureBox1.Visible = False
            Return
        End Try

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老侯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值