 |
|
ทำยังให้เอา Header ใน DataGridview มาใส่ใน Excel ได้ครับ งง ไปหมดแล้ว แก้จนปวดหัว!!! |
|
 |
|
|
 |
 |
|
ดูแล้วงงๆ เอา Name ของ คอลัมdatagrid มา .HeaderText เช่น gv_Rcode.HeaderText , gv_Rname.HeaderText
ของคุณตั้งเป็นอะไรผมไม่รู้อะเอามาใส่เองดูนะ
จากที่ดู อันนี้น่าจะเป็นเอาข้อมูล datagrid ไปใส่ใน workshhet
Code (VB.NET)
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
งั้นผมคิดว่าน่าจะเป็นงี้
Code (VB.NET)
xlWorkSheet.Cells(0, 0) = gv_Rcode.HeaderText.ToString()
xlWorkSheet.Cells(0, 1) = gv_Rname.HeaderText.ToString()
xlWorkSheet.Cells(0, 2) = gv_report.HeaderText.ToString()
ไม่รู้ว่า i เป็นแถวหรือ j เป็นแถวสลับดูแล้วกันครับ
|
ประวัติการแก้ไข 2011-04-26 02:05:13 2011-04-26 02:05:19 2011-04-26 02:06:46 2011-04-26 02:06:50
 |
 |
 |
 |
Date :
2011-04-26 02:04:42 |
By :
hamzter |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สวนนี้ผมเพิ่มเข้าไปคับ
Code (VB.NET)
With xlApp.ActiveSheet.Cells(1, 1) 'ส่วนนี้คือช่องแรกช่องเด๋วน่ะคับ คือ A1
.Value = "Report Code"
.Font.Bold = True
.VerticalAlignment = -4108
.HorizontalAlignment = -4108
.BORDERS.Weight = 1
End With
แล้วมาเปลี่ยนโค้ดตัวนี้
Code (VB.NET)
For i = 1 To DataGridView.RowCount - 2 '<<< ผมแก้ i=0 เป็น i=1 เพื่อต้องการให้มันขึ้นบรรทัดที่ 2 ใน excel
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
ปัญหามันยุตรงนี้คับ

คือผมอยากให้มันเริ่มตั้งแต่ บรรทัดที่ 2 คับ..เพราะ บรรทัดแรกจะใส่ header ลงไป ช่วยผมทีน่ะคับ
ไม่ทราบว่าจะพอเข้าใจในคำถามรึเปล่าน่ะคับ..ก้ ช่วย ๆ ผมทีเถอะครับ งง...คับ
|
ประวัติการแก้ไข 2011-04-26 09:19:52
 |
 |
 |
 |
Date :
2011-04-26 09:09:47 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Link ที่ให้มา คุณ Mi16 คับ ผมดูไม่ค่อยรู้เรื่องอ่ะคับ...ช่วยผมอีกนิดได้มั้ยคับ ขอรบกวนได้มั้ยคับ ขอที ๆ น่ะคับ ไม่รู้จะทำยังไง แล้ว ตอนนี้...
คุณ hamster ตามตัวอย่างที่ให้ผมมา คือ gv_Rcode อันนนี้คุณ สมมุติว่ามันคือ ชื่อ feild หรอคับ..ผมลองแร้ว มันไม่ได้ คับ
พี่วิน ขอบคุณมากคับ สำหรับ link แต่ ดู แร้ว ผมก้ยังเอามา ใช้ไม่ได้ อ่าคับ...
ขอรบกวนพี่ ๆ ที่มีประสบการณ์ช่วยผมทีน่ะคับ..ตอนนี้ งง ม๊าาก คับ
รึว่า..ถ้ายังไง พอจะมีวิธีที่จะให้มัน เขียนลงไปใน excel เริ่มตั้งแต่ cell ที่ 2 ได้มั้ยคับ cell ที่ 1 เว้นไว้ใส่ header ฝากด้วยน่ะคับพี่ ๆ ทุกคน
|
ประวัติการแก้ไข 2011-04-26 10:58:59
 |
 |
 |
 |
Date :
2011-04-26 10:57:23 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมด้วยคร๊าาฟ ทำไม่ได้ จริง ๆ ๆ
|
 |
 |
 |
 |
