Export Gridview to Excel ได้ แต่ gridview ไม่ออกมา
ผมทำใน VB หน่ะครับ เอา ข้อมูลใน Datgrid มาออก Excel ทั้งหมด ไม่ว่าข้อมูลนั้นจะมี กี่แถวกี่คอลัมน์ ให้ Get ออกมาใส่ Excel ให้หมด
โค้ดจะงงและเยอะหน่อยนะครับ ลองดู
Code (VB.NET)
For i = 1 To 10000
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("th-th")
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim datestr As String = Now.ToString("yyyyMMddHmmss")
Dim tmpRpt As String = System.Windows.Forms.Application.StartupPath & "\ReportTmp\Qry\tmpQry.xls"
Dim newRpr As String = tmpRpt
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(newRpr) 'เปิดรายงาน
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
Dim countCol As Integer = DgvAllSupport.ColumnCount
Dim countRow As Integer = DgvAllSupport.RowCount
xlWorkSheet.Range("A2").Value = "ลำดับ"
'เพิ่ม เส้นขอบในเซลล์
xlWorkSheet.Range("A2").Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A2").Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A2").Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A2").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlBorderWeight.xlHairline
dlg_ProgressTo7.Show()
dlg_ProgressTo7.ProgressBar1.Value = 0
dlg_ProgressTo7.ProgressBar1.Maximum = countRow
' "กำลังใส่ลำดับ"
For CountNo = 1 To countRow
xlWorkSheet.Range("A" & CountNo + 2).Value = CountNo
'เพิ่ม เส้นขอบในเซลล์
xlWorkSheet.Range("A" & CountNo + 2).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A" & CountNo + 2).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A" & CountNo + 2).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range("A" & CountNo + 2).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlBorderWeight.xlHairline
dlg_ProgressTo7.ProgressBar1.Value = CountNo
dlg_ProgressTo7.Refresh()
Next CountNo
dlg_ProgressTo7.ProgressBar1.Value = 0
dlg_ProgressTo7.ProgressBar1.Maximum = countCol
For coli = 1 To countCol
Dim colname As String
colname = Choose(coli, "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY")
xlWorkSheet.Range("A1").Value = Me.Text
xlWorkSheet.Range(colname & 2).Value = DgvAllSupport.Columns.Item(coli - 1).HeaderText
'เพิ่ม เส้นขอบในเซลล์
xlWorkSheet.Range(colname & 2).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & 2).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & 2).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & 2).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlBorderWeight.xlHairline
For rowi = 0 To countRow
Try
xlWorkSheet.Range(colname & rowi + 2).Value = DgvAllSupport.Rows(rowi - 1).Cells(coli - 1).Value.ToString
'เพิ่ม เส้นขอบในเซลล์
xlWorkSheet.Range(colname & rowi + 2).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & rowi + 2).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & rowi + 2).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlBorderWeight.xlHairline
xlWorkSheet.Range(colname & rowi + 2).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlBorderWeight.xlHairline
Catch ex As Exception
'MsgBox(ex.Message)
End Try
dlg_ProgressTo7.txtStatus.Text = "กำลัง Export ข้อมูล (คอลัมน์ที่ " & coli & " / " & countCol & " แถวที่ " & rowi & " / " & countRow & ")"
Next rowi
dlg_ProgressTo7.ProgressBar1.Value = coli
Next coli
MessageBox.Show("ทำการ Copy ข้อมูลทั้งหมดไปยังไฟล์ Excel เรียบร้อยแล้ว", "การทำงานเสร็จสิ้น", MessageBoxButtons.OK, MessageBoxIcon.Information)
xlApp.Visible = True
Next
Datagrid ข้อมูลเยอะยิ่งนาน
Date :
2014-11-12 12:37:10
By :
zarooman
ตอนนี้ Export Header ได้แล้วครับ ปัญหาน่าจะอยู่ตรงที่จับ Rows ไม่ได้ เพราะอาจจะไม่เห็น control ข้างใน
Date :
2014-11-12 16:21:47
By :
sadowskyz
ได้ยังคะช่าง
5555555555+
Date :
2014-11-19 16:22:16
By :
PiggyGirl
Load balance : Server 04