NPOI导出Excel(多sheet表)

csharp

浏览数:189

2019-1-7

AD:资源代下载服务

片段 1片段 2片段 3片段 4


Excel基类

namespace ZBiddingDB.Core.Common
{
    public class ExcelBase
    {
        /// <summary>
        /// 将excel先转换成临时DataTable,后续
        /// 将进行相关属性规则转换
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="strFileName"></param>
        public static DataTable ConvertExcelToDataTable(string strFileName, int startRow = 0,int iLastCellNum=0)
        {
            DataTable dtTemp = new DataTable();

            #region NPOI执行转换
            
            using (FileStream fileStream = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.Delete))
            {
                HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
                HSSFSheet sheet = workbook.GetSheetAt(0);

                HSSFRow headerRow = sheet.GetRow(startRow);
                iLastCellNum = (iLastCellNum == 0) ? headerRow.LastCellNum : iLastCellNum;
                for (int iStartCell = 0; iStartCell < iLastCellNum; iStartCell++)
                {
                    dtTemp.Columns.Add(headerRow.GetCell(iStartCell).ToString().Trim());
                }

                for (int iStartRow = startRow + 1; iStartRow <= sheet.LastRowNum; iStartRow++)
                {
                    HSSFRow dataRow = sheet.GetRow(iStartRow);

                    DataRow drWBS = dtTemp.NewRow();
                    for (int iDataStartCell = 0; iDataStartCell < iLastCellNum; iDataStartCell++)
                    {
                        HSSFCell dataCell = dataRow.GetCell(iDataStartCell);

                        if (null != dataCell)
                        {
                            #region 值转换

                            switch (dataCell.CellType)
                            {
                                case (int)HSSFCellType.BLANK:
                                    drWBS[iDataStartCell] = string.Empty;
                                    break;
                                case (int)HSSFCellType.BOOLEAN:
                                    drWBS[iDataStartCell] = dataCell.BooleanCellValue.ToString();
                                    break;
                                case (int)HSSFCellType.ERROR:
                                    drWBS[iDataStartCell] = dataCell.ErrorCellValue.ToString();
                                    break;
                                case (int)HSSFCellType.NUMERIC:
                                    if (HSSFDateUtil.IsCellDateFormatted(dataCell))
                                    {
                                        DateTime dateCellValue = dataCell.DateCellValue;
                                        if (dateCellValue.Year == 1900)
                                        {
                                            drWBS[iDataStartCell] = dataCell.ToString();
                                        }
                                        else
                                        {
                                            drWBS[iDataStartCell] = IDCommon.CommonFuns.ToDateString(dateCellValue);
                                        }
                                    }
                                    else
                                    {
                                        drWBS[iDataStartCell] = dataCell.NumericCellValue;
                                    }
                                    break;
                                case (int)HSSFCellType.STRING:
                                    drWBS[iDataStartCell] = dataCell.StringCellValue;
                                    break;
                                case (int)HSSFCellType.Unknown:
                                default:
                                    drWBS[iDataStartCell] = dataCell.ToString();
                                    break;
                            }

                            #endregion
                        }
                    }

                    dtTemp.Rows.Add(drWBS);
                }
            }

            #endregion

            return dtTemp;
        }

        #region 属性 枚举
        protected enum delayDaysContent
        {
            延期未启动, 延期未完成, 延期完成
        }
        /// <summary>
        /// 是否仅仅存放在文件中。
        /// 如果不启用的话,将返回文件路径(一般用于ajax下)。
        /// 否则直接提示导出。
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public bool SaveToFileOnly
        {
            get;
            set;
        }

        private string _strFilePath = "";        
        /// <summary>
        /// 存放的文件相对路径
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public string FilePath
        {
            get { return _strFilePath; }
        }

        private string _strPhysicalFilePath = "";
        /// <summary>
        /// 存放的文件物理路径
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public string PhysicalFilePath
        {
            get { return _strPhysicalFilePath; }
        }