Date :
2011-04-26 14:03:30 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ทำไม่ได้ คับ..พี่ ๆ คนไหน มีความคิดที่จะ export วิธีอื่นมั้งมั้ยคับ..คือผมตอนนี้ VS2010 มันเป็น crystal report ไม่ได้ คับ
จะออกเป็นตัวไหน ดีที่มันจะมี ข้อมูลครบตามเราต้องการคับ..ถามพี่ ๆผู้มีประสบการณ์มากกว่าผม
|
 |
 |
 |
 |
Date :
2011-04-26 14:56:30 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแก้ตัวนี้ดูนะครับ
For i = 0 To DataGridView.RowCount - 2
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
เป็นแบบนี้ครับ
For i = 1 To DataGridView.RowCount - 2
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
ลองดูครับ น่าจาช่วยได้
|
 |
 |
 |
 |
Date :
2011-04-26 15:11:17 |
By :
Aut_KuOn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษที แก้ผิดที่
For i = 0 To DataGridView.RowCount - 2
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
เป็นแบบนี้ครับ
For i = 1 To DataGridView.RowCount - 1
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
ลองดูครับ น่าจาช่วยได้
|
 |
 |
 |
 |
Date :
2011-04-26 15:18:09 |
By :
Aut_KuOn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ๊อ...ไม่ได้ คับ
มันขึ้น

คับ
|
 |
 |
 |
 |
Date :
2011-04-26 15:45:26 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างที่ถูกต้อง ง่ายๆ ที่สุดในโลก
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText.ToString()
Next
For i = 1 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i - 1).Value.ToString()
Next
Next
Dim ExcelName As String 'ตั้งชื่อให้ file excel
ExcelName = "test"
xlWorkSheet.SaveAs("D:\" & ExcelName & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
End Sub
|
 |
 |
 |
 |
Date :
2011-04-26 19:09:26 |
By :
Miss Chansira |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากคับ...อันนี้ถูกเรยคับ แต่ ตัวสุดท้ายหายไป เปลี่ยนนิดเด๋ว คับ มันมีตัวที่ผิดยุ
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText.ToString()
Next
For i = 1 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i).Value.ToString() 'เดิมมันเป็น (j,i-1)
Next
Next
Dim ExcelName As String 'ตั้งชื่อให้ file excel
ExcelName = "test"
xlWorkSheet.SaveAs("D:\" & ExcelName & ".xlsx")
xlWorkBook.Close()
xlApp.Quit()
End Sub
|
 |
 |
 |
 |
Date :
2011-04-26 20:57:48 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอดีว่า..ตอนแรกนึกว่าใช้ได้แล้วคับ...ดูไม่ดี อุตส่าดีใจทั้งคืน
มันก้จะหายไป 1 cell เหมือนเดิมคับ
ถ้า i-1 cells ล่างสุดก้หาย
ถ้าเป็น i cells บนสุดก้จะหาย
งงมากคับ..ขอความอนุเคราะห์อีกซักที นึงคับ..มันมีปัญหา อีกแร้ว
|
 |
 |
 |
 |
Date :
2011-04-27 11:14:08 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
11.
For j = 0 To DataGridView1.ColumnCount - 1
12.
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText.ToString()
13.
Next
ตรงนี้รึป่าวที่หายลองเอาสีแดงออกดู
|
ประวัติการแก้ไข 2011-04-27 15:23:40
 |
 |
 |
 |
Date :
2011-04-27 15:22:59 |
By :
hamzter |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คับ..ถ้าเอาออก มันจะไม่ดึง header มาคับ แต่ไม่ตอนนี้มีมาใหม่แร้ว ใช้ได้ ดูเอาน่ะคับ
Code (VB.NET)
For i = 0 To DataGridView.RowCount - 2 'ส่วนนี้ก้เป็นการเอาข้อมูลมาปกติแบบไม่ต้องเอาheader มาแสดงเหมืนตอนแรก
For j = 0 To DataGridView.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView(j, i).Value.ToString()
Next
Next
xlWorkSheet.Cells(1, 1).insert() 'พอหลุดลูป แล้วก้เพิ่ม cell ไปที่บรรทัดแรกให้เป็นค่าว่าง
With xlApp.ActiveSheet.Cells(1, 1) ' อันนี้ค่อยใส่ค่าลงไปใน cell ที่เป็นค่าว่างแทน
.Value = "Report Code"
.Font.Bold = True
.VerticalAlignment = -4108
.HorizontalAlignment = -4108
.BORDERS.Weight = 1
End With
เอาไปประยุกต์ใช้ได้น่ะน่ะคับ ^^
|
 |
 |
 |
 |
Date :
2011-04-28 14:20:52 |
By :
termja |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|