0

プログラミング言語として vb.net のみを使用して、グリッドビューから Excel にレコードをエクスポートしたいと考えています。私は C# を使用した多くのコードを見てきましたが、私は C# の専門家ではなく、vb.net しか使用していません。

こちらがゼドビー卿です

<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

このブログ投稿には、データを Excel にエクスポートするための vb.net コードが含まれています。

このエラーは、コード ビハインドで VerifyRenderingInServerForm をオーバーライドすることで解決できます。ブログのコメントを読んでください。

于 2013-01-24T07:46:41.700 に答える
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 に答える