        /// <summary>
        /// 列名字体
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFFont ColumnNameFont
        {
            get
            {
                if (ColumnNameFontCopy == null)
                {
                    HSSFFont font = hssfworkbook.CreateFont();
                    font.FontName = "宋体";
                    font.FontHeightInPoints = 10;
                    font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    ColumnNameFontCopy = font;
                    return font;
                }
                return ColumnNameFontCopy;
            }
        }
        private HSSFFont ColumnNameFontCopy;
        /// <summary>
        /// 正文字体
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFFont ContentFont
        {
            get
            {
                //此处不能频繁创建字体,否则字体太多会报错误
                if (ContentFontCopy == null)
                {
                    HSSFFont font = hssfworkbook.CreateFont();
                    font.FontName = "宋体";
                    font.FontHeightInPoints = 10;
                    ContentFontCopy = font;
                    return font;
                }
                return ContentFontCopy;
            }
        }
        private HSSFFont ContentFontCopy;
        /// <summary>
        /// 红色字体
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFFont RedContentFont
        {
            get
            {
                //此处不能频繁创建字体,否则字体太多会报错误
                if (RedContentFontCopy == null)
                {
                    HSSFFont font = hssfworkbook.CreateFont();
                    font.FontName = "宋体";
                    font.Color = HSSFFont.COLOR_RED;
                    font.FontHeightInPoints = 10;
                    RedContentFontCopy = font;
                    return font;
                }
                return RedContentFontCopy;
            }
        }
        private HSSFFont RedContentFontCopy;
        /// <summary>
        /// 蓝色字体
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFFont BlueContentFont
        {
            get
            {
                //此处不能频繁创建字体,否则字体太多会报错误
                if (BlueContentFontCopy == null)
                {
                    HSSFFont font = hssfworkbook.CreateFont();
                    font.FontName = "宋体";
                    font.Color = HSSFColor.BLUE.index;
                    font.FontHeightInPoints = 10;
                    BlueContentFontCopy = font;
                    return font;
                }
                return BlueContentFontCopy;
            }
        }
        private HSSFFont BlueContentFontCopy;

        private HSSFCellStyle ColumnNameStyleCopy;
        /// <summary>
        /// 列名Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ColumnNameStyle
        {
            get
            {
                if (null == ColumnNameStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ColumnNameFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;
                    ColumnNameStyleCopy = style;
                }
                return ColumnNameStyleCopy;
            }
        }

        private HSSFCellStyle ColumnNameLeftStyleCopy;
        /// <summary>
        /// 列名左对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ColumnNameLeftStyle
        {
            get
            {
                if (null == ColumnNameLeftStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ColumnNameFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_LEFT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;

                    ColumnNameLeftStyleCopy = style;
                }
                return ColumnNameLeftStyleCopy;
            }
        }

        private HSSFCellStyle ContentLeftStyleCopy;
        /// <summary>
        /// 正文左对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ContentLeftStyle
        {
            get
            {
                //不能创建太多,否则后面的样式会失效。
                if (null == ContentLeftStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_LEFT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;

                    ContentLeftStyleCopy = style;
                }
                return ContentLeftStyleCopy;
            }
        }

        private HSSFCellStyle ContentMiddleStyleCopy;
        /// <summary>
        /// 正文居中对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ContentMiddleStyle
        {
            get
            {
                if (null == ContentMiddleStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;

                    ContentMiddleStyleCopy = style;
                }
                return ContentMiddleStyleCopy;
            }
        }

        private HSSFCellStyle ContentRightStyleCopy;
        /// <summary>
        /// 正文右对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ContentRightStyle
        {
            get
            {
                if (null == ContentRightStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;
                    style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");

                    ContentRightStyleCopy = style;
                }
                return ContentRightStyleCopy;
            }
        }

        private HSSFCellStyle ContentNoBorderStyleCopy;
        /// <summary>
        /// 正文无边框Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ContentNoBorderStyle
        {
            get
            {
                if (null == ContentNoBorderStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_LEFT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    ContentNoBorderStyleCopy = style;
                }
                return ContentNoBorderStyleCopy;
            }
        }

        private HSSFCellStyle ColumnNameNoBorderStyleCopy;
        /// <summary>
        /// 列名无边框Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle ColumnNameNoBorderStyle
        {
            get
            {
                if (null == ColumnNameNoBorderStyleCopy)
                {

                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ColumnNameFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    ColumnNameNoBorderStyleCopy = style;
                }
                return ColumnNameNoBorderStyleCopy;
            }
        }

        private HSSFCellStyle SubTotalAccountingStyleCopy;
        /// <summary>
        /// 小计会计格式
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle SubTotalAccountingStyle
        {
            get
            {
                if (null == SubTotalAccountingStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ColumnNameFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;
                    //会计格式
                    HSSFDataFormat format = hssfworkbook.CreateDataFormat();
                    style.DataFormat = format.GetFormat("_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * \" - \"??_ ;_ @_ ");

                    SubTotalAccountingStyleCopy = style;
                }
                return SubTotalAccountingStyleCopy;
            }
        }

