Asp.net 使用VB.NET将Gridview导出到Excel
我有一个带有gridview的页面,它使用的是母版页。导出按钮位于母版页上,我只需要从中导出实际页面上的gridview数据 gridview上有一些隐藏列,这些列不能包含在导出到Excel的数据中 如何在不获取gridview周围(即页面本身)的任何其他格式的情况下做到这一点Asp.net 使用VB.NET将Gridview导出到Excel,asp.net,vb.net,excel,gridview,Asp.net,Vb.net,Excel,Gridview,我有一个带有gridview的页面,它使用的是母版页。导出按钮位于母版页上,我只需要从中导出实际页面上的gridview数据 gridview上有一些隐藏列,这些列不能包含在导出到Excel的数据中 如何在不获取gridview周围(即页面本身)的任何其他格式的情况下做到这一点 我基本上是在使用这个URL上的代码:我转换为VB.NET,但它似乎正在将网格上的所有数据导出到Excel,包括隐藏的列。我已经搜索了一段时间的解决方案。我以前看过你的代码,但我似乎无法让它工作——但这是因为缺乏知识。因此
我基本上是在使用这个URL上的代码:我转换为VB.NET,但它似乎正在将网格上的所有数据导出到Excel,包括隐藏的列。我已经搜索了一段时间的解决方案。我以前看过你的代码,但我似乎无法让它工作——但这是因为缺乏知识。因此,对于其他可能遇到同样问题的人,这是我的方案和解决方案,请注意,这是在VB中,可以使用任何在线转换工具进行转换: 场景:我有一个带有导出按钮的母版页,该按钮使用母版页从页面上的gridview导出数据 在Gridview页面的Page_Load事件中,我使用了以下代码: 然后我创建了public sub: 然后添加以下内容:
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
不幸的是,这意味着它需要进入您希望导出gridview的每个页面,但它为我完成了这项工作。这仅导出gridview中的数据
我希望这能帮助那些有同样问题的人。我实际上找到了另一种方法,它不影响页面本身,只需要在母版页上完成 公共覆盖子验证RenderingInServerFormControl,因为不需要控件: '在母版页中导出单击事件:
Public Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButExportExcel.Click
'*** take the paging and sorting out of the excel spreadsheet
Try
Dim sgv As GridView = CType(ContentPlaceHolder_body.FindControl("SummaryGridView"), GridView)
If (sgv IsNot Nothing) Then
sgv.AllowPaging = False
sgv.AllowSorting = False
sgv.DataBind()
End If
Dim sExportFileName As String = ""
sExportFileName = Path.GetFileName(Request.PhysicalPath)
sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"
Export2(sExportFileName, sgv)
Catch ex As Exception
End Try
End Sub
'Export Sub
Public Sub Export2(ByVal fileName As String, ByVal gv As GridView)
Dim sExportFileName As String = ""
Dim sStringToReplace As String = ""
gv.HeaderStyle.ForeColor = Drawing.Color.Black
gv.HeaderStyle.BackColor = Drawing.Color.White
gv.RowStyle.BackColor = Drawing.Color.White
gv.HeaderStyle.Font.Bold = True
gv.HeaderStyle.Font.Size = 10
sExportFileName = Path.GetFileName(Request.PhysicalPath)
sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"
Dim attachment As String = "attachment; filename=" & sExportFileName
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("content-disposition", attachment)
HttpContext.Current.Response.ContentType = "application/ms-excel"
Dim stw As New StringWriter()
Dim htextw As New HtmlTextWriter(stw)
Dim parent As Control = gv.Parent
Dim GridIndex As Integer = 0
If parent IsNot Nothing Then
GridIndex = parent.Controls.IndexOf(gv)
parent.Controls.Remove(gv)
End If
gv.RenderControl(htextw)
If parent IsNot Nothing Then
parent.Controls.AddAt(GridIndex, gv)
End If
'gv.RenderControl(htextw)
HttpContext.Current.Response.Write(stw.ToString())
Dim fi As New FileInfo(Server.MapPath("../JumpStart.css"))
Dim sb As New System.Text.StringBuilder()
Dim sr As StreamReader = fi.OpenText()
'sStringToReplace = "class=""generalheader"""
While sr.Peek() >= 0
sb.Append(sr.ReadLine())
End While
sr.Close()
Dim outputHtml = "<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>"
Response.Write(outputHtml.ToString)
'Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>")
stw = Nothing
htextw = Nothing
Response.Flush()
Response.[End]()
End Sub
2019年,该公司的职能部门和职能部门可能会发生冲突 PARA VB.NET 专用子代交换机
Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
excel.Application.Workbooks.Add(True)
Dim ColumnIndex As Integer = 0
For Each col As DataGridViewColumn In GrillaReporte.Columns
ColumnIndex += 1
excel.Cells(1, ColumnIndex) = col.Name
Next
Dim rowIndex As Integer = 0
For Each row As DataGridViewRow In GrillaReporte.Rows
rowIndex += 1
ColumnIndex = 0
For Each col As DataGridViewColumn In GrillaReporte.Columns
ColumnIndex += 1
excel.Cells(rowIndex + 1, ColumnIndex) = row.Cells(col.Name).Value
Next
Next
excel.Visible = True
End Sub
C段
私有无效GenerarExcel
{
请用英语写你的答案。
Public Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButExportExcel.Click
'*** take the paging and sorting out of the excel spreadsheet
Try
Dim sgv As GridView = CType(ContentPlaceHolder_body.FindControl("SummaryGridView"), GridView)
If (sgv IsNot Nothing) Then
sgv.AllowPaging = False
sgv.AllowSorting = False
sgv.DataBind()
End If
Dim sExportFileName As String = ""
sExportFileName = Path.GetFileName(Request.PhysicalPath)
sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"
Export2(sExportFileName, sgv)
Catch ex As Exception
End Try
End Sub
'Export Sub
Public Sub Export2(ByVal fileName As String, ByVal gv As GridView)
Dim sExportFileName As String = ""
Dim sStringToReplace As String = ""
gv.HeaderStyle.ForeColor = Drawing.Color.Black
gv.HeaderStyle.BackColor = Drawing.Color.White
gv.RowStyle.BackColor = Drawing.Color.White
gv.HeaderStyle.Font.Bold = True
gv.HeaderStyle.Font.Size = 10
sExportFileName = Path.GetFileName(Request.PhysicalPath)
sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"
Dim attachment As String = "attachment; filename=" & sExportFileName
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("content-disposition", attachment)
HttpContext.Current.Response.ContentType = "application/ms-excel"
Dim stw As New StringWriter()
Dim htextw As New HtmlTextWriter(stw)
Dim parent As Control = gv.Parent
Dim GridIndex As Integer = 0
If parent IsNot Nothing Then
GridIndex = parent.Controls.IndexOf(gv)
parent.Controls.Remove(gv)
End If
gv.RenderControl(htextw)
If parent IsNot Nothing Then
parent.Controls.AddAt(GridIndex, gv)
End If
'gv.RenderControl(htextw)
HttpContext.Current.Response.Write(stw.ToString())
Dim fi As New FileInfo(Server.MapPath("../JumpStart.css"))
Dim sb As New System.Text.StringBuilder()
Dim sr As StreamReader = fi.OpenText()
'sStringToReplace = "class=""generalheader"""
While sr.Peek() >= 0
sb.Append(sr.ReadLine())
End While
sr.Close()
Dim outputHtml = "<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>"
Response.Write(outputHtml.ToString)
'Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>")
stw = Nothing
htextw = Nothing
Response.Flush()
Response.[End]()
End Sub
Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
excel.Application.Workbooks.Add(True)
Dim ColumnIndex As Integer = 0
For Each col As DataGridViewColumn In GrillaReporte.Columns
ColumnIndex += 1
excel.Cells(1, ColumnIndex) = col.Name
Next
Dim rowIndex As Integer = 0
For Each row As DataGridViewRow In GrillaReporte.Rows
rowIndex += 1
ColumnIndex = 0
For Each col As DataGridViewColumn In GrillaReporte.Columns
ColumnIndex += 1
excel.Cells(rowIndex + 1, ColumnIndex) = row.Cells(col.Name).Value
Next
Next
excel.Visible = True
End Sub
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
int ColumnIndex = 0;
foreach (DataGridViewColumn col in GrillaArticulos.Columns)
{
ColumnIndex++;
excel.Cells[1, ColumnIndex] = col.Name;
}
int rowIndex = 0;
foreach (DataGridViewRow row in GrillaArticulos.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataGridViewColumn col in GrillaArticulos.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value;
}
}
excel.Visible = true;
}