ExcelHelper----根據指定樣式的數據,生成excel(一個sheet1頁)文件流

 /// <summary>
    /// Excel導出類
    /// </summary>
    public class ExcelHelper
    {
        /// <summary>
        /// 根據指定樣式的數據,生成excel(一個sheet1頁)文件流
        /// </summary>
        /// <param name="data">待導出數據的數組(包括隱藏的數據)</param>
        /// <param name="title">The title.</param>
        /// <param name="header">The header.</param>
        /// <param name="isShowRowNo">.若是定義爲null或者empty,那麼表示不須要增長序號列</param>
        /// <param name="colVisibleFlagArray">待導出數據的沒列的隱藏標識</param>
        /// <param name="autosize">自動單元格寬度調整</param>
        /// <returns>MemoryStream.</returns>
        public static MemoryStream ExportFromArray(string[][] data, string title, string[] header, bool isShowRowNo, bool[] colVisibleFlagArray,bool autosize = true)
        {
            string[][] newdata = new string[data.Length][];
            List<List<string>> newlist = new List<List<string>>();
            List<string> newheader = new List<string>(); 
            //慮隱藏的數據:
            //過濾隱藏的數據頭
            for (int i = 0; i < colVisibleFlagArray.Length; i++)
            {
                if (colVisibleFlagArray[i] == true)
                {
                    newheader.Add(header[i]);
                }
            }
            //過濾隱藏列的數據
            for (int j = 0; j < data.Length; j++)
            {
                newdata[j] = new string[newheader.Count];
               //當前列數
                int cnt = 0;
                //只添加加顯示列的數據
                for (int i = 0; i < colVisibleFlagArray.Length; i++)
                {
                    if (colVisibleFlagArray[i] == true)
                    {
                        newdata[j][cnt] = data[j][i];
                        cnt++;
                    }
                }
            } 
            //若是顯示序號,序號列頭指定爲「序號」;不然不輸出列頭
            string RowNo = isShowRowNo ? "序號" : null;
            MemoryStream stream = new MemoryStream();
            ExportExcelFromData instance = new ExportExcelFromData();
            SheetDefine sheet=new SheetDefine("sheet1", title, RowNo, newheader.ToArray(), newdata);
            sheet.AutoSizeColumn = autosize;
            instance.AddSheet(sheet);

            instance.WriteExcel(stream);
  
            return stream;
        }
    }
相關文章
相關標籤/搜索