        private HSSFCellStyle SubTotalStyleCopy;
        /// <summary>
        /// 小计总计Style,数字类型的
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle SubTotalStyle
        {
            get
            {
                if (null == SubTotalStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.SetFont(this.ColumnNameFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;

                    style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                    SubTotalStyleCopy = style;
                }

                return SubTotalStyleCopy;
            }
        }

        private HSSFCellStyle LockedContentRightStyleCopy;
        /// <summary>
        /// Locked正文右对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle LockedContentRightStyle
        {
            get
            {
                if (null == LockedContentRightStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.IsLocked = true;
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;
                    style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                    LockedContentRightStyleCopy = style;
                }
                return LockedContentRightStyleCopy;
            }
        }

        private HSSFCellStyle UnlockedContentRightStyleCopy;
        /// <summary>
        /// UnLocked正文右对齐Style
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFCellStyle UnlockedContentRightStyle
        {
            get
            {
                if (null == UnlockedContentRightStyleCopy)
                {
                    HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    style.IsLocked = false;
                    style.SetFont(this.ContentFont);
                    //对齐
                    style.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    style.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
                    //边框
                    style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                    style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                    style.BorderRight = HSSFCellStyle.BORDER_THIN;
                    style.BorderTop = HSSFCellStyle.BORDER_THIN;
                    style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                    UnlockedContentRightStyleCopy = style;
                }
                return UnlockedContentRightStyleCopy;
            }
        }
        public HSSFWorkbook hssfworkbook;
        /// <summary>
        /// 初始化WorkBook
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        protected void InitializeWorkbook()
        {
            if (string.IsNullOrEmpty(strTemplate))
            {
                hssfworkbook = new HSSFWorkbook();
            }
            else
            {
                FileStream file = new FileStream(curContext.Server.MapPath("~/" + strTemplate),
                    FileMode.Open, FileAccess.Read);
                hssfworkbook = new HSSFWorkbook(file);
            }

            ////create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "zhongjyuan";
            hssfworkbook.DocumentSummaryInformation = dsi;

            ////create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "zhongjyuan";
            hssfworkbook.SummaryInformation = si;
        }
        /// <summary>
        /// Http上下文
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HttpContext curContext
        {
            get;
            set;
        }
        /// <summary>
        /// 文件名
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public string strFileName
        {
            get;
            set;
        }
        /// <summary>
        /// sheet Name
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public List<string> arrSheetName
        {
            get;
            set;
        }
        /// <summary>
        /// 表头名
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public string strTableHeaderName
        {
            get;
            set;
        }
        /// <summary>
        /// 获取第一个Sheet
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public HSSFSheet FirstSheet
        {
            get
            {
                if (hssfworkbook.NumberOfSheets > 0)
                {
                    return hssfworkbook.GetSheetAt(0);
                }
                else
                {
                    return hssfworkbook.CreateSheet(this.arrSheetName[0]);
                }
            }
        }

        public const string CONST_NA = "N/A";
        /// <summary>
        /// EXCEL模板生成
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public string strTemplate
        {
            get;
            set;
        }
        /// <summary>
        /// 定义页面类型枚举
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public enum PageType
        {
            /// <summary>
            /// 新增页面
            /// </summary>
            Add,
            /// <summary>
            /// 修改、调整页面
            /// </summary>
            Edit,
            /// <summary>
            /// 变更页面
            /// </summary>
            Change,
            /// <summary>
            /// 浏览页面   (Browse页面,Check页面中除 调整之外,都是浏览)
            /// </summary>
            Browse
        }
        #endregion

        #region 方法
        /// <summary>
        /// 设置每行的单元格格式
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="row">行</param>
        /// <param name="style">样式Style</param>
        protected void SetRowStyle(HSSFRow row, HSSFCellStyle style)
        {
            IEnumerator enumerator = row.GetEnumerator();
            while (enumerator.MoveNext())
            {
                HSSFCell cell = (HSSFCell)enumerator.Current;
                cell.CellStyle = this.ColumnNameStyle;
            }
        }
        /// <summary>
        /// 输出文件
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        private void WriteToFile()
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            hssfworkbook.Write(ms);

            if (!SaveToFileOnly)
            {
                // 设置编码和附件格式   
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = Encoding.UTF8;
                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition",
                    "attachment;filename=" + HttpUtility.UrlEncode(strFileName + ".xls", Encoding.UTF8));

                curContext.Response.BinaryWrite(ms.GetBuffer());
                curContext.Response.End();
                ms.Close();
                ms.Dispose();
            }
            else
            {
                string strDirPath = "\\Accessary\\Ex_Xls\\" + string.Format("{0:0000}", DateTime.Now.Year) + "\\" + String.Format("{0:00}", DateTime.Now.Month);
                _strFilePath = strDirPath + "\\" + strFileName + ".xls";
                strDirPath = curContext.Server.MapPath(HttpContext.Current.Request.ApplicationPath + strDirPath);
                if (!System.IO.Directory.Exists(strDirPath))
                {
                    System.IO.Directory.CreateDirectory(strDirPath); //这里可能需要配置IIS目录权限为可写入。
                }

                _strPhysicalFilePath = strDirPath + "\\" + strFileName + ".xls";

                using (FileStream fs = new FileStream(_strPhysicalFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Delete))
                {
                    byte[] data = ms.ToArray();

                    fs.Write(data, 0, data.Length);
                    fs.Flush();

                    data = null;
                }

            }

