繁体   English   中英

用C#导出到Excel

[英]Exporting to Excel in C#

我使用以下代码导出到excel,一切正常,但是一列的文本框带有标签,只有该列的数据没有显示。 它在excel中显示为空。 我正在使用asp:GridView。

protected void exportExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "Booking Confirmation " + DateTime.Now + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
            BookingConfirmationList.GridLines = GridLines.Both;
            BookingConfirmationList.HeaderStyle.Font.Bold = true;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlProjectId").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlBookNo").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlEngineerBooked").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlDateBooked").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlChemicals").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlQuantityBooked").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlBatchNo").Visible = false;
            BookingConfirmationList.HeaderRow.Cells[0].FindControl("ddlOperatorName").Visible = false;
            //BookingConfirmationList.HeaderRow.Cells[0].FindControl("chkAllRows").Visible = false;
            //BookingConfirmationList.FindControl("chkRow").Visible = false;



            for (int i = 0; i < BookingConfirmationList.Rows.Count; i++)
            {
                GridViewRow row = BookingConfirmationList.Rows[i];
                GridViewRow rowH = BookingConfirmationList.HeaderRow;
                rowH.Cells[0].Visible = false;
                row.Cells[0].Visible = false;
                row.Cells[6].Visible = true;

            }

            BookingConfirmationList.RowStyle.Height = 25;
            BookingConfirmationList.RowStyle.HorizontalAlign = HorizontalAlign.Left;

            BookingConfirmationList.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.Flush();
            Response.End();

        }

ASP网格视图

参考图像Excel

为了获得您想要的第六列的文本,您可以做的一件事是根据文本框的文本在循环中为每一行设置文本。

类似于以下内容:

row.Cells[6].Text = textboxValue.Text + " " + textboxUnit.Text;

暂无
暂无

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

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