匯出Excel

匯出Excel

 

Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet

            xlWorkBook = New Excel.Application().Workbooks.Add(Missing.Value)
            xlWorkBook.Application.Visible = True
            xlWorkSheet = xlWorkBook.ActiveSheet

            Dim i As Integer = 2
            With xlWorkSheet
                .Cells(1, 1) = "機台編號"
                .Cells(1, 2) = "機台名稱"
                .Cells(1, 3) = "類別"
                .Cells(1, 4) = "不良原因"
                .Cells(1, 5) = "維修內容"
                .Cells(1, 6) = "申請日期"
                .Cells(1, 7) = "維修開始日期"
                .Cells(1, 8) = "開始時間"
                .Cells(1, 9) = "維修完成日期"
                .Cells(1, 10) = "完成時間"
                .Cells(1, 11) = "確認時間"
                .Cells(1, 12) = "轉換時間"
                .Cells(1, 13) = "維修人員"
                .Cells(1, 14) = "生產批號"

                xlWorkSheet.Range("$A1:$N1").Font.ColorIndex = Excel.Constants.xlColor3
                xlWorkSheet.Range("$A1:$N1").Font.Bold = True

                For Each dr As Data.DataRow In CType(Session("ReportDT"), Data.DataTable).Rows
                    With xlWorkSheet
                        .Cells(i, 1) = dr(0)
                        .Cells(i, 2) = dr(1)
                        .Cells(i, 3) = dr(2)
                        .Cells(i, 4) = dr(3)
                        .Cells(i, 5) = dr(4)
                        .Cells(i, 6) = dr(5)
                        .Cells(i, 7) = dr(6)
                        .Cells(i, 8) = dr(7)
                        .Cells(i, 9) = dr(8)
                        .Cells(i, 10) = dr(9)
                        .Cells(i, 11) = dr(10)
                        .Cells(i, 12) = dr(11)
                        .Cells(i, 13) = dr(12)
                        .Cells(i, 14) = dr(13)

                        i = i + 1
                    End With
                Next

                xlWorkSheet.Columns.AutoFit()
                xlWorkBook.Close()

            End With