Home > Net >  The gridview export data to EXCEL
The gridview export data to EXCEL

Time:10-09

The gridview self-built header binding data, web page browsing OK, export to EXCEL header when there is something wrong with,
Posted question part of the code, everybody to help me see, here is part of the code of the meter
Protected void GridView1_RowCreated (object sender, GridViewRowEventArgs e)
{
String date1=TextBox1. Text;
String date2=TextBox2. Text;
The switch (" e.R ow. RowType)
{
Case DataControlRowType. Header:

//total header
TableCellCollection tcHeader="e.R ow. Cells;
TcHeader. The Clear ();
//the first row header
TcHeader. Add (new TableHeaderCell ());
TcHeader [0]. The Attributes. The Add (" bgcolor ", "white");
TcHeader [0]. The Attributes. The Add (" BorderColor ", "# 333300");
TcHeader [0]. The Attributes. The Add (" BorderWidth ", "1 px");
TcHeader [0]. The Attributes. The Add (" colspan ", "10");//merge in the first row 10
TcHeader [0]. Text="BRT line traffic platform income statistics & lt;/th> ";
//the second line header
TcHeader. Add (new TableHeaderCell ());
TcHeader [1]. The Attributes. The Add (" bgcolor ", "white");
TcHeader [1]. The Attributes. The Add (" colspan ", "10");//merge the second row 10
tcHeader [1]. The Text=TextBox1. "~" Text + + TextBox2. Text + "& lt;/th> ";
.
}
}
The following is the code for the export file
Private void Export1 (the GridView gvUser, string Style, string FileType, string FileName)
{
The Response. ClearContent ();
The Response. The Charset="GB2312";
The Response. ContentEncoding=System. Text.. Encoding UTF8;
The Response. AppendHeader (" the Content - the Disposition ", "legislation; Filename="+ HttpUtility. UrlEncode (filename, System. Text. Encoding. UTF8). The ToString ());
The Response. ContentType=FileType;
//page. The Response. ContentType="application/ms - excel";
Page. EnableViewState=false;
StringWriter sw=new StringWriter ();
HtmlTextWriter hw=new HtmlTextWriter (sw);
//turn off the paging
GvUser. RenderControl (hw);
Response. Write (Style);
Response. Write (sw. ToString ());
//page. The Response. Flush ();
The Response. The End ();
}
If change the red font to tcHeader [1]. The Text="2019-07-01" + "and" + "2019-07-07" + "& lt;/th> "; Is no problem, the content of the header can only be exported write die? What should the dynamic binding export? The great god is what I use to build priority meter method and export to see how to change the code

CodePudding user response:

To what is wrong, it is ok to debug, isn't it?

CodePudding user response:

reference 1st floor it_gz_xi response:
quote what is wrong, it is ok to debug?

Look at the picture, the export date guide not to come out, procedural problem

TextBox1. Text="2019-07-01"
TextBox2. Text="2019-07-07"
TcHeader [1]. The Text="2019-07-01" + "to" + "2019-07-07" + "& lt;/th> ";
TcHeader [1]. The Text=TextBox1. "~" Text + + TextBox2. Text + "& lt;/th> ";
These two headers to create a statement at the time of export what's the difference

CodePudding user response:

Look to debug the TextBox1. Text has no value?

CodePudding user response:

reference it_gz_xi reply: 3/f
look debugging TextBox1. Text have no value?

Has a value, the first picture is when debugging on the web page for the query results, everything is all right
The following picture is the export to EXCEL file, the date of the second line is missing

CodePudding user response:

Change:
String myText=TextBox1. Text + + TextBox2. "~" Text + "& lt;/th> ";
TcHeader [1]. The Text=myText;
Here a breakpoint,
TcHeader [1]. The Text=myText;
What see myText value,
Back to the interrupted point? Please press F9

CodePudding user response:

reference 5 floor it_gz_xi reply:
change:
String myText=TextBox1. Text + + TextBox2. "~" Text + "& lt;/th> ";
TcHeader [1]. The Text=myText;
Here a breakpoint,
TcHeader [1]. The Text=myText;
What see myText value,
Back to the interrupted point? Please press F9

MyText "2019-07-26 ~ 2019-07-26 & lt;/th> " The string
The crux of the problem is that the string myText=TextBox1. "~" Text + + TextBox2. Text + "& lt;/th> "; Take control value export guide when I was out of the TextBox1 control value,
Write die tcHeader [1]. The Text="2019-07-01" + "to" + "2019-07-07" + "& lt;/th> "; Export is no problem
Displayed on the web, including tcHeader breakpoint query method [1]. The Text values are corresponding to the date of the period, if I can't find the TextBox control the export of the statement read values?

CodePudding user response:

Your TextBox1 not server controls, are you? With server controls

CodePudding user response:

refer to 7th floor it_gz_xi response:
your TextBox1 not server controls, are you? With server controls

TEXTBOX server controls are not to be?
  •  Tags:  
  • C#
  • Related