none
vb2008怎样将datagridview的内容输出到excel? RRS feed

  • 问题

  • 我参考的是vs2008里面自带的一个试例。代码如下:

    Imports Microsoft.Office.Interop
    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SaveFileDialog1.Filter = "excel2007(*.xls)|*.xlsx"
            SaveFileDialog1.Title = "保存为excel文件"
            SaveFileDialog1.InitialDirectory = "d:"
            If SaveFileDialog1.ShowDialog() = DialogResult.OK And SaveFileDialog1.FileName <> "" Then
                PopulateExcelWorkbook(SaveFileDialog1.FileName, False)
            End If
        End Sub
        Function StartExcel(Optional ByVal IsVisible As Boolean = True) As Excel.Application
            Dim objExcel As New Excel.Application
            objExcel.Visible = IsVisible
            Return objExcel
        End Function
        Sub ForceExcelToQuit(ByVal objExcel As Excel.Application)
            Try
                objExcel.Quit()
            Catch ex As Exception
            End Try
        End Sub
        Sub datagridviewToExcelSheet(ByVal dt As DataGridView, ByVal objSheet As Excel.Worksheet, ByVal nStartRow As Integer, ByVal nStartCol As Integer)
            Dim nRow As Integer, nCol As Integer
            For nRow = 0 To dt.Rows.Count - 1
                For nCol = 0 To dt.Columns.Count - 1
                    objSheet.Cells(nStartRow + nRow, nStartCol + nCol) = dt(nRow, nCol).Value
                Next nCol
            Next nRow
        End Sub
        Function PopulateExcelWorkbook(ByVal strSaveFilename As String, ByVal blnIsVisible As Boolean) As DataGridView
            Dim dt As DataGridView
            Dim objExcel As Excel.Application, objWorkbook As Excel.Workbook
            Dim objSheet As Excel.Worksheet
            Dim strFileName As String
            dt = DataGridView1
            '
            '启动 Excel 并通过模板新建一个工作簿
            objExcel = StartExcel(blnIsVisible)
            Try
                strFileName = "d:\123.xls"
                objWorkbook = objExcel.Workbooks.Add(strFileName)
                objSheet = objWorkbook.Sheets("Sheet1")
                '
                '将数据表插入到 Excel 电子表格中
                datagridviewToExcelSheet(dt, objSheet, 2, 1)
                objWorkbook.Saved = True '版主,是放在这儿吗,似乎还是不行!
                '如果可见则退出,以使用户可以看到,否则保存并退出
                If blnIsVisible = False Then
                    objWorkbook.SaveAs(strSaveFilename, Excel.XlFileFormat.xlWorkbookDefault)
                    objWorkbook.Close(False)
                    objExcel.Quit()
                End If
            Catch ex As Exception
                If blnIsVisible Then MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "Error populating workbook")
                ForceExcelToQuit(objExcel)
            End Try
            Return dt
        End Function
    End Class

     

     我用savefiledialog对话框,调用  PopulateExcelWorkbook函数,strSaveFilename用的是savefiledialog.filename,程序执行后,总是提示是否保存“123X.xls”文件?而在savefiledialog对话框选择的目录下没有任何文件产生。

    希望版主能够给予答复!最好能够帮我修改一下代码,谢谢!

    (重新修改过,还是不行,希望版主指教!谢谢!)

    2008年11月13日 2:26

答案

全部回复