复合表头的RadGridView导出excel

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;
}
}

}

时间: 2025-01-18 08:44:02

复合表头的RadGridView导出excel的相关文章

使用easypoi根据表头信息动态导出excel

第一步添加依赖 <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.1.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi

silverlight RadGridView 复合表头 多表头 导出excel

ListHeaderShow.Clear(); ListHeaderShow.Add("区县"); ListHeaderShow.Add("企业名称"); ListHeaderShow.Add("监测点名称"); ListHeaderShow.Add("监测时间"); Util.Export<T_DATA_DATACOMMON> Exportxls = new Util.Export<T_DATA_DATAC

单表头,多表头导出excel

一.单表头 1 #region 导出 2 if (this.dgvInfo.Rows.Count > 0) 3 { 4 SaveFileDialog saveFileDialog = new SaveFileDialog(); 5 saveFileDialog.DefaultExt = "xls"; 6 saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.xls"; 7 saveFileDialog.FilterIndex =

带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel(转)

步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript"> //导出Excel function exportExcel() { var data = ""; $("#divRptTable").find("table").find("tr").each(function

Java导出Excel三表头

1.问题背景 Java导出Excel表格时,表头出现了三个,即多表头Excel 2.实现源码 /** * * @Project:Report * @Title:ThreeHead.java * @Package:com.you.excel * @Description: * @Author:YouHaiDong * @Date:2015年11月4日 下午3:10:12 * @Version: */ package com.you.excel; import java.io.FileOutputS

【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进阶,除了上一篇提到的处理乱码问题,本例还添加了处理多行表头.合并单元格的功能及处理中文文件名乱码问题,应该可以满足日常开发的需要了. 废话不多说了,直接上代码: [C#] 可以写单独类 1 using System; 2 using System.Collections.Generic; 3 usi

poi导出Excel报表多表头双层表头、合并单元格

效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @RequestMapping("/export")    @ResponseBody    public void export(HttpServletRequest request,            HttpServletResponse response, String year

【每日一点】1. Java如何实现导出Excel单表头或多表头

一.背景 在后台项目中,经常会遇到将呈现的内容导出到Excel的需求,通过都是导出单个表头的Excel文件,如果存在级联关系的情况下,也就需要导出多表头的场景.今天这篇文章就是分享导出Excel单表头或多表头的实现,目前实现方案仅支持2行表头场景.如有更复杂的3行表头.4行表头复杂需求可以自行实现. 二.实现思路 1. 借助POI包实现表头的写入.每个表头其实就是一行,如果是多个表头,无非就是将写多行表头,然后将需要合并的表头进行合并,借助POI的函数为addMergedRegion. 2. 将

导入导出Excel的Java工具类ExcelUtil

在编写ExcelUtil之前,在网上查了一些资料.java中用来处理Excel的第三方开源项目主要就是POI和JXL.poi功能强大,但是比较耗资源,对于大数据量的导入导出性能不是太好:jxl功能简单,但是性能比较好. 由于本项目的导入导出更多关注性能问题,而且jxl提供的功能基本也都够用了,于是选择了jxl作为支持. 实战 导出就是将List转化为Excel(listToExcel) 导入就是将Excel转化为List(excelToList) 导入导出中会出现各种各样的问题,比如:数据源为空