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
public void Exportexcel (T ttype, list<t> tsource, String tfilename, RadGridView Tgridview, list<string> Tlist Header, String Tkey, Raddatapager Tdatapager)
{
int pageSize = 0;
int pageIndex = 0;
if (Tdatapager! = null)
{
PageSize = tdatapager.pagesize;
PageIndex = Tdatapager.pageindex;
Export all of the data by setting page zeroing.
Tdatapager.pageindex = 0;
tdatapager.pagesize = 0;
}
T t_data = new T ();
int CurrentRow;
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 + = (sender, e) = =
{
if (e.element = = Exportelement.headerrow)
{
String Header = "<table style= ' border-collapse:collapse ' border= ' 1 ' ><tr>";
foreach (Var col in Tgridview.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 (Tlistheader.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 (Tlistheader.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 = Tsource.count;
if (CurrentRow < i)
{
T_data = Tsource[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 (Tkey). 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>");
}
}
}
});
Executed prior to exported
tgridview.elementexporting + = (sender, 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, Tkey);
}
}
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 (Tkey). 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 (Tkey). 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;
}
});
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;
}
}
Specific data output, the corresponding table header
private static string Datashow (object Column, Object obj, T t_data, String Tkey)
{
String value = "";
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 ();
}
}
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 = "xlsx";
SaveFileDialog Dialog = new SaveFileDialog ()
{
DefaultExt = extension,
Filter = String.Format ("{1} files (*.{ 0}) |*. {0}| All Files (*. *) |*.* ", extension," Excel "),
FilterIndex = 1,
Defaultfilename = Tfilename
};
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;
}
}
}
RadGridView of composite Headers export Excel