none
导出到excel2010问题 RRS feed

  • 问题

  •    Public Sub DcExcel(ByVal DGV As DataGridView)
            '把datagridview中的数据导出到excel
            Dim wapp As New Microsoft.Office.Interop.Excel.Application
            Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim wbook As Microsoft.Office.Interop.Excel.Workbook

            On Error Resume Next

            wapp.Visible = True
            wbook = wapp.Workbooks.Add()
            wsheet = wbook.ActiveSheet

            Dim iX As Integer
            Dim iY As Integer
            Dim iC As Integer

            For iC = 0 To DGV.Columns.Count - 1
                wsheet.Cells(1, iC + 1).Value = DGV.Columns(iC).HeaderText
                wsheet.Cells(1, iC + 1).Font.Bold = True
            Next

            wsheet.Rows(2).select()
            For iX = 0 To DGV.Rows.Count - 1
                For iY = 0 To DGV.Columns.Count - 1
                    wsheet.Cells(iX + 2, iY + 1).value = DGV(iY, iX).Value.ToString
                Next
            Next
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dvg As DataGridView
            Call DcExcel(dvg)

        End Sub

    为什么我导出datagridview的资料到excel2010,无法显示出内容。只是直接打开excel的文件但是却没有内容??

    2012年2月16日 4:26

答案

  • Dim dvg As DataGridView
    Call DcExcel(dvg)

    1.您的DataGridView沒有實體化呢!
    2.如果沒有將資料Bind給DataGridView的話,他也不會有東西呀!

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    	Dim dvg As New DataGridView
    	'建立空的DataTable
    	Dim dtResult = New DataTable("SC_1Funs")
    	dtResult.Columns.Add("FUN_ITEM_NAME", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID1", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID2", Type.GetType("System.String"))
    	'新增資料到DataTable之中
    	dtResult.Rows.Add(New String() {"Fun1_name", "Fun1", "Fun1", "Fun1"})
    	dtResult.Rows.Add(New String() {"Fun2_name", "Fun2", "Fun2", "Fun2"})
    	dvg.DataSource = dtResult
    
    	Me.Controls.Add(dvg)
    	dvg.Visible = False
    	Call DcExcel(dvg)
    End Sub
    Public Sub DcExcel(ByVal DGV As DataGridView)
    	'把datagridview中的?据?出到excel
    	Dim wapp As New Microsoft.Office.Interop.Excel.Application
    	Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
    	Dim wbook As Microsoft.Office.Interop.Excel.Workbook
    
    
    
    	wapp.Visible = True
    	wbook = wapp.Workbooks.Add()
    	wsheet = wbook.ActiveSheet
    
    	Dim iX As Integer
    	Dim iY As Integer
    	Dim iC As Integer
    
    	For iC = 0 To DGV.Columns.Count - 1
    		wsheet.Cells(1, iC + 1).Value = DGV.Columns(iC).HeaderText
    		wsheet.Cells(1, iC + 1).Font.Bold = True
    	Next
    
    	wsheet.Rows(2).select()
    	For iX = 0 To DGV.Rows.Count - 2 '有一行是header
    		For iY = 0 To DGV.Columns.Count - 1
    			wsheet.Cells(iX + 2, iY + 1).value = DGV(iY, iX).Value
    		Next
    	Next
    End Sub

    請參考,


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    • 已标记为答案 ckjason 2012年2月17日 1:06
    2012年2月16日 9:44

全部回复

  • Dim dvg As DataGridView
    Call DcExcel(dvg)

    1.您的DataGridView沒有實體化呢!
    2.如果沒有將資料Bind給DataGridView的話,他也不會有東西呀!

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    	Dim dvg As New DataGridView
    	'建立空的DataTable
    	Dim dtResult = New DataTable("SC_1Funs")
    	dtResult.Columns.Add("FUN_ITEM_NAME", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID1", Type.GetType("System.String"))
    	dtResult.Columns.Add("FUN_ITEM_ID2", Type.GetType("System.String"))
    	'新增資料到DataTable之中
    	dtResult.Rows.Add(New String() {"Fun1_name", "Fun1", "Fun1", "Fun1"})
    	dtResult.Rows.Add(New String() {"Fun2_name", "Fun2", "Fun2", "Fun2"})
    	dvg.DataSource = dtResult
    
    	Me.Controls.Add(dvg)
    	dvg.Visible = False
    	Call DcExcel(dvg)
    End Sub
    Public Sub DcExcel(ByVal DGV As DataGridView)
    	'把datagridview中的?据?出到excel
    	Dim wapp As New Microsoft.Office.Interop.Excel.Application
    	Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
    	Dim wbook As Microsoft.Office.Interop.Excel.Workbook
    
    
    
    	wapp.Visible = True
    	wbook = wapp.Workbooks.Add()
    	wsheet = wbook.ActiveSheet
    
    	Dim iX As Integer
    	Dim iY As Integer
    	Dim iC As Integer
    
    	For iC = 0 To DGV.Columns.Count - 1
    		wsheet.Cells(1, iC + 1).Value = DGV.Columns(iC).HeaderText
    		wsheet.Cells(1, iC + 1).Font.Bold = True
    	Next
    
    	wsheet.Rows(2).select()
    	For iX = 0 To DGV.Rows.Count - 2 '有一行是header
    		For iY = 0 To DGV.Columns.Count - 1
    			wsheet.Cells(iX + 2, iY + 1).value = DGV(iY, iX).Value
    		Next
    	Next
    End Sub

    請參考,


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    • 已标记为答案 ckjason 2012年2月17日 1:06
    2012年2月16日 9:44
  • 可能哪句出错了,去掉On Error Resume Next


    http://feiyun0112.cnblogs.com/

    2012年2月16日 13:08
    版主