zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    idea14导入eclipse项目并部署运行完整步骤
    Java之Socket
    Docker之宿主机ssh至docker容器
    ElasticSearch的安装、使用、踩坑
    Linux下grep、tail、wc、awk文件处理命令
    Spring中@Async注解实现“方法”的异步调用
    Thrift——栗子
    Linux中的守护进程——supervise
    【composer】 PHP composer 镜像地址更换
    【Mac】解决macos安装升级时报错安装所选更新时发生错误的问题
  • 原文地址:https://www.cnblogs.com/zhengwei-cq/p/10316386.html
Copyright © 2011-2022 走看看