            hssfworkbook = null;

        }

        /// <summary>
        /// 构建列名称
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public virtual void BuildHeader()
        {

        }
        /// <summary>
        /// 构建内容项
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public virtual void BuildItems()
        {
        }
        /// <summary>
        /// 构建Excel
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        public virtual void BuildExcel()
        {
            InitializeWorkbook();
            BuildHeader();
            BuildItems();
            WriteToFile();
        }
        /// <summary>
        /// 创建EXCEL行,包括创建单元格
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="rowNum">行号,从0开始算</param>
        /// <param name="icellCounts">单元格的数量</param>
        /// <param name="style">Style</param>
        protected HSSFRow CreateRowCells(int rowNum, int icellCounts, HSSFCellStyle style)
        {
            HSSFSheet sheet = FirstSheet;//默认获取第1个SHEET
            HSSFRow row = sheet.CreateRow(rowNum);
            row.HeightInPoints = 19.5f;
            for (int i = 0; i < icellCounts; i++)
            {
                HSSFCell cell = row.CreateCell(i);
                cell.CellStyle = style;
            }

            return row;
        }
        /// <summary>
        /// 支持任意HSSFSheet创建EXCEL行,包括创建单元格
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="isheet">第几个Sheet</param>
        /// <param name="rowNum">行号,从0开始算</param>
        /// <param name="icellCounts">单元格的数量</param>
        /// <param name="style">Style</param>
        /// <returns></returns>
        protected HSSFRow CreateRowCells(HSSFSheet isheet, int rowNum, int icellCounts, HSSFCellStyle style)
        {
            HSSFSheet sheet = isheet;//支持任意Sheet
            HSSFRow row = sheet.CreateRow(rowNum);
            row.HeightInPoints = 19.5f;
            for (int i = 0; i < icellCounts; i++)
            {
                HSSFCell cell = row.CreateCell(i);
                cell.CellStyle = style;
            }

            return row;
        }

        /// <summary>
        /// 根据ASCII CODE获取字母
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="asciiCode"></param>
        /// <returns></returns>
        protected string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }
        /// <summary>
        /// 导入Excel,返回DataTable
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="path">Excel路径</param>
        /// <returns>DataTable</returns>
        public DataTable ConvertToDataTable(string path)
        {
            DataTable dt = new DataTable();
            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            HSSFSheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
            while (rows.MoveNext())
            {
                HSSFRow row = (HSSFRow)rows.Current;
                DataRow dr = dt.NewRow();
                //创建表格行
                //if (dt.Columns.Count == 0)
                //{
                //    for (int j = 0; j < row.LastCellNum; j++)
                //    {
                //        dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
                //    }
                //}
                /* Modify by: wenghq, on 2012/10/24, 
                 * 因为读取Excel时,是逐行扫描,当扫描到第一行时,只有第一个单元格有数据,此时这里的row.LastCellNum返回1,
                 * 能正确的给dt增加一个列。
                 * 但是当扫描到Excel的第三行数据时,row.LastCellNum返回15(因为实际上是有15列的数据),因为此时dt.Columns.Count已经不等于0了。
                 * 所以无法给dt增加列,故改称一下逻辑,判断dt的列数小于row.LastCellNum时,补足缺少的列。
                 */
                if (dt.Columns.Count < row.LastCellNum)
                {
                    for (int j = dt.Columns.Count; j < row.LastCellNum; j++)
                    {
                        dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
                    }
                }
                //赋值
                for (int i = 0; i < row.LastCellNum; i++)
                {
                    HSSFCell cell = row.GetCell(i);
                    if (cell == null)
                    {
                        dr[i] = null;
                    }
                    else
                    {
                        dr[i] = cell.ToString();
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 设置总计行的公式,默认为(SUM):总计=小计+小计...
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="totalRow">总计行</param>
        /// <param name="arrColumn">需要总计的列</param>
        /// <param name="listSubTotalRow">需要总计的行</param>
        protected void SetTotalRowFormula(HSSFRow totalRow, int[] arrColumn, List<int> listSubTotalRow)
        {
            StringBuilder sbFormular = new StringBuilder();
            sbFormular.Append("SUM(");
            for (int i = 0; i < listSubTotalRow.Count; i++)
            {
                if (i < listSubTotalRow.Count - 1)
                {
                    sbFormular.Append("{0}" + (1 + listSubTotalRow[i]).ToString() + ",");
                }
                else
                {
                    sbFormular.Append("{0}" + (1 + listSubTotalRow[i]).ToString() + ")");
                }
            }
            for (int i = 0; i < arrColumn.Length; i++)
            {
                totalRow.GetCell(arrColumn[i]).SetCellFormula(string.Format(sbFormular.ToString(), Chr(arrColumn[i] + 65)));
                totalRow.GetCell(arrColumn[i]).CellStyle = this.SubTotalStyle;
                totalRow.GetCell(arrColumn[i]).CellStyle.IsLocked = true;
            }
        }
        /// <summary>
        /// 设置批注
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="strComment"></param>
        protected void SetCellComment(HSSFCell cell, string strComment)
        {
            HSSFPatriarch patr = FirstSheet.CreateDrawingPatriarch();
            HSSFComment comment = patr.CreateComment(new HSSFClientAnchor(0, 0, 0, 0, cell.ColumnIndex, cell.RowIndex, cell.ColumnIndex + 1, cell.RowIndex + 3));

            // set text in the comment
            comment.String = (new HSSFRichTextString(strComment));
            cell.CellComment = (comment);
        }
        /// <summary>
        /// 根据后台配置任务类型、列名得到CellStyle(ExcelBase类里的居左属性会有bug,原因不明,故在此写)
        /// zhongjyuan
        /// 2018-08-15
        /// </summary>
        /// <param name="strPlanType">取值范围:KF,FUN,NewAdd</param>
        /// <param name="strColName">列名</param>
        /// <returns></returns>
        protected HSSFCellStyle GetCellStyle(string alignVal)
        {
            HSSFCellStyle cellStyle = hssfworkbook.CreateCellStyle();

            //对齐
            cellStyle.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
            //边框
            cellStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderRight = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderTop = HSSFCellStyle.BORDER_THIN;

            switch (alignVal)
            {
                case "left":
                    cellStyle.Alignment = HSSFCellStyle.ALIGN_LEFT;
                    break;
                case "center":
                    cellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    break;
                case "right":
                    cellStyle.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                    break;
                default:
                    break;
            }

            return cellStyle;
        }
        #endregion
    }
}

    /// <summary>
    /// Excel表格列数据类型
        /// zhongjyuan
        /// 2018-08-15
    /// </summary>
public enum HSSFCellType : int
{
    Unknown = -1,
    /// <summary>
    /// 数字
    /// </summary>
    NUMERIC = 0,
    /// <summary>
    /// 字符串
    /// </summary>
    STRING = 1,
    /// <summary>
    /// 公式
    /// </summary>
    FORMULA = 2,
    /// <summary>
    /// 空白
    /// </summary>
    BLANK = 3,
    /// <summary>
    /// 布尔
    /// </summary>
    BOOLEAN = 4,
    /// <summary>
    /// 错误
    /// </summary>
    ERROR = 5
}


excel导出类

    /// <summary>
    /// 导出模板
    /// 作者:钟金元
    /// 时间:2017年12月06日 14:27:51
    /// </summary>
    public class ProjectPurchaseToExcel : ExcelBase
    {
        HSSFSheet sheetProjectCase;//主表
        HSSFSheet sheet1;//
        HSSFSheet sheet2;
        HSSFSheet sheet3;
        HSSFSheet sheet4;
        HSSFSheet sheet5;
        HSSFName range;//定义名称,下拉列表所在区域
        DVConstraint constraint;//加载下拉列表内容
        CellRangeAddressList regions;//加载单元格位置
        HSSFDataValidation dataValidate;//数据有效性对象
        private string PTID;
        public void setPTID(string PTID)
        {
            this.PTID = PTID;
        }

        public override void BuildHeader()
        {
            //主表
            int iCellIndex = 0;
            sheetProjectCase = (hssfworkbook.NumberOfSheets > 0) ? hssfworkbook.GetSheetAt(0) : hssfworkbook.CreateSheet(this.arrSheetName[0]);
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//计划/合约名称
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//采购主体
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//采购方式
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//采购专业
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//招投标方式
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//供应方式
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//标段划分
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//标段说明
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//招标经办人
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//战略采购协议
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//签约控制价
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//计划开始时间
            sheetProjectCase.SetColumnWidth(iCellIndex++, 15 * 256);//计划结束时间
            //采购主体表
            iCellIndex = 0;
            sheet1 = (hssfworkbook.NumberOfSheets > 1) ? hssfworkbook.GetSheetAt(1) : hssfworkbook.CreateSheet(this.arrSheetName[1]);
            sheet1.SetColumnWidth(iCellIndex++, 40 * 256);
            //采购方式表
            iCellIndex = 0;
            sheet2 = (hssfworkbook.NumberOfSheets > 2) ? hssfworkbook.GetSheetAt(2) : hssfworkbook.CreateSheet(this.arrSheetName[2]);
            sheet2.SetColumnWidth(iCellIndex++, 40 * 256);
            //采购专业表
            iCellIndex = 0;
            sheet3 = (hssfworkbook.NumberOfSheets > 3) ? hssfworkbook.GetSheetAt(3) : hssfworkbook.CreateSheet(this.arrSheetName[3]);
            sheet3.SetColumnWidth(iCellIndex++, 40 * 256);
            //招投标方式表
            iCellIndex = 0;
            sheet4 = (hssfworkbook.NumberOfSheets > 4) ? hssfworkbook.GetSheetAt(4) : hssfworkbook.CreateSheet(this.arrSheetName[4]);
            sheet4.SetColumnWidth(iCellIndex++, 40 * 256);
            //供应方式表
            iCellIndex = 0;
            sheet5 = (hssfworkbook.NumberOfSheets > 5) ? hssfworkbook.GetSheetAt(5) : hssfworkbook.CreateSheet(this.arrSheetName[5]);
            sheet5.SetColumnWidth(iCellIndex++, 40 * 256);

        }
        public override void BuildItems()
        {
            //采购主体
            Loadsheet1Case();
            //采购方式
            Loadsheet2Case();
            //采购专业
            Loadsheet3Case();
            //招投标方式
            Loadsheet4Case();
            //供应方式
            Loadsheet5Case();
            //主表
            LoadProjectCase();
        }
        private void LoadProjectCase()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;
            //表头
            row = this.CreateRowCells(sheetProjectCase, iRowIndex++, 13, this.ContentLeftStyle);

            cell = row.GetCell(iCellIndex++);//列号
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*计划/合约名称");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*采购主体");
            //设置约束
            regions = new CellRangeAddressList(0, 65535, 1, 1);
            constraint = DVConstraint.CreateFormulaListConstraint("dicRange1");
            dataValidate = new HSSFDataValidation(regions, constraint);
            sheetProjectCase.AddValidationData(dataValidate);

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*采购方式");
            //设置约束
            regions = new CellRangeAddressList(0, 65535, 2, 2);
            constraint = DVConstraint.CreateFormulaListConstraint("dicRange2");
            dataValidate = new HSSFDataValidation(regions, constraint);
            sheetProjectCase.AddValidationData(dataValidate);

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*采购专业");
            //设置约束
            regions = new CellRangeAddressList(0, 65535, 3, 3);
            constraint = DVConstraint.CreateFormulaListConstraint("dicRange3");
            dataValidate = new HSSFDataValidation(regions, constraint);
            sheetProjectCase.AddValidationData(dataValidate);

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*招投标方式");
            //设置约束
            regions = new CellRangeAddressList(0, 65535, 4, 4);
            constraint = DVConstraint.CreateFormulaListConstraint("dicRange4");
            dataValidate = new HSSFDataValidation(regions, constraint);
            sheetProjectCase.AddValidationData(dataValidate);

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("供应方式");
            //设置约束
            regions = new CellRangeAddressList(0, 65535, 5, 5);
            constraint = DVConstraint.CreateFormulaListConstraint("dicRange5");
            dataValidate = new HSSFDataValidation(regions, constraint);
            sheetProjectCase.AddValidationData(dataValidate);

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("标段划分");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("标段说明");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*招标经办人");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("战略采购协议");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("签约控制价");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*计划开始时间");

            cell = row.GetCell(iCellIndex++);
            cell.CellStyle = ColumnNameStyle;
            cell.SetCellValue("*计划结束时间");

            //数据
        }
        //采购主体
        private void Loadsheet1Case()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;

            //数据
            CTZBMainBodyView bodyview = new CTZBMainBodyView();
            DataTable dt = bodyview.GetDataTable(CTZBMainBody._.IsDelete == "N", CTZBMainBody._.RowNo.Asc, new ExpressionClip[] { CTZBMainBody._.ZBMBID, CTZBMainBody._.ZBMBName });
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    iCellIndex = 0;
                    row = this.CreateRowCells(sheet1, iRowIndex++, 1, this.ContentLeftStyle);

                    cell = row.GetCell(iCellIndex++);
                    cell.CellStyle = ContentLeftStyle;
                    cell.SetCellValue(dt.Rows[i]["ZBMBName"].ToString());
                }
            }
            //定义一个名称,指向当前表数据的区域
            range = hssfworkbook.CreateName();
            range.Reference = String.Format("{2}!${0}$1:${0}${1}", (Char)('A'), dt.Rows.Count, "采购主体");//=Sheet2!$A$1:$A$5  
            range.NameName = "dicRange1";
        }
        //采购方式
        private void Loadsheet2Case()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;

            //数据
            DataTable dt = new DataTable();
            dt.Columns.Add("ZBPProperty");
            dt.Columns.Add("ZBPPropertyName");
            DataRow dr = dt.NewRow();
            dr["ZBPProperty"] = "0";
            dr["ZBPPropertyName"] = "分散采购";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ZBPProperty"] = "1";
            dr["ZBPPropertyName"] = "战采协议";
            dt.Rows.Add(dr);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    iCellIndex = 0;
                    row = this.CreateRowCells(sheet2, iRowIndex++, 1, this.ContentLeftStyle);

                    cell = row.GetCell(iCellIndex++);
                    cell.CellStyle = ContentLeftStyle;
                    cell.SetCellValue(dt.Rows[i]["ZBPPropertyName"].ToString());
                }
            }
            //定义一个名称,指向当前表数据的区域
            range = hssfworkbook.CreateName();
            range.Reference = String.Format("{2}!${0}$1:${0}${1}", (Char)('A'), dt.Rows.Count, "采购方式");
            range.NameName = "dicRange2";
        }
        //采购专业
        private void Loadsheet3Case()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;

            //数据
            IDDatabase dbType = new IDDatabase();
            DataSet dsZbetType = dbType.GetDataSet(ConnectionName.ZBiddingDB, "select ZBETID,ZBETName,ZBiddingDB.dbo.F3_GetZBETypeOutline(zbetid,ptid) as Outline from ZBiddingDB.dbo.TZBEType where IsDelete='N' and PTID='" + PTID + "' ORDER BY Outline ASC");
            DataTable dt;
            if (dsZbetType != null && dsZbetType.Tables[0].Rows.Count > 0)
            {
                dt = dsZbetType.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        iCellIndex = 0;
                        row = this.CreateRowCells(sheet3, iRowIndex++, 1, this.ContentLeftStyle);

                        cell = row.GetCell(iCellIndex++);
                        cell.CellStyle = ContentLeftStyle;
                        cell.SetCellValue(dt.Rows[i]["ZBETName"].ToString());
                    }
                }
            }
            else
            {
                dt = new DataTable();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        iCellIndex = 0;
                        row = this.CreateRowCells(sheet3, iRowIndex++, 1, this.ContentLeftStyle);

                        cell = row.GetCell(iCellIndex++);
                        cell.CellStyle = ContentLeftStyle;
                        cell.SetCellValue(dt.Rows[i]["ZBETName"].ToString());
                    }
                }
            }
            //定义一个名称,指向当前表数据的区域
            range = hssfworkbook.CreateName();
            range.Reference = String.Format("{2}!${0}$1:${0}${1}", (Char)('A'), dt.Rows.Count, "采购专业");
            range.NameName = "dicRange3";
        }
        //招投标方式
        private void Loadsheet4Case()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;

            //数据
            CTNewZBWorkFlowModelView zbwfmView = new CTNewZBWorkFlowModelView();
            DataTable dt = zbwfmView.GetDataTable(CTNewZBWorkFlowModel._.IsDelete == "N", CTNewZBWorkFlowModel._.RowNo.Asc, new ExpressionClip[] { CTNewZBWorkFlowModel._.ZBWFMID, CTNewZBWorkFlowModel._.ZBWFMName });
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    iCellIndex = 0;
                    row = this.CreateRowCells(sheet4, iRowIndex++, 1, this.ContentLeftStyle);

                    cell = row.GetCell(iCellIndex++);
                    cell.CellStyle = ContentLeftStyle;
                    cell.SetCellValue(dt.Rows[i]["ZBWFMName"].ToString());
                }
            }
            //定义一个名称,指向当前表数据的区域
            range = hssfworkbook.CreateName();
            range.Reference = String.Format("{2}!${0}$1:${0}${1}", (Char)('A'), dt.Rows.Count, "招投标方式");
            range.NameName = "dicRange4";
        }
        //供应方式
        private void Loadsheet5Case()
        {
            HSSFRow row = null;
            HSSFCell cell = null;
            int iRowIndex = 0, iCellIndex = 0;

            //数据
            CTQCTypeView QCTypeView = new CTQCTypeView();
            DataTable dt = QCTypeView.GetDataTable(CTQCType._.RowNo.Asc);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    iCellIndex = 0;
                    row = this.CreateRowCells(sheet5, iRowIndex++, 1, this.ContentLeftStyle);

                    cell = row.GetCell(iCellIndex++);
                    cell.CellStyle = ContentLeftStyle;
                    cell.SetCellValue(dt.Rows[i]["QCTName"].ToString());
                }
            }
            //定义一个名称,指向当前表数据的区域
            range = hssfworkbook.CreateName();
            range.Reference = String.Format("{2}!${0}$1:${0}${1}", (Char)('A'), dt.Rows.Count, "供应方式");
            range.NameName = "dicRange5";
        }
    }


