Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > เรื่องการ Export DataGridView เป็นไฟล์ excel คับ แนะนำทีคับ


 

[.NET] เรื่องการ Export DataGridView เป็นไฟล์ excel คับ แนะนำทีคับ

 
Topic : 117912



โพสกระทู้ ( 21 )
บทความ ( 0 )



สถานะออฟไลน์



Error แบบนี้อะครับ
ฟห
ข้อมูลออกนะคับ แต่ออกไม่ครบ ละ error
ฟหด


อันนี้ code คับแนะนำทีคับ
Code (VB.NET)
001.Private Sub report()
002. 
003.        Dim i As Integer = 0
004.        Dim j As Integer = 5
005. 
006.        Dim i2 As Integer = 0
007.        Dim j2 As Integer = 5
008.        Dim ExcelApp As Excel.Application
009.        Dim ExcelBooks As Excel.Workbook
010.        Dim ExcelSheets As Excel.Worksheet
011. 
012.        ExcelApp = New Excel.Application
013.        Dim chartRange As Excel.Range
014. 
015. 
016.        ExcelApp.Visible = True
017.        ExcelBooks = ExcelApp.Workbooks.Add()
018.        ExcelSheets = DirectCast(ExcelBooks.Worksheets(1), Excel.Worksheet)
019.        ExcelSheets.Name = "กรมธรรม์"
020. 
021. 
022.        Dim CurrentThread As System.Threading.Thread
023.        CurrentThread = System.Threading.Thread.CurrentThread
024.        CurrentThread.CurrentCulture = New CultureInfo("en-US")
025.        CurrentThread.CurrentCulture = New CultureInfo("th-TH")
026. 
027. 
028.        Dim dsChk As New DataSet
029.        Dim sqlselect As String = ""
030.        Dim idagent As String = ""
031.        Dim addagent As String = ""
032. 
033.        With ExcelSheets
034. 
035. 
036.            chartRange = ExcelSheets.Range("A1", "I1")
037.            chartRange.Merge()
038.            chartRange.FormulaR1C1 = "รายงานแสดงรายละเอียดกรมธรรม์ทั้งหมด"
039.            chartRange.HorizontalAlignment = 3
040.            chartRange.VerticalAlignment = 3
041. 
042.            chartRange = ExcelSheets.Range("A2", "I2")
043.            chartRange.Merge()
044.            chartRange.FormulaR1C1 = "บริษัท ทิพยประกันภัย จำกัด ศูนย์ ระยอง"
045.            chartRange.HorizontalAlignment = 3
046.            chartRange.VerticalAlignment = 3
047. 
048. 
049. 
050. 
051.            .Range("A1").Font.Size = 20
052.            .Range("A2").Font.Size = 16
053. 
054.            '0
055.            .Range("A4").Value = "รหัสสาขา"
056.            ' .Range("A4").Borders.LineStyle = True
057.            '.Range("A4").Font.Bold = True
058.            '.Range("A4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
059.            '1
060.            .Range("B4").Value = "ชื่อสาขา"
061.            '.Range("B4").Borders.LineStyle = True
062.            '.Range("B4").Font.Bold = True
063.            ' .Range("B4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
064.            '2
065.             
066. 
067.            Dim t0 As String
068.            Dim t1 As String
069. 
070.            For i = 0 To DataGridView_Branch.Rows.Count - 2
071.                t0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
072. 
073.                t1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
074. 
075.                
076.                .Range("A" & j.ToString()).Value = t0.ToString
077. 
078.                '.Range("A" & j.ToString()).Borders.LineStyle = True
079.                ' .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
080. 
081. 
082.                .Range("B" & j.ToString()).Value = t1.ToString
083.                '.Range("B" & j.ToString()).Borders.LineStyle = True
084.                ' .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
085. 
086.                
087.                j += 1
088.            Next
089.            chartRange = ExcelSheets.Range("A5", "A" & j.ToString() - 1)
090.            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
091.            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
092.            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
093. 
094.            chartRange = ExcelSheets.Range("B5", "B" & j.ToString() - 1)
095.            chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
096.            Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
097.            xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
098. 
099.        End With
100. 
101. 
102.        ExcelSheets = Nothing
103.        ExcelBooks = Nothing
104.        ExcelApp = Nothing
105.        '-----------------------------------------
106. 
107. 
108.    End Sub




Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2008 (.NET 3.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-07-20 09:28:46 By : aphisit007 View : 1896 Reply : 7
 

 

No. 1



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

มันติดตรง j ที่เท่าไหร่ครับ

Code (VB.NET)
1.For i = 0 To DataGridView_Branch.Rows.Count - 2
2.                .Range("A" & (i-5).ToString()).Value = DataGridView_Branch(0,i).Value.ToString()
3. 
4.                .Range("B" & (i-5).ToString()).Value =DataGridView_Branch(1,i).Value.ToString
5. 
6.            Next

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 09:49:25 By : lamaka.tor
 

 

No. 2



โพสกระทู้ ( 21 )
บทความ ( 0 )



สถานะออฟไลน์


j ไว้ กำหนด Range อะคับ A1 A2
ข้อมูลออกบางครั้งบรรทัด ไม่เท่ากันอะคับ แล้วก็ Error
บางครั้งออก 10 บรรดทัด บางครั้งกะออก 10 กว่าบรรดทัด
แต่ code เดียวกันนี้อะคับ ยัง งงๆ


ประวัติการแก้ไข
2015-07-20 10:02:41
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 10:00:50 By : aphisit007
 

 

No. 3



โพสกระทู้ ( 821 )
บทความ ( 0 )



สถานะออฟไลน์


ถ้าใช้ 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
 

 

No. 4



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 2 เขียนโดย : aphisit007 เมื่อวันที่ 2015-07-20 10:00:50
รายละเอียดของการตอบ ::
ทั้งๆที่ DataGridView_Branch.Rows.Count - 2 เท่ากันเหรอครับ
งง ๆๆๆๆ ผมไม่เคยเป็น
แต่ผมใช้ 2007 ver นั้ เสถียรดีครับไม่กินแรม

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 10:38:53 By : lamaka.tor
 

 

No. 5



โพสกระทู้ ( 21 )
บทความ ( 0 )



สถานะออฟไลน์


ยัง error อยู่เลยครับ เหมือนเดิมเลย ฮ่าๆ

Code (VB.NET)
01.Private Sub report()
02. 
03.     Dim i As Integer = 0
04.     Dim j As Integer = 5
05.     ''''''''''''''''' บุคลากร
06.     Dim i2 As Integer = 0
07.     Dim j2 As Integer = 5
08.     Dim ExcelApp As Excel.Application
09.     Dim ExcelBooks As Excel.Workbook
10.     Dim ExcelSheets As Excel.Worksheet
11. 
12.     ExcelApp = New Excel.Application
13.     Dim chartRange As Excel.Range
14. 
15. 
16.     ExcelApp.Visible = True
17.     ExcelBooks = ExcelApp.Workbooks.Add()
18.     ExcelSheets = DirectCast(ExcelBooks.Worksheets(1), Excel.Worksheet)
19.     ExcelSheets.Name = "กรมธรรม์"
20. 
21. 
22.     Dim CurrentThread As System.Threading.Thread
23.     CurrentThread = System.Threading.Thread.CurrentThread
24.     CurrentThread.CurrentCulture = New CultureInfo("en-US")
25.     CurrentThread.CurrentCulture = New CultureInfo("th-TH")
26. 
27. 
28.     Dim dsChk As New DataSet
29.     Dim sqlselect As String = ""
30.     Dim idagent As String = ""
31.     Dim addagent As String = ""
32. 
33.     With ExcelSheets
34. 
35. 
36.         chartRange = ExcelSheets.Range("A1", "I1")
37.         chartRange.Merge()
38.         chartRange.FormulaR1C1 = "รายงานแสดงรายละเอียดกรมธรรม์ทั้งหมด"
39.         chartRange.HorizontalAlignment = 3
40.         chartRange.VerticalAlignment = 3
41. 
42.         chartRange = ExcelSheets.Range("A2", "I2")
43.         chartRange.Merge()
44.         chartRange.FormulaR1C1 = "บริษัท ทิพยประกันภัย จำกัด ศูนย์ ระยอง"
45.         chartRange.HorizontalAlignment = 3
46.         chartRange.VerticalAlignment = 3
47. 
48. 
49. 
50.         
51. 
52.         .Range("A1").Font.Size = 20
53.         .Range("A2").Font.Size = 16
54. 
55.         '0
56.         .Range("A4").Value = "บริษัท"
57.         .Range("A4").Borders.LineStyle = True
58.         .Range("A4").Font.Bold = True
59.         .Range("A4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
60.         '1
61.         .Range("B4").Value = "ชื่อโรงเรียน"
62.         .Range("B4").Borders.LineStyle = True
63.         .Range("B4").Font.Bold = True
64.         .Range("B4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
65.         
66. 
67.         Dim t0 As String
68.         Dim t1 As String
69. 
70.         For i = 0 To DataGridView_Branch.Rows.Count - 2
71.             t0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
72. 
73.             t1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
74.             
75. 
76.             .Range("A" & j.ToString()).Value = t0.ToString
77.             .Range("A" & j.ToString()).Borders.LineStyle = True
78.             .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
79. 
80. 
81.             .Range("B" & j.ToString()).Value = t1.ToString
82.             .Range("B" & j.ToString()).Borders.LineStyle = True
83.             .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
84. 
85.             j += 1
86.         Next
87.         chartRange = ExcelSheets.Range("A5", "A" & j.ToString() - 1)
88.         chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
89.         Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
90.         xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
91. 
92.         chartRange = ExcelSheets.Range("B5", "B" & j.ToString() - 1)
93.         chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, _
94.         Excel.XlBorderWeight.xlThin, Excel.XlColorIndex. _
95.         xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
96. 
97.       
98.     End With



ประวัติการแก้ไข
2015-07-20 13:00:04
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 12:49:00 By : aphisit007
 

 

No. 6



โพสกระทู้ ( 4,440 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ลอง export โดยไม่เกี่ยวกับ datagrid ดูครับประมาณ

Code (VB.NET)
01.For i = 0 To DataGridView_Branch.Rows.Count - 2
02.            't0 = CStr(DataGridView_Branch.Rows(i).Cells(0).Value.ToString)
03. 
04.            't1 = CStr(DataGridView_Branch.Rows(i).Cells(1).Value.ToString)
05.            
06. 
07.            .Range("A" & j.ToString()).Value = "Test A " & i
08.            .Range("A" & j.ToString()).Borders.LineStyle = True
09.            .Range("A" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
10. 
11. 
12.            .Range("B" & j.ToString()).Value = "Test B " & i
13.            .Range("B" & j.ToString()).Borders.LineStyle = True
14.            .Range("B" & j.ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
15. 
16.            j += 1
17.        Next

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 14:19:11 By : lamaka.tor
 

 

No. 7



โพสกระทู้ ( 21 )
บทความ ( 0 )



สถานะออฟไลน์


ผมน่าจะรู้แล้วคับ คือ ผม export ละให้มัน

ExcelApp.Visible = True เลย ซึ่ง excel ผมยังไม่ได้ Activation
พอมันรันมันก็ถามจะให้ Activation โปรแกรมเลยหยุดการทำงานอะคับ
เลยตัดตรงส่วนนั้นออก ไปก่อนคับ ทำได้เลยคับ ฮ่าๆ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-07-20 16:46:14 By : aphisit007
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เรื่องการ Export DataGridView เป็นไฟล์ excel คับ แนะนำทีคับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่