GridView 导出Excel(如何在excel中显示'0001'而不是'1')

来源:互联网 发布:stc单片机选型 编辑:程序博客网 时间:2024/05/30 23:30
  1. GridView 导出Excel(如何在excel中显示'0001'而不是'1')
  2. 在RowBound中添加
  3.   if (e.Row.RowType == DataControlRowType.DataRow)
  4.         {
  5.             e.Row.Cells[1].Attributes.Add("style""vnd.ms-excel.numberformat: @");
  6.            
  7.         }
  8. 以上可以解决(如何在excel中显示'0001'而不是'1')的问题
  9. 设公共变量 bool flag = false;
  10.  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  11.     {
  12.        
  13.         if (flag == true)
  14.         {
  15.             e.Row.Cells[this.GridView1.Columns.Count - 2].Visible = false;
  16.            
  17.         }
  18. //可以解决导出时是否隐藏哪列的问题
  19.     }
  20.  protected void LinkButton1_Click(object sender, EventArgs e)
  21.     {
  22.         
  23.         flag = true;//可以解决导出时是否隐藏哪列的问题
  24.         Response.Clear();
  25.         HttpContext.Current.Response.AppendHeader("Content-Disposition"
  26. "attachment;filename=" + "RedemptionReport.xls");
  27.         Response.Charset = "UTF-8";
  28.         Response.ContentEncoding = System.Text.Encoding.Default;
  29.         Response.ContentType = "application/ms-excel"
  30.         StringWriter stringWrite=new StringWriter(); 
  31.         HtmlTextWriter htmlWrite=new HtmlTextWriter(stringWrite); 
  32.         GridView1.AllowPaging=false;
  33.         gridviewshow(str);  //重新绑定
  34.         
  35.         GridView1.RenderControl(htmlWrite); 
  36.         Response.Write(stringWrite.ToString());
  37.         Response.End();
  38.         flag = false;  //可以解决导出时是否隐藏哪列的问题
  39.         GridView1.AllowPaging=true;
  40.         gridviewshow(str); //重新绑定
  41.        
  42.     }
  43.     public override void VerifyRenderingInServerForm(Control control)
  44.     {
  45.         // Confirms that an HtmlForm control is rendered for
  46.     } 
  47. 在导出时改变Header
  48. 在RowBound中
  49.  if (e.Row.RowType == DataControlRowType.Header)
  50.         {
  51.             //***********************************************************
  52.             GridViewRow row3 = new GridViewRow(-1, -1, DataControlRowType.Header, 
  53. DataControlRowState.Normal);
  54.             //for (int j = 0; j < 7; j++)
  55.             //{
  56.             TableCell cell30 = new TableCell();
  57.             cell30.Text = "<b>Report:</b>";
  58.             row3.Cells.AddAt(0, cell30);
  59.             TableCell cell31 = new TableCell();
  60.             cell31.Text = "New Loans By Deal";
  61.             row3.Cells.AddAt(1, cell31);
  62.             TableCell cell32 = new TableCell();
  63.             cell32.Attributes["align"] = "right";
  64.             cell32.Text = "<font style=/"text-align:right; font-
  65. weight:bold;/">Date/Time:</font>";
  66.             row3.Cells.AddAt(2, cell32);
  67.             
  68.             TableCell cell33 = new TableCell();
  69.             cell33.Text = DateTime.Now.ToString("yyyy-MM-dd") + " " + 
  70. DateTime.Now.ToString(@"HH:mm:ss");
  71.             row3.Cells.AddAt(3, cell33);
  72.             TableCell cell34 = new TableCell();
  73.             cell34.Text = "";
  74.             row3.Cells.AddAt(4, cell34);
  75.             TableCell cell35 = new TableCell();
  76.             cell35.Text = "";
  77.             row3.Cells.AddAt(5, cell35);
  78.             TableCell cell36 = new TableCell();
  79.             cell36.Text = "";
  80.             row3.Cells.AddAt(6, cell36);
  81.             GridView2.Controls[0].Controls.AddAt(0, row3);
  82. }
原创粉丝点击