2

コードで多くのことを試しましたが、以前に使用したのと同じコードが現在のプロジェクトでは機能しません。データベースからデータを取得できます。変更するvisible=trueと、データもグリッド ビューに表示されます。

コードは次のとおりです。

aspx ページ コード

<asp:GridView ID="gvExport" runat="server" Visible="false"
                            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                            GridLines="None">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
        <asp:BoundField DataField="artID" HeaderText="ID" />
        <asp:BoundField DataField="artName" HeaderText="Name" />
        <asp:BoundField DataField="artType" HeaderText="Category" />
        <asp:BoundField DataField="artWork" HeaderText="Work Type" />
        <asp:BoundField DataField="artCont1" HeaderText="Contact Person (I)" />
        <asp:BoundField DataField="artMob1" HeaderText="Mobile" />
        <asp:BoundField DataField="artCont2" HeaderText="Contact person (II)" />
        <asp:BoundField DataField="artMob2" HeaderText="Mobile" />
        <asp:BoundField DataField="artPhone" HeaderText="Office Phone" />
        <asp:BoundField DataField="artEmail" HeaderText="Email ID" />
        <asp:BoundField DataField="artStreet" HeaderText="Street" />
        <asp:BoundField DataField="artCity" HeaderText="City" />
        <asp:BoundField DataField="artState" HeaderText="State" />
        <asp:BoundField DataField="artCountry" HeaderText="Country" />
    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

以下は裏ページのコードです

DataBaseConnection.dataBase dConnect = new dataBase();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
static string exceprionString = "", artType = "", searchCity = "", searchType = "",     searchCont = "", searchSugType = "", searchSugCIty = "", fileName = "";
static int i = 0, j = 0, artID = 0;
static List<string> listArt = new List<string>();


 /*Export User Artist File As MS Excel Format*/
protected void btExport_Click(object sender, EventArgs e)
{
    if (((string)Session["userRole"]).Equals("ADMIN") || ((string)Session["userRole"]).Equals("MANAGER"))
    {
            exportFile("artist");
    }
    else
        SMS("User Doed Not Rights To Export File");
}

private void exportFile(string expportType)
{
    if (((string)Session["userRole"]).Equals("ADMIN") || ((string)Session["userRole"]).Equals("MANAGER"))
    {
            ds.Clear();
            if (expportType.Equals("artist"))
            {
                if (((string)Session["userID"]).Equals("admin"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "admin");
                else if (((string)Session["userRole"]).Equals("MANAGER"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "manager");
                else if (((string)Session["userRole"]).Equals("MANAGER"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "select");
                fileName = (string)Session["userID"];
            }

            exceprionString = "";
            exceprionString = dConnect.exceptionMessage();

            if (ds.Tables[0].Rows.Count > 0 && exceprionString.Equals(""))
            {
                if (expportType.Equals("artist"))
                {
                    gvExport.DataSource = ds.Tables[0];
                    gvExport.DataBind();

                    ExportInExcel(gvExport);
                }
            }
            else
                SMS("File Not Export Due To Data Not Found");
    }
    else
        SMS("user Does Not Rights To Export File");
}

private void ExportInExcel(GridView grid)
{
    HtmlForm form = new HtmlForm();
    string attachment = "attachment; filename=" + fileName + ".xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    form.Controls.Add(grid);
    this.Controls.Add(form);
    form.RenderControl(htextw);
    Response.Write(stw.ToString());
    //Response.Output.Write(stw.ToString());//
    //Response.Flush();//
    //Response.End();

    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
4

2 に答える 2

0

グリッド ビューを false にしないでください。

于 2012-08-24T06:24:24.043 に答える
0

次のコードを試してください

public void CreateExcel(GridView dgDetails, string fileName)
          {
              dgDetails.AllowPaging = false;
              dgDetails.DataBind();
              Response.ClearContent();
              Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", ""+fileName+".xls"));
              Response.Charset = "";
              Response.ContentType = "application/ms-excel";
              StringWriter sw = new StringWriter();
              HtmlTextWriter htw = new HtmlTextWriter(sw);
              dgDetails.RenderControl(htw);
              Response.End();         
          }
于 2012-07-26T05:46:05.110 に答える