在?Infragistics UltraWebGrid 所在的页面上点击鼠标右键,可以在右键菜单中选择“导出到Microsoft Office Excel(X)”来实现将Infragistics UltraWebGrid 中的数据导出到Excel中,但是对于复合表头或者有隐藏行、隐藏列的情况,用这种方法处理起来就不太方便,会给客户增加一些不必要的处理麻烦。为了能方便的处理将有复合表头、隐藏行、隐藏列的Infragistics UltraWebGrid 中的数据导出到Excel中,我编写了以下的导出类,可以实现导出功能。由于时间有限,对于导出后背景颜色等细节的处理,没有添加进去。代码如下:
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Data;

//添加引用:Com?->?Microsoft?Excel?11.0?Object?Library?
using?Microsoft.Office.Core;
using?Microsoft.Office.Interop.Excel;
using?Infragistics.WebUI.UltraWebGrid;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.IO;
using?System.Diagnostics;

namespace?ExportToExcel

...{

????/**////?<summary>
????///?导出数据到Excel的类
????///?</summary>
????public?class?Export

????...{
????????private?Microsoft.Office.Interop.Excel.Application?xlApp;
????????private?Microsoft.Office.Interop.Excel.Workbook?workbook;
????????private?object?missing?=?System.Reflection.Missing.Value;
????????private?byte[]?Buffer;
???????
????????//导出文件的路径(长名)
????????private?string?Report?=?"";

????????//导入到Excel时的行开始位置
????????private?int?rowStartIndex?=?1;

????????//导入到Excel时的列开始位置
????????private?int?colStartIndex?=?1;

????????//是否显示标题
????????bool?isShowTitle?=?true;

????????//是否显示边框线
????????bool?isShowGridLine?=?true;

????????//表格标题字体大小
????????private?int?titleFontSize?=?14;

????????//表格内容字体大小
????????private?int?tableFontSize?=?12;


????????/**////?<summary>
????????///?获取或设置导入到Excel时在Excel中行的开始位置(大于0的整数)
????????///?</summary>
????????public?int?RowStartIndex

????????...{

????????????get?...{?return?rowStartIndex;?}
????????????set?

????????????...{
????????????????if?(value?>?0)

????????????????...{
????????????????????rowStartIndex?=?value;
????????????????}
????????????????else

????????????????...{
????????????????????rowStartIndex?=?1;
????????????????}
????????????}
????????}


????????/**////?<summary>
????????///?获取或设置导入到Excel时在Excel中列的开始位置(大于0的整数)
????????///?</summary>
????????public?int?ColStartIndex

????????...{

????????????get?...{?return?colStartIndex;?}
????????????set?

????????????...{
????????????????if?(value?>?0)

????????????????...{
????????????????????colStartIndex?=?value;
????????????????}
????????????????else

????????????????...{
????????????????????colStartIndex?=?1;
????????????????}
????????????}
????????}


????????/**////?<summary>
????????///?获取或设置是否显示表格标题
????????///?</summary>
????????public?bool?IsShowTitle

????????...{

????????????get?...{?return?isShowTitle;?}

????????????set?...{?isShowTitle?=?value;?}
????????}


????????/**////?<summary>
????????///?获取或设置是否显示表格的边框和格线
????????///?</summary>
????????public?bool?IsShowGridLine

????????...{

????????????get?...{?return?isShowGridLine;?}

????????????set?...{?isShowGridLine?=?value;?}
????????}


????????/**////?<summary>
????????///?获取或设置表格标题字体大小(大于0的整数)
????????///?</summary>
????????public?int?TitleFontSize

????????...{

????????????get?...{?return?titleFontSize;?}
????????????set

????????????...{
????????????????if?(value?>?0)

????????????????...{
????????????????????titleFontSize?=?value;
????????????????}
????????????????else

????????????????...{
????????????????????titleFontSize?=?14;
????????????????}
????????????}
????????}


????????/**////?<summary>
????????///?获取或设置表格内容字体大小(大于0的整数)
????????///?</summary>
????????public?int?TableFontSize

????????...{

????????????get?...{?return?tableFontSize;?}
????????????set

????????????...{
????????????????if?(value?>?0)

????????????????...{
????????????????????tableFontSize?=?value;
????????????????}
????????????????else

本文介绍了一种将InfragisticsUltraWebGrid中的数据,包括复合表头及隐藏行列,导出到Excel的方法。通过自定义导出类,实现了更灵活的数据处理方式。

2585

被折叠的 条评论
为什么被折叠?



