码迷,mamicode.com
首页 > 其他好文 > 详细

复合表头的RadGridView导出excel

时间:2015-07-27 20:45:12      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

public class Export<T> where T : new()
{
/// <summary>
/// gridview导出excel
/// </summary>
/// <param name="Ttype">行对应的对象 如T_data_datacommon</param>
/// <param name="Tsource">gridview的数据源</param>
/// <param name="TfileName">导出文件命名</param>
/// <param name="TgridView">要导出的gridview</param>
/// <param name="TlistHeader">gridview的表头字符串集合</param>
/// <param name="Tkey">gridview中对应绑定中的主键</param>
/// TdataPager 分页
public void ExportExcel(T Ttype, List<T> Tsource, string TfileName, RadGridView TgridView, List<string> TlistHeader, string Tkey, RadDataPager TdataPager)
{

int pageSize = 0;
int pageIndex = 0;
if (TdataPager != null)
{
pageSize = TdataPager.PageSize;
pageIndex = TdataPager.PageIndex;
//通过设置分页归零,来导出全部数据。
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)
{
//多grid的多表头进行收集
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)
{
//对textblock放到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;
//在此可获得cell对应的复合表头
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//避免textbloc的空值
{
Type type = T_data.GetType(); //获取类型
if (type.GetProperty(Tkey).GetValue(T_data, null).ToString() != null)
{
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(L.Tag.ToString()); //获取指定名称的属性
e.Writer.Write(propertyInfo.GetValue(T_data, null).ToString());
}
}
catch { }
e.Writer.Write("</td>");
}
}
}
});

//先于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(); //获取类型
if (type.GetProperty(Tkey).GetValue(T_data, null).ToString() != null)
{
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(txtBlock.Tag.ToString()); //获取指定名称的属性
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(); //获取类型
if (type.GetProperty(Tkey).GetValue(T_data, null).ToString() != null)
{
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(txtBlock.Tag.ToString()); //获取指定名称的属性
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;
}
}


//具体数据输出时,对应表头
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(); //获取类型
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(L.Tag.ToString()); //获取指定名称的属性

if (type.GetProperty(Tkey).GetValue(T_data, null).ToString() != null)
{
value += propertyInfo.GetValue(T_data, null).ToString();
}

}

return value;
}


//收集一级表头
public void GetHeaderText(RadGridView GridView, int HeaderNum, List<string> ListHeaderShow)
{
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导出excel,简单表
/// </summary>
/// <param name="TfileName"> 导出文件的命名</param>
/// <param name="TgridView"> 要导出的gridview</param>
/// <param name="TpageIndex"> 分页的index</param>
/// <param name="TpageSize"> 分页的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导出excel

标签:

原文地址:http://www.cnblogs.com/gucheng/p/4680791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!