0

我想通过仅使用 vb.net 作为编程语言将记录从我的 gridview 导出到 excel。我见过很多使用 C# 的代码,但我不是 C# 从业者,我只使用 vb.net。

这里是 ZedBee 爵士

<body>
<form id="form1" runat="server">

<div id= "bg">

<div id = "scroll">

    <asp:GridView ID="tblreport" runat="server" CellPadding="4" ForeColor="#333333" 

        style="z-index: 1; left: -1px; top: 0px; position: absolute; height: 1px; width: 880px" 
        BorderColor="Black">
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>
    </div>

    <asp:Button ID="Button1" runat="server" 
        style="z-index: 1; left: 410px; top: 396px; position: absolute; height: 26px;" 
        Text="Generate" />
    <asp:Button ID="Button2" runat="server" 
        style="z-index: 1; left: 511px; top: 395px; position: absolute; width: 70px" 
        Text="Cancel" />
    <asp:TextBox ID="TextBox1" runat="server" 
        style="z-index: 1; left: 182px; top: 65px; position: absolute"></asp:TextBox>
    <asp:DropDownList ID="dditem" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="TABLE_NAME" 
        DataValueField="TABLE_NAME" 
        style="z-index: 1; left: 83px; top: 32px; position: absolute">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:dbapproveditemConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:dbapproveditemConnectionString.ProviderName %>" 
        SelectCommand="SELECT TABLE_NAME FROM information_schema.COLUMNS WHERE (TABLE_SCHEMA = 'dbitem') AND (ORDINAL_POSITION = 1)">
    </asp:SqlDataSource>
    <asp:Button ID="Button3" runat="server" 
        style="z-index: 1; left: 330px; top: 62px; position: absolute; width: 62px" 
        Text="OK" />
    <asp:DropDownList ID="DropDownList2" runat="server" 

        style="z-index: 1; left: 82px; top: 65px; position: absolute; height: 16px; width: 79px;">
        <asp:ListItem>Status</asp:ListItem>
    </asp:DropDownList>

</div>

</form>

4

4 回答 4

1

尝试这个

     response.ClearHeaders()
    'first let's clean up the response.object
    response.Clear()
    response.Charset = ""
    'set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel"

     strfileName = "InActiveContactOptions"

    'response.AddHeader("Content-Disposition", "inline;filename=ContactOptions.xls")
    response.AddHeader("Content-Disposition", "inline;filename=" + strfileName + ".xls")
    'create a stringwriter
    Dim stringWrite As New System.IO.StringWriter
    'create an htmltextwriter which uses the stringwriter
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    'instantiate a datagrid
    Dim dg As New DataGrid
    'set the datagrid datasource to the dataset passed in
    dg.HeaderStyle.BackColor = Drawing.Color.Gray
    dg.HeaderStyle.ForeColor = Drawing.Color.White
    dg.Caption = _Caption
    dg.CaptionAlign = TableCaptionAlign.Left
    dg.AlternatingItemStyle.BackColor = Drawing.Color.Ivory
    dg.DataSource = ds.Tables(0)
    'bind the datagrid
    dg.DataBind()
    'tell the datagrid to render itself to our htmltextwriter
    dg.RenderControl(htmlWrite)
    'all that's left is to output the html
    response.Write(stringWrite.ToString.Replace("&nbsp;", ""))
    response.End()
于 2013-01-24T08:37:47.587 回答
0
    Response.Clear()
    Response.AddHeader("content-disposition", "attachment;filename=" + "Name of excel" + ".xls")
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/vnd.ms-excel" ' for excel 2003
    'Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ' for excel 2007
    Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWrite)
    Dim gv As System.Web.UI.WebControls.GridView = New System.Web.UI.WebControls.GridView()
    gv.DataSource = dt
    gv.DataBind()

    gv.HeaderRow.Style.Add("background-color", "#FFFFFF")
    gv.HeaderRow.Style.Add("Height", "30px")

    For i As Integer = 0 To gv.HeaderRow.Cells.Count - 1 Step i + 1
        gv.HeaderRow.Cells(i).Style.Add("background-color", "#f5f5f5")
    Next

    Dim i1 As Integer = gv.HeaderRow.Cells.Count - 1
    For i As Integer = 0 To gv.Rows.Count - 1
        Dim row As GridViewRow = gv.Rows(i)
        'Change Color back to white
        row.BackColor = System.Drawing.Color.White
        'Apply text style to each Row
        row.Attributes.Add("class", "textmode")
        'Apply style to Individual Cells of Alternating Row
        If i Mod 2 <> 0 Then
            For i0 As Integer = 0 To i1
                row.Cells(i0).Style.Add("background-color", "#F2F2F2")
            Next
        End If
    Next
    gv.RenderControl(htmlWrite)
    'style to format numbers to string
    Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
    Response.Write(style)
    Response.Output.Write(stringWrite.ToString())
    Response.Flush()
    Response.End()
于 2013-11-29T08:39:31.770 回答
0

你试过这个代码吗?

IT 在 C# 中为我工作,我只是使用在线转换器将其转换为 VB

将此添加到您的导出按钮单击事件中

Dim filename As String = "Test.xls"
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

'Get the H`enter code here`TML for the control.
yourGrid.RenderControl(hw)
'Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & "")

Response.Write(tw.ToString())

原文来源:https ://stackoverflow.com/a/11308445/1196411

于 2013-01-24T07:57:17.757 回答
0

这篇博文有用于将数据导出到 excel 的 vb.net 代码。

可以通过在后面的代码中覆盖 VerifyRenderingInServerForm 来解决该错误。请阅读博客中的评论。

于 2013-01-24T07:46:41.700 回答