标签:
ListHeaderShow.Clear();
ListHeaderShow.Add("区县");
ListHeaderShow.Add("企业名称");
ListHeaderShow.Add("监测点名称");
ListHeaderShow.Add("监测时间");
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导出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 分页
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> TlistHeader, string Tkey, RadDataPager TdataPager)
{
pageSize = 0;
pageIndex = 0;
isFirstLoad = true;
if (TdataPager != null)
{
pageSize = TdataPager.PageSize;
pageIndex = TdataPager.PageIndex;
//通过设置分页归零,来导出全部数据。
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;
//先于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("导出处理完成。");
}
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(); //获取类型
if (type.GetProperty(TkeyS).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(TkeyS).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;
}
}
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)
{
//多grid的多表头进行收集
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)
{
//对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 = TsourceS.Count;
if (currentRow < i)
{
T_data = TsourceS[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(TkeyS).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>");
}
}
}
}
//具体数据输出时,对应表头
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(); //获取类型
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();
}
}
}
catch
{ }
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 = "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("导出处理完成。");
}
}
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 复合表头 多表头 导出excel
标签:
原文地址:http://www.cnblogs.com/gucheng/p/4810999.html