繁体   English   中英

如何将gridview转换为excel?

[英]How to convert gridview to excel?

我试图将gridview转换为excel .xls但它会抛出错误,当我单击确定然后它转换但整个页面出现在excel中。 我尝试了每种可能的内容类型,我已经超越了2010。

错误:

您尝试转换的文件格式与文件扩展名指定的格式不同

码:

 protected void btnTransactionDetails_Click(object sender, EventArgs e)
    {
        try
        {
            LabelResult.Text = "";
            //GetReport();
            int BusID= Convert.ToInt32(DropDownListBuses.SelectedValue);
            int AccountID= Convert.ToInt32(DropDownList1.SelectedValue);
            DateTime FromDate = Convert.ToDateTime( FromDateTextBox.Text);
            DateTime ToDate = Convert.ToDateTime( ToDateTextBox.Text);
            DataTable dt= new DataTable();
            dt= Activities.GetLedger(AccountID, BusID, FromDate, ToDate);
            GridViewLedger.DataSource=  dt;
            GridViewLedger.DataBind();
            ViewState["Ledger"]= dt;

        }
        catch (Exception ex)
        {
            LabelResult.Text = ex.Message;
        }

    }

 protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=LedgerReport_" + FromDateTextBox.Text + " To " + ToDateTextBox.Text + ".xls");
            Response.ContentType = "application/vnd.xlsx";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            htmlWrite.Write("<table><tr><td colspan='4'><center>Report Date : " + FromDateTextBox.Text + " To " + FromDateTextBox.Text + "</center></td></tr></table>");
            GridViewLedger.AllowPaging = false;
            GridViewLedger.AllowSorting = false;
            //  showAttendance();
            GridViewLedger.DataSource = (DataSet)ViewState["Ledger"];
            GridViewLedger.DataBind();
            for (int i = 0; i <= GridViewLedger.Columns.Count - 1; i++)
            {

                GridViewLedger.HeaderRow.Cells[i].Style.Add("background-color", "#2FA4E7");
                GridViewLedger.HeaderRow.Cells[i].Style.Add("color", "#FFFFFF");

            }


            for (int i = 0; i < GridViewLedger.Rows.Count; i++)
            {
                GridViewRow row = GridViewLedger.Rows[i];

                row.Cells[3].Style.Add("background-color", "#73a839");
                row.Cells[3].Style.Add("color", "#FFFFFF");

                row.Cells[4].Style.Add("background-color", "#DA272D");
                row.Cells[4].Style.Add("color", "#FFFFFF");

            }

            GridViewLedger.RenderControl(htmlWrite);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
        catch (Exception x)
        {
          //  ResultLabel.ResultLabelAttributes(x.Message, ProjectUserControls.Enums.ResultLabel_Color.Red);
        }
    }

改变这一行

(数据集)的ViewState [ “总帐”]; To(DataTable)ViewState [“Ledger”];

您正在尝试将TypeCast DataTable添加到DataSet,您将获得以下异常

无法将类型为“System.Data.DataTable”的对象强制转换为“System.Data.DataSet”。

因为ViewState [“Ledger”]包含数据表(您已为其分配了数据表)


我创建了一个示例项目,它与您的代码完全相同。 它的工作正常我请检查

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            // Here we add five DataRows.
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
            GridView1.DataSource = table;
            GridView1.DataBind();
            ViewState["Ledger"] = table;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=LedgerReport.xls");
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        htmlWrite.Write("<table><tr><td colspan='4'><center>Report Date </center></td></tr></table>");
        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        GridView1.DataSource = (DataTable)ViewState["Ledger"];
        GridView1.DataBind();
        for (int i = 0; i <= GridView1.Columns.Count - 1; i++)
        {
            GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#2FA4E7");
            GridView1.HeaderRow.Cells[i].Style.Add("color", "#FFFFFF");

        }

        GridView1.RenderControl(htmlWrite);
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM