Listheadershow.clear ();
Listheadershow.add ("counties");
Listheadershow.add ("Enterprise Name");
Listheadershow.add ("Monitoring point Name");
Listheadershow.add ("Monitoring Time");
util.export<t_data_datacommon> Exportxls = new util.export<t_data_datacommon> ();
for (int i = 5; I <= 7; i++)
{
if (this. Gridview.columns[i]. IsVisible = = True)
{
Exportxls.getheadertext (GridView, I, listheadershow);
}
}
util.export<t_data_datacommon> Exportxls = new util.export<t_data_datacommon> ();
Exportxls.exportexcel (T_data, ListData, this. Header.tostring (), this. GridView, Listheadershow, "Regionname", DataPager);
public class export<t> where T:new ()
{
<summary>
GridView Export Excel
</summary>
<param name= "Ttype" > Rows correspond to objects such as t_data_datacommon</param>
<param name= "TSource" >gridview data source </param>
<param name= "Tfilename" > Export file naming </param>
<param name= "Tgridview" > gridview</param> to Export
<param name= "Tlistheader" >gridview Header string Collection </param>
<param name= The primary key in the corresponding binding in the "Tkey" >gridview </param>
Tdatapager Sub-page
int pageSize;
int pageIndex;
BOOL Isfirstload;
List<t> tsources;
RadGridView tgridviews;
T T_data;
int CurrentRow;
String Tkeys;
List<string> tlistheaders;
public void Exportexcel (T ttype, list<t> tsource, String tfilename, RadGridView Tgridview, list<string> Tlist Header, String Tkey, Raddatapager Tdatapager)
{
pageSize = 0;
PageIndex = 0;
Isfirstload = true;
if (Tdatapager! = null)
{
PageSize = tdatapager.pagesize;
PageIndex = Tdatapager.pageindex;
Export all of the data by setting page zeroing.
Tdatapager.pageindex = 0;
tdatapager.pagesize = 0;
}
Tsources = new list<t> ();
Tsources = TSource;
Tgridviews = new RadGridView ();
Tgridviews = Tgridview;
Tkeys = Tkey;
Tlistheaders = new list<string> ();
Tlistheaders = Tlistheader;
T_data = new T ();
CurrentRow = 0;
if (Tsource.count > 0)
{
T_data = Tsource[currentrow];
}
string extension = "xls";
SaveFileDialog Dialog = new SaveFileDialog ()
{
DefaultExt = extension,
Filter = String.Format ("{1} files (*.{ 0}) |*. {0}| All Files (*. *) |*.* ", extension," Excel "),
FilterIndex = 1,
Defaultfilename = Tfilename
};
tgridview.elementexported + = tgridview_elementexported;
Executed prior to exported
Tgridview.elementexporting + = tgridview_elementexporting;
if (dialog. ShowDialog () = = True)
{
using (Stream stream = dialog. OpenFile ())
{
Tgridview.export (Stream,
New Gridviewexportoptions ()
{
Format = exportformat.html,
Showcolumnheaders = True,
Showcolumnfooters = False,
Showgroupfooters = False,
});
}
}
if (Tdatapager! = null)
{
Tdatapager.pagesize = PageSize;
Tdatapager.pageindex = PageIndex;
}
tgridview.elementexported-= tgridview_elementexported;
Tgridview.elementexporting-= tgridview_elementexporting;
MessageBox.Show ("Export processing completed. ");
}
void Tgridview_elementexporting (object sender, Gridviewelementexportingeventargs e)
{
if (e.value! = null)
{
if (e.element = = Exportelement.cell && e.value.gettype () = = typeof (T))
{
Gridviewcolumn Column = E.context as Gridviewcolumn;
E.value = Datashow (Column, E.value, T_data, Tkeys);
}
}
Else
{
if (e.element = = Exportelement.cell && e.context.gettype () = = typeof (Gridviewdatacolumn))
{
String value = "";
Gridviewcolumn C = E.context as Gridviewcolumn;
if (c.celltemplate! = null)
{
if (c.celltemplate.gettype () = = typeof (DataTemplate))
{
C.celltemplate.loadcontent ();
TextBlock Txtblock = (TextBlock) c.celltemplate.loadcontent ();
Type type = T_data. GetType (); Get type
if (type. GetProperty (Tkeys). GetValue (T_data, NULL). ToString () = null)
{
System.Reflection.PropertyInfo PropertyInfo = type. GetProperty (TxtBlock.Tag.ToString ()); Gets the property of the specified name
Value + = (propertyinfo.getvalue (t_data, NULL). ToString ());
}
}
}
E.value = Value;
}
}
if (e.element = = Exportelement.cell && e.context.gettype () = = typeof (Gridviewdatacolumn) && E. value.tostring () = = "")
{
Try
{
String value = "";
Gridviewcolumn C = E.context as Gridviewcolumn;
if (c.celltemplate! = null)
{
if (c.celltemplate.gettype () = = typeof (DataTemplate))
{
C.celltemplate.loadcontent ();
TextBlock Txtblock = (TextBlock) c.celltemplate.loadcontent ();
Type type = T_data. GetType (); Get type
if (type. GetProperty (Tkeys). GetValue (T_data, NULL). ToString () = null)
{
System.Reflection.PropertyInfo PropertyInfo = type. GetProperty (TxtBlock.Tag.ToString ()); Gets the property of the specified name
Value + = (propertyinfo.getvalue (t_data, NULL). ToString ());
}
}
}
E.value = Value;
}
Catch {}
}
if (e.element = = Exportelement.headercell)
{
E.cancel = true;
}
}
void Tgridview_elementexported (object sender, Gridviewelementexportedeventargs e)
{
if (e.element = = Exportelement.headerrow && Isfirstload = = True)
{
Isfirstload = false;
String header= "<meta http-equiv= ' content-type ' content= ' application/ms-excel; Charset=utf-8 '/> ';
header+= "<table style= ' border-collapse:collapse ' border= ' 1 ' ><tr>";
foreach (Var col in Tgridviews.columns)
{
Multiple-grid multi-header collection
if (Col. Header.gettype () = = typeof (Grid))
{
Grid G = new Grid ();
G = Col. Header as Grid;
TextBlock L = g.findchildbytype<textblock> ();
if (Tlistheaders.contains (l.text.tostring ()))
{
Header + = "<TD";
Grid Grid = G.findchildbytype<grid> ();
var s = grid. Children;
int x = S.count;
Header + = "colspan=";
Header + = ((x-1)/2). ToString ();
Header + = "' >";
Header + = "<table border frame=box> <tr> <td align=center";
Header + = "colspan=";
Header + = ((x-1)/2). ToString ();
Header + = "' >";
Header + = "<font size= ' 4 ' >";
Header + = L.text.tostring ();
Header + = "</font></td></tr> <tr>";
for (int y = 1; y < x; y + = 2)
{
TextBlock z = s[y] as TextBlock;
Header + = "<td>";
Header + = "<font size= ' 3 ' >";
Header + = Z.text.tostring ();
Header + = "</font></td>";
}
Header + = "</tr> </table>";
Header + = "</td>";
}
}
else if (Tlistheaders.contains (Col. Header.tostring ()))
{
if (Col. CellTemplate = null)
{
Collect the headers that TextBlock put in the DataTemplate
if (Col. Celltemplate.gettype () = = typeof (DataTemplate))
{
Col. Celltemplate.loadcontent ();
TextBlock Txtblock = (TextBlock) Col. Celltemplate.loadcontent ();
Col. UniqueName = Txtblock.name;
Header + = "<td >";
Header + = "<font size= ' 4 ' >";
Header + = Col. Header;
Header + = "</font></td>";
}
}
Else
{
Header + = "<td >";
Header + = "<font size= ' 4 ' >";
Header + = Col. Header;
Header + = "</font></td>";
}
}
}
Header + = "</tr>";
E.writer.write (header);
}
if (e.element = = Exportelement.row)
{
currentrow++;
int i = Tsources.count;
if (CurrentRow < i)
{
T_data = Tsources[currentrow];
}
}
if (e.element = = Exportelement.cell)
{
Gridviewdatacolumn obj = E.context as gridviewdatacolumn;
The compound header for the cell is available here
if (obj. Header.gettype () = = typeof (Grid))
{
Grid G = obj. Header as Grid;
Grid gchileds = new Grid ();
Grid celltemplate = obj. Celltemplate.loadcontent () as Grid;
int chileds = CellTemplate.Children.Count;
for (int i = 2; i < chileds; i + = 2)
{
TextBlock L = new TextBlock ();
L = Celltemplate.children[i] as TextBlock;
E.writer.write ("<td>");
try//avoid null values for Textbloc
{
Type type = T_data. GetType (); Get type
if (type. GetProperty (Tkeys). GetValue (T_data, NULL). ToString () = null)
{
System.Reflection.PropertyInfo PropertyInfo = type. GetProperty (L.tag.tostring ()); Gets the property of the specified name
E.writer.write (Propertyinfo.getvalue (T_data, NULL). ToString ());
}
}
Catch {}
E.writer.write ("</td>");
}
}
}
}
Specific data output, the corresponding table header
private static string Datashow (object Column, Object obj, T t_data, String Tkey)
{
String value = "";
Try
{
Gridviewcolumn C = Column as Gridviewcolumn;
if (c.header.gettype () = = typeof (Grid))
{
Grid celltemplate = C.celltemplate.loadcontent () as Grid;
int chileds = CellTemplate.Children.Count;
TextBlock L = new TextBlock ();
L = celltemplate.children[0] as TextBlock;
Type type = T_data. GetType (); Get type
System.Reflection.PropertyInfo PropertyInfo = type. GetProperty (L.tag.tostring ()); Gets the property of the specified name
if (type. GetProperty (Tkey). GetValue (T_data, NULL). ToString () = null)
{
Value + = Propertyinfo.getvalue (T_data, NULL). ToString ();
}
}
}
Catch
{ }
return value;
}
//Collect first-level header
public void Getheadertext (RadGridView GridView, int headernum, list<string> listheadershow) br> {
if (Gridview.columns[headernum]. Header.gettype () = = typeof (Grid))
{
Grid G = new Grid ();
G = Gridview.columns[headernum]. Header as Grid;
TextBlock L = g.findchildbytype<textblock> ();
Listheadershow.add (l.text.tostring ());
}
Else
{
Listheadershow.add (Gridview.columns[headernum]. Header.tostring ());
}
}
<summary>
GridView export Excel, Simple table
</summary>
<param name= "Tfilename" > Export file naming </param>
<param name= "Tgridview" > gridview</param> to Export
<param name= "Tpageindex" > Pagination index</param>
<param name= "Tpagesize" > Pagination size</param>
public void Exportexcel (string tfilename, RadGridView Tgridview, Raddatapager Tdatapager)
{
int pageSize = 0;
int pageIndex = 0;
if (Tdatapager! = null)
{
PageSize = tdatapager.pagesize;
PageIndex = Tdatapager.pageindex;
Tdatapager.pageindex = 0;
tdatapager.pagesize = 0;
}
string extension = "xls";
SaveFileDialog Dialog = new SaveFileDialog ()
{
DefaultExt = extension,
Filter = String.Format ("{1} files (*.{ 0}) |*. {0}| All Files (*. *) |*.* ", extension," Excel "),
FilterIndex = 1,
Defaultfilename = Tfilename
};
Tgridview.elementexported+=tgridview_elementexported2;
Tgridview.elementexporting+=tgridview_elementexporting2;
if (dialog. ShowDialog () = = True)
{
using (Stream stream = dialog. OpenFile ())
{
Tgridview.export (Stream,
New Gridviewexportoptions ()
{
Format = Exportformat.excelml,
Showcolumnheaders = True,
Showcolumnfooters = False,
Showgroupfooters = False,
});
MessageBox.Show ("Export processing completed. ");
}
}
if (Tdatapager! = null)
{
Tdatapager.pagesize = PageSize;
Tdatapager.pageindex = PageIndex;
}
}
void Tgridview_elementexported2 (object sender, Gridviewelementexportedeventargs e)
{
}
void Tgridview_elementexporting2 (object sender, Gridviewelementexportingeventargs e)
{
}
}
Silverlight RadGridView Composite header multiple header export Excel