Visual Basic:从Gridview导出到Excel时缺少列标题 [英] Visual Basic: Missing column headers when exporting from Gridview to Excel

查看:109
本文介绍了Visual Basic:从Gridview导出到Excel时缺少列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我是Visual Basic 2008的新手,并尝试将gridview导出为ex​​cel。我能够将网格的数据内容导出为ex​​cel,除了列(标题),即ID,名称等





你能帮我解决一下我错过的东西吗?





我正在使用的导出按钮的代码是如下:



Hello Everyone,

I am new to Visual basic 2008 and trying to export gridview to excel. I am able to export the data content of the grid to excel, apart from the columns (headings) i.e ID, name etc


Could you please help me what I am missing?


The code for the "Export" button that I am using is as follows:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer

'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)

With ExcelSheet



For i = 1 To Me.DataGrid.RowCount
.cells(i, 1) = Me.DataGrid.Rows(i - 1).Cells("IDDataGridViewTextBoxColumn").Value
For j = 1 To DataGrid.Columns.Count - 1
.cells(i, j + 1) = DataGrid.Rows(i - 1).Cells(j).Value

Next
Next
End With
ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing


End Sub







谢谢你提前你的帮助: - )




Thanks for your help in advance :-)

推荐答案

你好,



有一种更简单的阅读方式列名:



Hello,

There is a much easier way to read the columns names:

For columnIndex As Integer = 0 To Me.DataGrid.ColumnCount - 1
    Dim columnName = Me.DataGrid.Columns(columnIndex).Name
    .cells(columnIndex + 1, 1) = columnName
Next







Valery。




Valery.


有效。非常感谢@Valery
It worked. Thanks a lot @Valery


这篇关于Visual Basic:从Gridview导出到Excel时缺少列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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