|  | 
	                
  
    |  |  
    | 
        
        เรื่องการ Export DataGridView เป็นไฟล์ excel คับ แนะนำทีคับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | Error แบบนี้อะครับ 
  ข้อมูลออกนะคับ แต่ออกไม่ครบ ละ error
 
  
 
 อันนี้ code คับแนะนำทีคับ
 Code (VB.NET)
 
 Private Sub report()
        Dim i As Integer = 0
        Dim j As Integer = 5
        Dim i2 As Integer = 0
        Dim j2 As Integer = 5
        Dim ExcelApp As Excel.Application
        Dim ExcelBooks As Excel.Workbook
        Dim ExcelSheets As Excel.Worksheet
        ExcelApp = New Excel.Application
        Dim chartRange As Excel.Range
        ExcelApp.Visible = True
        ExcelBooks = ExcelApp.Workbooks.Add()
        ExcelSheets = DirectCast(ExcelBooks.Worksheets(1), Excel.Worksheet)
        ExcelSheets.Name = "กรมธรรม์"
        Dim CurrentThread As System.Threading.Thread
        CurrentThread = System.Threading.Thread.CurrentThread
        CurrentThread.CurrentCulture = New CultureInfo("en-US")
        CurrentThread.CurrentCulture = New CultureInfo("th-TH")
        Dim dsChk As New DataSet
        Dim sqlselect As String = ""
        Dim idagent As String = ""
        Dim addagent As String = ""
        With ExcelSheets
            chartRange = ExcelSheets.Range("A1", "I1")
            chartRange.Merge()
            chartRange.FormulaR1C1 = "รายงานแสดงรายละเอียดกรมธรรม์ทั้งหมด"
            chartRange.HorizontalAlignment = 3
            chartRange.VerticalAlignment = 3
            chartRange = ExcelSheets.Range("A2", "I2")
            chartRange.Merge()
            chartRange.FormulaR1C1 = "บริษัท ทิพยประกันภัย จำกัด ศูนย์ ระยอง"
            chartRange.HorizontalAlignment = 3
            chartRange.VerticalAlignment = 3
            .Range("A1").Font.Size = 20
            .Range("A2").Font.Size = 16
            '0
            .Range("A4").Value = "รหัสสาขา"
            ' .Range("A4").Borders.LineStyle = True
            '.Range("A4").Font.Bold = True
            '.Range("A4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            '1
            .Range("B4").Value = "ชื่อสาขา"
            '.Range("B4").Borders.LineStyle = True
            '.Range("B4").Font.Bold = True
            ' .Range("B4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            '2
            
            Dim t0 As String
            Dim t1 As String
            For i = 0 To DataGridView_Branch.Rows.Count - 2
                t0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
                t1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
               
                .Range("A" & j.ToString()).Value = t0.ToString
                '.Range("A" & j.ToString()).Borders.LineStyle = True
                ' .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                .Range("B" & j.ToString()).Value = t1.ToString
                '.Range("B" & j.ToString()).Borders.LineStyle = True
                ' .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
               
                j += 1
            Next
            chartRange = ExcelSheets.Range("A5", "A" & j.ToString() - 1)
            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
            chartRange = ExcelSheets.Range("B5", "B" & j.ToString() - 1)
            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
        End With
        ExcelSheets = Nothing
        ExcelBooks = Nothing
        ExcelApp = Nothing
        '-----------------------------------------
    End Sub
 
 
 Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2008 (.NET 3.x)
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2015-07-20 09:28:46 | By :
                          aphisit007 | View :
                          1962 | Reply :
                          7 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | มันติดตรง j ที่เท่าไหร่ครับ 
 Code (VB.NET)
 
 For i = 0 To DataGridView_Branch.Rows.Count - 2
                .Range("A" & (i-5).ToString()).Value = DataGridView_Branch(0,i).Value.ToString()
                .Range("B" & (i-5).ToString()).Value =DataGridView_Branch(1,i).Value.ToString
            Next
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 09:49:25 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | j ไว้ กำหนด Range อะคับ A1 A2 ข้อมูลออกบางครั้งบรรทัด ไม่เท่ากันอะคับ แล้วก็ Error
 บางครั้งออก 10 บรรดทัด บางครั้งกะออก 10 กว่าบรรดทัด
 แต่ code เดียวกันนี้อะคับ ยัง งงๆ
 
 |  
              | 
 ประวัติการแก้ไข
 2015-07-20 10:02:41
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 10:00:50 | By :
                            aphisit007 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ถ้าใช้ MS Excel ผมเจอปัญหาเดียวกันครับ มันจะ Random Error
 เหมือนกับว่า Excel มันทำงานไม่ทันอ่ะครับ
 ลองเปลี่ยนมาเป็นตัวนี้ดูครับ
 http://epplus.codeplex.com/
 
 
 วิธีใช้
 http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-Report-in
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 10:13:14 | By :
                            fonfire |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ยัง error อยู่เลยครับ เหมือนเดิมเลย ฮ่าๆ 
 Code (VB.NET)
 
    Private Sub report()
        Dim i As Integer = 0
        Dim j As Integer = 5
        ''''''''''''''''' บุคลากร
        Dim i2 As Integer = 0
        Dim j2 As Integer = 5
        Dim ExcelApp As Excel.Application
        Dim ExcelBooks As Excel.Workbook
        Dim ExcelSheets As Excel.Worksheet
        ExcelApp = New Excel.Application
        Dim chartRange As Excel.Range
        ExcelApp.Visible = True
        ExcelBooks = ExcelApp.Workbooks.Add()
        ExcelSheets = DirectCast(ExcelBooks.Worksheets(1), Excel.Worksheet)
        ExcelSheets.Name = "กรมธรรม์"
        Dim CurrentThread As System.Threading.Thread
        CurrentThread = System.Threading.Thread.CurrentThread
        CurrentThread.CurrentCulture = New CultureInfo("en-US")
        CurrentThread.CurrentCulture = New CultureInfo("th-TH")
        Dim dsChk As New DataSet
        Dim sqlselect As String = ""
        Dim idagent As String = ""
        Dim addagent As String = ""
        With ExcelSheets
            chartRange = ExcelSheets.Range("A1", "I1")
            chartRange.Merge()
            chartRange.FormulaR1C1 = "รายงานแสดงรายละเอียดกรมธรรม์ทั้งหมด"
            chartRange.HorizontalAlignment = 3
            chartRange.VerticalAlignment = 3
            chartRange = ExcelSheets.Range("A2", "I2")
            chartRange.Merge()
            chartRange.FormulaR1C1 = "บริษัท ทิพยประกันภัย จำกัด ศูนย์ ระยอง"
            chartRange.HorizontalAlignment = 3
            chartRange.VerticalAlignment = 3
           
            .Range("A1").Font.Size = 20
            .Range("A2").Font.Size = 16
            '0
            .Range("A4").Value = "บริษัท"
            .Range("A4").Borders.LineStyle = True
            .Range("A4").Font.Bold = True
            .Range("A4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            '1
            .Range("B4").Value = "ชื่อโรงเรียน"
            .Range("B4").Borders.LineStyle = True
            .Range("B4").Font.Bold = True
            .Range("B4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
           
            Dim t0 As String
            Dim t1 As String
            For i = 0 To DataGridView_Branch.Rows.Count - 2
                t0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
                t1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
               
                .Range("A" & j.ToString()).Value = t0.ToString
                .Range("A" & j.ToString()).Borders.LineStyle = True
                .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                .Range("B" & j.ToString()).Value = t1.ToString
                .Range("B" & j.ToString()).Borders.LineStyle = True
                .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                j += 1
            Next
            chartRange = ExcelSheets.Range("A5", "A" & j.ToString() - 1)
            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
            chartRange = ExcelSheets.Range("B5", "B" & j.ToString() - 1)
            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
         
        End With
 |  
              | 
 ประวัติการแก้ไข
 2015-07-20 13:00:04
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 12:49:00 | By :
                            aphisit007 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลอง export โดยไม่เกี่ยวกับ datagrid ดูครับประมาณ 
 Code (VB.NET)
 
  For i = 0 To DataGridView_Branch.Rows.Count - 2
             't0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
             't1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
            
             .Range("A" & j.ToString()).Value = "Test A " & i 
             .Range("A" & j.ToString()).Borders.LineStyle = True
             .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
             .Range("B" & j.ToString()).Value = "Test B " & i 
             .Range("B" & j.ToString()).Borders.LineStyle = True
             .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
             j += 1
         Next
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 14:19:11 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ผมน่าจะรู้แล้วคับ คือ ผม export ละให้มัน 
 ExcelApp.Visible = True เลย ซึ่ง excel ผมยังไม่ได้ Activation
 พอมันรันมันก็ถามจะให้  Activation  โปรแกรมเลยหยุดการทำงานอะคับ
 เลยตัดตรงส่วนนั้นออก ไปก่อนคับ ทำได้เลยคับ ฮ่าๆ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-07-20 16:46:14 | By :
                            aphisit007 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |