从gridview导出记录到asp.net中的excel?

时间:2013-01-24 07:40:28

标签: asp.net visual-studio-2010 excel gridview

我想通过仅使用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 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

This博客文章包含用于将数据导出到Excel的vb.net代码。

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

答案 2 :(得分: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

答案 3 :(得分: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()