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

尝试以下代码

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 回答
0

永远不要让网格视图可见错误,

于 2012-08-24T06:24:24.043 回答