使用Vb.net + Windows应用程序导出到Excel [英] Export to Excel using Vb.net + Windows app

查看:110
本文介绍了使用Vb.net + Windows应用程序导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  Private Sub ExportGridToExcel()
Dim Excel As Object = CreateObject(Excel.Application)
如果Excel没有那么
MsgBox(看起来Excel没有安装在这台机器上,这个操作需要MS Excel安装在这台机器上。, MsgBoxStyle.Critical)
返回
结束如果
'使Excel可见
Excel.Visible = True
'初始化Excel工作表
使用Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'如何将整个网格绑定到excel而不循环...

End With
Excel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
Excel = Nothing
MsgBox(Export to Excel完成,MsgBoxStyle.Information)
End Sub

如何将整个网格绑定到excel而不循环..

解决方案

我假设您的网格意味着一个datagridview?我认为你不能将你的数据绑定到excel。我前段时间写了这个将datagridview导出为ex​​cel:

  private _dt as new Datatable 

Dim strTempFile As String = My.Computer.FileSystem.GetTempFileName()
_dt = dgvResults.DataSource
Dim strLine As New StringBuilder()

For c As Integer = 0 To _dt.Columns.Count - 1
strLine.Append(_dt.Columns(c).ColumnName.ToString&,)
Next
My.Computer.FileSystem.WriteAllText(strTempFile ,strLine.ToString.TrimEnd(,)& vbCrLf,True)


For r As Integer = 0 To _dt.Rows.Count - 1
strLine = New StringBuilder()
For c As Integer = 0 To _dt.Columns.Count - 1
strLine.Append(_dt.Rows(r).Item(c).ToString&,)
Next
My.Computer.FileSystem.WriteAllText(strTempFile,strLine.ToString.TrimEnd(,)& vbCrLf,True)
下一个

过程。开始(excel,strTempFile)


Simple code :

Private Sub ExportGridToExcel()
        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical)
            Return
        End If
        'Make Excel visible
        Excel.Visible = True
        'Initialize Excel Sheet
        With Excel
            .SheetsInNewWorkbook = 1
            .Workbooks.Add()
            .Worksheets(1).Select()

            'How to bind entire grid into excel without looping...

        End With
        Excel.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
        Excel = Nothing
        MsgBox("Export to Excel Complete", MsgBoxStyle.Information)
    End Sub

How to bind entire grid in to excel without looping..

解决方案

I assume by the grid you mean a datagridview? I don't think you will be able to bind your data to excel. I wrote this some time ago to export a datagridview to excel:

private _dt as new Datatable

Dim strTempFile As String = My.Computer.FileSystem.GetTempFileName()
_dt = dgvResults.DataSource
Dim strLine As New StringBuilder("")

For c As Integer = 0 To _dt.Columns.Count - 1
    strLine.Append(_dt.Columns(c).ColumnName.ToString & ",")
Next
My.Computer.FileSystem.WriteAllText(strTempFile, strLine.ToString.TrimEnd(",") & vbCrLf, True)


For r As Integer = 0 To _dt.Rows.Count - 1
    strLine = New StringBuilder("")
    For c As Integer = 0 To _dt.Columns.Count - 1
        strLine.Append(_dt.Rows(r).Item(c).ToString & ",")
    Next
    My.Computer.FileSystem.WriteAllText(strTempFile, strLine.ToString.TrimEnd(",") & vbCrLf, True)
Next

Process.Start("excel", strTempFile)

这篇关于使用Vb.net + Windows应用程序导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