vb.net匯出到excel

來源:互聯網
上載者:User

網了查了一堆資料,自己也寫出了到出到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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.