- GridView 导出Excel(如何在excel中显示'0001'而不是'1')
- 在RowBound中添加
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat: @");
- }
- 以上可以解决(如何在excel中显示'0001'而不是'1')的问题
- 设公共变量 bool flag = false;
- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
- {
- if (flag == true)
- {
- e.Row.Cells[this.GridView1.Columns.Count - 2].Visible = false;
- }
- //可以解决导出时是否隐藏哪列的问题
- }
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- flag = true;//可以解决导出时是否隐藏哪列的问题
- Response.Clear();
- HttpContext.Current.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + "RedemptionReport.xls");
- Response.Charset = "UTF-8";
- Response.ContentEncoding = System.Text.Encoding.Default;
- Response.ContentType = "application/ms-excel";
- StringWriter stringWrite=new StringWriter();
- HtmlTextWriter htmlWrite=new HtmlTextWriter(stringWrite);
- GridView1.AllowPaging=false;
- gridviewshow(str); //重新绑定
- GridView1.RenderControl(htmlWrite);
- Response.Write(stringWrite.ToString());
- Response.End();
- flag = false; //可以解决导出时是否隐藏哪列的问题
- GridView1.AllowPaging=true;
- gridviewshow(str); //重新绑定
- }
- public override void VerifyRenderingInServerForm(Control control)
- {
- // Confirms that an HtmlForm control is rendered for
- }
- 在导出时改变Header
- 在RowBound中
- if (e.Row.RowType == DataControlRowType.Header)
- {
- //***********************************************************
- GridViewRow row3 = new GridViewRow(-1, -1, DataControlRowType.Header,
- DataControlRowState.Normal);
- //for (int j = 0; j < 7; j++)
- //{
- TableCell cell30 = new TableCell();
- cell30.Text = "<b>Report:</b>";
- row3.Cells.AddAt(0, cell30);
- TableCell cell31 = new TableCell();
- cell31.Text = "New Loans By Deal";
- row3.Cells.AddAt(1, cell31);
- TableCell cell32 = new TableCell();
- cell32.Attributes["align"] = "right";
- cell32.Text = "<font style=/"text-align:right; font-
- weight:bold;/">Date/Time:</font>";
- row3.Cells.AddAt(2, cell32);
- TableCell cell33 = new TableCell();
- cell33.Text = DateTime.Now.ToString("yyyy-MM-dd") + " " +
- DateTime.Now.ToString(@"HH:mm:ss");
- row3.Cells.AddAt(3, cell33);
- TableCell cell34 = new TableCell();
- cell34.Text = "";
- row3.Cells.AddAt(4, cell34);
- TableCell cell35 = new TableCell();
- cell35.Text = "";
- row3.Cells.AddAt(5, cell35);
- TableCell cell36 = new TableCell();
- cell36.Text = "";
- row3.Cells.AddAt(6, cell36);
- GridView2.Controls[0].Controls.AddAt(0, row3);
- }
GridView 导出Excel(如何在excel中显示'0001'而不是'1')
最新推荐文章于 2022-10-25 16:59:04 发布