从Infragistics UltraWebGrid 导出数据到Excel

本文介绍如何使用自定义方法导出Infragistics UltraWebGrid中的复杂数据(包括复合表头和隐藏行列)到Excel,避免了默认导出方式的局限性。虽然未处理背景颜色等细节,但提供了直接导出并删除服务器端临时文件的功能。通过在按钮点击事件中调用代码,可以实现一键导出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 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
                
{
                    tableFontSize 
= 12;
                }

            }

        }


       
/// <summary>
        
/// 构造函数
       
/// </summary>
        
/// <param name="tempDirectory">存放临时文件的目录</param>

        public Export(string tempDirectory)
        
{
            
this.xlApp = new Application();
            
this.xlApp.DisplayAlerts = false;
            
this.workbook = xlApp.Workbooks.Add(true);
            
this.Report = this.Report = System.IO.Path.Combine(tempDirectory, DateTime.Now.ToString("yyyyMMdd-HHmmss"+ ".xls");
        }


        
/// <summary>
        
/// 析构函数
        
/// </summary>

        ~Export()
        
{
            CloseExcle();
        }


        
/// <summary>
        
/// 将UltraWebGrid中的内容导入到Excel文档中
        
/// </summary>
        
/// <param name="grid">导出数据的UltraWebGrid的ID</param>
        
/// <param name="title">导出的表格的标题</param>
        
/// <param name="isShowHiddenRow">标志是否显示隐藏的行</param>
        
/// <param name="isShowHiddenCol">标志是否显示隐藏的列</param>
        
/// <param name="Response">封装来自ASP.NET操作的HTTP相应信息</param>

        public void UltraWebGridExportToExcel(UltraWebGrid grid, string title, bool isShowHiddenRow, bool isShowHiddenCol, HttpResponse Response)
        
{
            
//注意:Excel的行列序均从1开始
            try
            
{
                Infragistics.WebUI.UltraWebGrid.HeadersCollection dt 
= grid.Bands[0].HeaderLayout;

                
画表前的预处理

                
画表过程

                xlApp.Cells.EntireColumn.AutoFit();
                xlApp.Cells.VerticalAlignment 
= Microsoft.Office.Interop.Excel.Constants.xlCenter;
                xlApp.Cells.HorizontalAlignment 
= Microsoft.Office.Interop.Excel.Constants.xlCenter;

                
//保存临时文件到服务器端
                workbook.SaveAs(this.Report, XlFileFormat.xlTemplate, missing, missing, missing, missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                
//发送文件到客户端
                SendFileToClient(Response);
            }

            
catch (Exception ex)
            
{
                
throw (ex);
            }

        }


        
//产生下载效果导出Excel
        private void SendFileToClient(HttpResponse Response)
        
{
            
try
            
{
                
//删除服务器端的临时文件
                DeleteExcelFile();

                Response.Clear();
                Response.Buffer 
= true;
                Response.Charset 
= "GB2312";
                Response.AppendHeader(
"Content-Disposition""attachment;filename=ExportDataTable.xls");
                Response.ContentEncoding 
= System.Text.Encoding.UTF7;
                Response.ContentType 
= "application/ms-excel";
                Response.BinaryWrite(Buffer);
                Response.Flush();
                Response.Close();
                Response.End();
            }

            
catch (Exception ex)
            
{
                
throw (ex);
            }

        }


        
        
//清除内存中的Excle进程
        private void CloseExcle()
        
{
            
if (this.workbook != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.workbook);
                
this.workbook = null;
            }

            
if (this.xlApp != null)
            
{
                System.Runtime.InteropServices.Marshal.ReleaseComObject(
this.xlApp);
                
this.xlApp = null;
            }

            
            GC.Collect();
        }


        
//删除生成的Excel临时文件
        private void DeleteExcelFile()
        
{
            
this.workbook.Save();
            
this.workbook.Close(missing, missing, missing);
            
this.xlApp.Quit();
            CloseExcle();

            FileStream MyFileStream 
= new FileStream(this.Report, FileMode.Open);
            
long FileSize = MyFileStream.Length;
            Buffer 
= new byte[(int)FileSize];
            MyFileStream.Read(Buffer, 
0, (int)FileSize);
            MyFileStream.Close();

            FileInfo mode 
= new FileInfo(this.Report);
            
try
            
{
                mode.Delete();
            }

            
catch (Exception ex)
            
{
                
throw (ex);
            }

        }

    }

}

对类的调用方式:

在UltraWebGrid所在页面添加一个导出按钮(如Button1),在该按钮的单击事件中加入如下代码:

   protected   void  Button1_Click( object  sender, EventArgs e)
    
{
       
//在应用程序的根目录下建立一个临时文件夹
        string path = Server.MapPath("~/Temp/");
        ExportToExcel.Export ex 
= new ExportToExcel.Export(path);
        ex.RowStartIndex 
= 3;
        ex.ColStartIndex 
= 3;
        ex.IsShowGridLine 
= true;
        ex.IsShowTitle 
= true;
        ex.TableFontSize 
= 16;
        ex.TitleFontSize 
= 18;

        ex.UltraWebGridExportToExcel(UltraWebGrid1, 
"导出数据"falsefalse, Response);
    }

这样就可以导出数据到Excel,并发送到客户端,然后把服务器端的临时文件自动删除。

以上只是其中的一种方法,如有其它更佳方法,欢迎探讨!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值