调用实例

            ProjectPurchaseToExcel PExcel = new ProjectPurchaseToExcel();
            PExcel.setPTID(PTID);
            PExcel.arrSheetName = new System.Collections.Generic.List<string>();
            PExcel.arrSheetName.Add("项目采购总计划表");
            PExcel.arrSheetName.Add("采购主体");
            PExcel.arrSheetName.Add("采购方式");
            PExcel.arrSheetName.Add("采购专业");
            PExcel.arrSheetName.Add("招投标方式");
            PExcel.arrSheetName.Add("供应方式");
            PExcel.strFileName = CGeneral.GetIDValue()+".xls";//项目采购总计划表";
            PExcel.curContext = HttpContext.Current;
            PExcel.SaveToFileOnly = true;
            PExcel.BuildExcel();

            string[] dr = { PExcel.PhysicalFilePath.Replace(Request.PhysicalApplicationPath,""), PExcel.strFileName };
            
            ahd.Data = JsonConvert.SerializeObject(dr);
            ahd.Success = "Y";
            //dr为下载文件路径


前端下载该Excel文件

                       downloadFile(Path, "项目采购总计划表.xls", null, true);

function downloadFile(fileName, fileTitle, frameName)
{
    if (!fileTitle)
    {
        if (fileName.indexOf("/") != -1)
        {
            fileTitle = fileName.substr(fileName.lastIndexOf("/") + 1);
        }
        else if (fileName.indexOf("\\") != -1)
        {
            fileTitle = fileName.substr(fileName.lastIndexOf("\\") + 1);
        }
        else
        {
            fileTitle = fileName;
        }
    }
    if (!frameName)
    {
        frameName = "ID_DownloadFile";
    }
    var frame = getObj(frameName);
    if (frame == null || frame.tagName.toUpperCase() != "IFRAME")
    {
        frame = document.createElement("IFRAME");
        frame.id = frameName;
        frame.style.display = "none";
        document.body.appendChild(frame);
    }

    var url = "/" + rootUrl + "/Common/Handler/Download.aspx?FileName=" + encode(fileName) + "&FileTitle=" + encode(fileTitle);
    if (arguments.length > 3 && arguments[3] === true)
    {
        url += "&Opt=Export";
    }

    frame.src = url;
}