在aspx页面的代码后面,我有一个Datatable:
Dim people As DataTable = New DataTable()
people.Columns.Add("ID", System.Type.GetType("System.Int32"))
people.Columns.Add("FirstName", System.Type.GetType("System.String"))
people.Columns.Add("LastName", System.Type.GetType("System.String"))
people.Rows.Add(10, "Merci", "Beaucoup")
这就是GridViewData :(不是asp:GridView)
Dim gvPeople As System.Windows.Forms.DataGridView = New DataGridView()
gvPeople.AutoGenerateColumns = False
'Set Columns Count
gvPeople.ColumnCount = 3
'Add Columns
gvPeople.Columns(0).Name = "ID"
gvPeople.Columns(0).HeaderText = "ID"
gvPeople.Columns(0).DataPropertyName = "ID"
gvPeople.Columns(1).Name = "FirstName"
gvPeople.Columns(1).HeaderText = "FirstName"
gvPeople.Columns(1).DataPropertyName = "FirstName"
gvPeople.Columns(2).Name = "LastName"
gvPeople.Columns(2).HeaderText = "LastName"
gvPeople.Columns(2).DataPropertyName = "LastName"
这里我将DataGridView的数据源设置为DataTable:
gvPeople.DataSource = people
当我使用Gembox.Spreadsheet
将DataGridView导入Excel工作表时,它只向我显示没有数据的DataGridView的headerText。
这是导入代码:
DataGridViewConverter.ImportFromDataGridView(ws, gvPeople, New ImportFromDataGridViewOptions() With _
{
.ColumnHeaders = True,
.StartRow = 8,
.StartColumn = 0
})
我尝试了多项内容,例如:
将.DataMember
设置为DataTable名称:gvPeople.DataMember=people.TableName
Refresh()
后, Update()
或.Datasource
DataGridView。
注意:这不是asp:GridView
,而是DataGridView
,并且没有DataBind()
方法。
答案 0 :(得分:3)
如果使用asp:GridView
,只需使用databind
功能
gvPeople.DataSource = people
gvPeople.Databind()
注意:建议不要在Web项目中使用datagridView,此控件是为winforms应用程序设计的。
如果使用windows.Forms.DataGridView
DataGridView
添加到表单控件集合才能执行其布局
Me.Controls.Add(gvPeople)
但是在使用Web项目(获取错误)
时无法做到这一点所以你必须做一些解决方法
首先,我创建了一个将数据流转换为字符串数组
的函数Public Function ToStringArray(ByVal dRow As DataRow) As String()
Dim lst As New List(Of Object)
lst.AddRange(dRow.ItemArray)
Return lst.Select(Function(x) x.ToString).ToArray()
End Function
然后我使用了以下代码
这是您的代码:
Dim people As DataTable = New DataTable("people")
people.Columns.Add("ID", System.Type.GetType("System.Int32"))
people.Columns.Add("FirstName", System.Type.GetType("System.String"))
people.Columns.Add("LastName", System.Type.GetType("System.String"))
people.Rows.Add(10, "Merci", "Beaucoup")
Dim gvPeople As System.Windows.Forms.DataGridView = New System.Windows.Forms.DataGridView()
gvPeople.AutoGenerateColumns = False
''Set Columns Count
gvPeople.ColumnCount = 3
''Add Columns
gvPeople.Columns(0).Name = "ID"
gvPeople.Columns(0).HeaderText = "ID"
gvPeople.Columns(0).DataPropertyName = "ID"
gvPeople.Columns(1).Name = "FirstName"
gvPeople.Columns(1).HeaderText = "FirstName"
gvPeople.Columns(1).DataPropertyName = "FirstName"
gvPeople.Columns(2).Name = "LastName"
gvPeople.Columns(2).HeaderText = "LastName"
gvPeople.Columns(2).DataPropertyName = "LastName"
这是我添加的代码
For Each drow As DataRow In people.Rows
gvPeople.Rows.Add(ToStringArray(drow))
Next
gvPeople.RowCount = people.Rows.Count
gvPeople.Refresh()
答案 1 :(得分:2)
您应该重新考虑这种方法(在ASP.NET应用程序中使用DataGridView)。
如果只想将GridView控件中的数据插入Excel文件,请使用DataTable。
正如您在评论中提到的,您希望自定义要导出的列和列的顺序。在这种情况下,您只需要调整DataTable本身,例如尝试以下方法:
' Sample DataTable.
Dim people As New DataTable()
people.Columns.Add("ID", GetType(Integer))
people.Columns.Add("FirstName", GetType(String))
people.Columns.Add("LastName", GetType(String))
people.Rows.Add(1, "John", "Doe")
people.Rows.Add(2, "Jane", "Doe")
people.Rows.Add(10, "Merci", "Beaucoup")
' Remove "ID" column.
people.Columns.Remove("ID")
' Set "LastName" column on first place.
people.Columns("LastName").SetOrdinal(0)
' Create Excel file.
Dim ef As New ExcelFile()
Dim ws As ExcelWorksheet = ef.Worksheets.Add("Sheet1")
' Insert DataTable into Excel file.
Dim op As New InsertDataTableOptions()
op.ColumnHeaders = True
ws.InsertDataTable(people, op)
' Save Excel file.
ef.Save("Sample.xlsx")
我希望这会有所帮助 另外,如果您对此感兴趣的话,您将如何将数据与GridView控件中的样式和格式一起插入到带有GemBox.Spreadsheet的Excel文件中: https://www.gemboxsoftware.com/support-center/kb/articles/export-gridview-and-or-datagrid-control-to-excel-file
答案 2 :(得分:0)
您可以使用asp gridview代替在asp.net应用程序中使用datagrid 这可以通过以下链接
https://www.gemboxsoftware.com/spreadsheet/examples/asp-net-excel-export/5101