ExcelConfig

来源:互联网 发布:html img js 编辑:程序博客网 时间:2024/05/09 17:02

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Drawing;

/// <summary>
/// 用于设置打印输出的Excel配置文件
/// </summary>
public class ExcelConfig
{
    private object _data = null;
    private bool _isGridView = true;
    private bool _isDataTable = false;

    private bool _isfromtemplate = false;
    private string _exportexcelfilename = "";
    private string _exportexcelfilepath = "";
    private string _templatefilename = "";
    private string _templatefilepath = "";
    private string _sheetname = null;
    private string[] _headertext = null;
    private string[,] _contenttext = null;
    private string _headertitle = "";
    private bool _hasmacro = false;
    private string _macro = "";

    public ExcelConfig()
    {
        this._data = null;
        this._isGridView = false;
        this._isDataTable = false;
        initConfig();
    }
    public ExcelConfig(DataTable dt)
    {
        this._data = dt;
        this._isGridView = false;
        this._isDataTable = true;
        initConfig();
    }
 public ExcelConfig(GridView gv)
 {
        this._data = gv;
        this._isGridView = true;
        this._isDataTable = false;
        initConfig();
 }

    private void initConfig()
    {
       
        this._exportexcelfilename = DateTime.Now.ToString("yyyymmddhhmmss") + ".xls";//默认保存的文件名根据系统时间自动生成
        this._exportexcelfilepath = "~/xls/"; //默认文件输出的路径
        this._templatefilepath = "~/App_Data/";//默认模板文件的路径
        this._sheetname = "Sheet1"; //默认的加载工作表的名称为Sheet1;
        this._headertext = this.getHeaderText(); //根据GridView自动获取HeaderText数组
        this._contenttext = this.getContentText();
    }

    /// <summary>
    /// 是否有标题
    /// </summary>
    public bool HasTitle
    {
        get {
            return this._headertitle!="";
        }
    }
    /// <summary>
    /// 输出保存的Excel文件名
    /// </summary>
    public string OutputFileName
    {
        get {
            return this._exportexcelfilepath + this._exportexcelfilename;

        }
    }

    /// <summary>
    /// 得到或设置GridView或DataTable或
    /// List里面的数据组织成一个二维数组
    /// </summary>
    /// <returns>二维数组</returns>
    private string[,] getContentText()
    {

        if (this._data == null) throw new Exception("请赋值数据");
        string[,] myData = new string[this.RowCount + 1, this.ColumnCount];
        if (this._isGridView)
        {
            GridView _gridview = this._data as GridView;
            //获取GridView中的所有行和列的数值,填充到一个二维数组中.
          
            for (int i = 0; i < this.RowCount; i++)
            {
                for (int j = 0; j < this.ColumnCount; j++)
                {
                    string text = _gridview.Rows[i].Cells[j].Text;
                    if (text == "&nbsp;") text = " "; //考虑到空格的情况(&nbsp;)
                    myData[i, j] = text;//这里的获取注意行列次序
                    //myData[i, j] = dgv[j, i].Value;         //这里的获取注意行列次序
                }
            }

            //return myData;
        }
        else if (this._isDataTable)
        {
            return null;
            throw new Exception("DataTable方法尚未实现");
        }

        return myData;
    }

    /// <summary>
    /// 得到或设置表格的Header部分
    /// </summary>
    /// <returns>一维数组</returns>
    private string[] getHeaderText() {

        string[] headers = new string[this.ColumnCount];
        if (this._isGridView)
        {
            if (this._data == null) throw new Exception("没有赋值要打印输出的GridView对象!");
           
            int index = 0;
            GridView GridView_Data = this._data as GridView;
            foreach (DataControlField dHeader in GridView_Data.Columns)
            {
                headers[index] = dHeader.HeaderText;
                index++;
            }
        }
        else if (this._isDataTable)
        {
            return null;
            //TODO:暂未实现
            throw new Exception("DataTable方试尚未实现");
          
        }

        return headers;
       
    }

    /// <summary>
    /// 指示是否从模板加载(只读属性)
    /// </summary>
    public bool IsFromTemplate
    {
        get
        {
            return this._templatefilename.Trim()!="";
        }
       
    }
    /// <summary>
    /// 保存的Excel文件的名称(eg:通讯录.xls)
    /// </summary>
    public string ExportExcelFileName
    {
        get
        {
            return this._exportexcelfilename;
        }
        set
        {
            this._exportexcelfilename = value;
        }
    }
    /// <summary>
    /// 保存的Excel文件的路径(如不设置默认的为~/xls/)
    /// </summary>
    public string ExportExcelFilePath
    {
        get
        {
            return this._exportexcelfilepath;
        }
        set
        {
            this._exportexcelfilepath = value;
        }
    }
    /// <summary>
    /// 调用的Excel模板名称(默认为template.xls)
    /// </summary>
    public string TemplateFileName
    {
        get
        {
            return this._templatefilename;
        }
        set
        {
            this._templatefilename = value;
        }
    }
    /// <summary>
    /// 调用的Excel模板路径(默认为~/App_Data/下)
    /// </summary>
    public string TemplateFilePath
    {
        get
        {
            return this._templatefilepath;
        }
        set
        {
            this._templatefilepath = value;
        }
    }
    /// <summary>
    /// Excel中的Sheet表(默认为Sheet1)
    /// </summary>
    public string SheetName
    {
        get
        {
            return this._sheetname;
        }
        set
        {
            this._sheetname = value;
        }
    }
    /// <summary>
    /// 自定义或从GridViewa或DataTable中
    /// 得到Header部分用于输出到Excel中的表头部分
    /// </summary>
    public string[] HeaderText
    {
        get
        {
            return this._headertext;
        }
        set
        {
            this._headertext = value;
        }
    }


    /// <summary>
    /// 自定义或从GridViewa或DataTable中或List中
    /// 得到内容用于输出到Excel中的内容部分
    /// </summary>
    public string[,] ContentText
    {
        get
        {
            return this._contenttext;
        }
        set
        {
            this._contenttext = value;
        }
    }
    /// <summary>
    /// 设置或得到第一行的标题
    /// </summary>
    public string HeaderTitle
    {
        get
        {
            return this._headertitle;
        }
        set
        {
            this._headertitle = value;
        }
    }


    /// <summary>
    /// 计算出打印输出的列数(不包括表头)
    /// </summary>
    public int ColumnCount
    {
        get {
            if (this._data==null) throw new Exception("The method or operation is not implemented.");
            if (this._isGridView)
                return (this._data as GridView).Columns.Count;
            else if (this._isDataTable)
            {
                throw new Exception("DataTable方法尚未实现");
                return -1;
            }
            else
                return -1;
        }
    }
    /// <summary>
    /// 计算出打印输出的行数(不包括表头)
    /// </summary>
    public int RowCount
    {
        get
        {
            if (this._data == null) throw new Exception("The method or operation is not implemented.");
            if (this._isGridView)
                return (this._data as GridView).Rows.Count;
            else if (this._isDataTable)
            {
                return -1;
                throw new Exception("DataTable方法尚未实现");

            }
            else
                return -1;
        }
    }
    /// <summary>
    /// 指示是否模板中包含Excel宏定义
    /// </summary>
    public bool HasMacro
    {
        get
        {
            return this._macro.Trim()!="";
        }

    }
    /// <summary>
    /// Excel模板中创建的宏名称
    /// </summary>
    public string Macro
    {
        get
        {
            return this._macro;
        }
        set
        {
            this._macro = value;
        }
    }

}