写通用的list导excel

-一个大坑 2019-09-18 03:46:59
只能先转datatable,再循环导出excel吗
用List<T> list的话,t不确定,不能循环。。。
...全文
148 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2019-09-19
  • 打赏
  • 举报
回复

目前是这样的,list先转datatable,然后Model反射取列名和列宽,最后循环datatable
可以直接循环list吗,我不会反射取list的每个栏位的值
        public static MemoryStream DataTableToExcel<T>(DataTable dt) where T : class, new()
        {
            XSSFWorkbook fileWorkbook = new XSSFWorkbook();
            ISheet sheet = fileWorkbook.CreateSheet("Sheet1");

            List<int> nonDownList = new List<int>();
            List<int> columnWidthList = new List<int>();
            List<string> columnNameList = new List<string>();
            var type = typeof(T).GetType();
            int nonDown = 0;
            foreach (var porp in type.GetProperties())
            {
                nonDown++;
                if (porp.IsDefined(typeof(ShowNameAttribute), true))
                {
                    var showNameAttribute = porp.GetCustomAttribute<ShowNameAttribute>();
                    columnNameList.Add(showNameAttribute.ShowName);

                    var columnWidthAttribute = porp.GetCustomAttribute<ColumnWidthAttribute>();
                    var number = columnWidthAttribute == null ? columnWidth : columnWidthAttribute.Number;
                    columnWidthList.Add(number);
                }
                else
                {
                    nonDownList.Add(nonDown);
                }
            }

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < columnNameList.Count; i++)
            {
                ICell cell = row.CreateCell(i); //創建單元格
                cell.SetCellValue(columnNameList[i]); //表頭賦值
                sheet.SetColumnWidth(i, columnWidthList[i]); //表頭寬度
                cell.CellStyle = excelStyle.CellHeadStyle; //表頭樣式
                row.Height = 30 * 20; //行高为30
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (!nonDownList.Contains(j))
                    {
                        ICell cell = row1.CreateCell(j);
                        string drValue = dt.Rows[i][j].ToString();
                        CellStyle(cell, dt.Columns[j].DataType.ToString(), drValue);
                    }
                }
            }

            //转为字节数组
            using (MemoryStream ms = new MemoryStream())
            {
                fileWorkbook.Write(ms);
                ms.Flush();
                return ms;
            }
        }
-一个大坑 2019-09-19
  • 打赏
  • 举报
回复
引用 10 楼 XBodhi. 的回复:
带样式的 可以用 com 组件直接调用 ApplicationClass csv 的可以用用如下代码 我博客有。 https://blog.csdn.net/qiaohuyue/article/details/95472532
现在主要是list<T>循环取值问题,以及list<T>转dt循环取值好还是list<T>反射取值好,哪个效率高点
-一个大坑 2019-09-19
  • 打赏
  • 举报
回复
引用 9 楼 exception92 的回复:
[quote=引用 7 楼 -一个大坑 的回复:] [quote=引用 6 楼 exception92 的回复:] 转DataTable简单点也不是什么坏事。https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/
本来以前是DataTableToExcel(DataTable dt, string[] columnNameArr, int[] columnWidthArr)这样, 后面发现增加报表栏位经常容易忘了增加列名或者宽度,下载就报错, 如果用特性,没有设置特性可以指定默认值 当然也可以传model和dt,先取特性的列名数组和宽度数组,再循环把dt的内容写入excel 不过觉得list转dt再循环好像有点多此一举[/quote] https://www.easyxls.com/manual/basics/export-list-to-excel.html 看这个。[/quote] 看了一下,以list的形式添加,和dt不一样,不过,我还是做不到循环list的数据来添加。 因为传得T不知道是什么类型,也就循环不了,反射我只会取属性和特性,和属性值,list就不知道怎么反射取值了
XBodhi. 2019-09-19
  • 打赏
  • 举报
回复
带样式的 可以用 com 组件直接调用 ApplicationClass csv 的可以用用如下代码 我博客有。 https://blog.csdn.net/qiaohuyue/article/details/95472532
exception92 2019-09-19
  • 打赏
  • 举报
回复
引用 7 楼 -一个大坑 的回复:
[quote=引用 6 楼 exception92 的回复:] 转DataTable简单点也不是什么坏事。https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/
本来以前是DataTableToExcel(DataTable dt, string[] columnNameArr, int[] columnWidthArr)这样, 后面发现增加报表栏位经常容易忘了增加列名或者宽度,下载就报错, 如果用特性,没有设置特性可以指定默认值 当然也可以传model和dt,先取特性的列名数组和宽度数组,再循环把dt的内容写入excel 不过觉得list转dt再循环好像有点多此一举[/quote] https://www.easyxls.com/manual/basics/export-list-to-excel.html 看这个。
-一个大坑 2019-09-19
  • 打赏
  • 举报
回复
引用 4 楼 by_封爱 的回复:
where T class new 就行了 然后根据T反射表头 然后取值... 跟datatable一样.
列名和列宽都找出来了,但是list要怎么反射取值 如果不行,我就先转dt再循环dt取值,还要增加需要排除的列

            var type = default(T).GetType();
            foreach (var porp in type.GetProperties())
            {
                if(porp.IsDefined(typeof(ShowNameAttribute), true))
                {
                    var showNameAttribute = porp.GetCustomAttribute<ShowNameAttribute>();
                    showNameList.Add(showNameAttribute.ShowName);

                    var columnWidthAttribute = porp.GetCustomAttribute<ColumnWidthAttribute>();
                    var number = columnWidthAttribute == null ? columnWidth : columnWidthAttribute.Number;
                    columnWidthList.Add(number);
                }               
            }
-一个大坑 2019-09-19
  • 打赏
  • 举报
回复
引用 6 楼 exception92 的回复:
转DataTable简单点也不是什么坏事。https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/
本来以前是DataTableToExcel(DataTable dt, string[] columnNameArr, int[] columnWidthArr)这样, 后面发现增加报表栏位经常容易忘了增加列名或者宽度,下载就报错, 如果用特性,没有设置特性可以指定默认值 当然也可以传model和dt,先取特性的列名数组和宽度数组,再循环把dt的内容写入excel 不过觉得list转dt再循环好像有点多此一举
极客诗人 2019-09-19
  • 打赏
  • 举报
回复
赞同4#
by_封爱 2019-09-19
  • 打赏
  • 举报
回复
where T class new 就行了 然后根据T反射表头 然后取值... 跟datatable一样.
-一个大坑 2019-09-18
  • 打赏
  • 举报
回复
引用 2 楼 -一个大坑 的回复:
好像很多反射,判断,转换的

//数据
            for (int i = 0; i < list.Count; i++)
            {
                int j = 0;
                IRow row1 = sheet.CreateRow(i + 1);
                foreach (var porp in type.GetProperties())
                {                    
                    if (porp.IsDefined(typeof(ShowNameAttribute), true))
                    {
                        j++;
                        ICell cell = row1.CreateCell(j);
                        string drValue = porp.GetValue(default(T)).ToString();
                        CellStyle(cell, porp.PropertyType.Name, drValue);
                    }
                }
            }
好像没用到list。。。
-一个大坑 2019-09-18
  • 打赏
  • 举报
回复
好像很多反射,判断,转换的

//数据
            for (int i = 0; i < list.Count; i++)
            {
                int j = 0;
                IRow row1 = sheet.CreateRow(i + 1);
                foreach (var porp in type.GetProperties())
                {                    
                    if (porp.IsDefined(typeof(ShowNameAttribute), true))
                    {
                        j++;
                        ICell cell = row1.CreateCell(j);
                        string drValue = porp.GetValue(default(T)).ToString();
                        CellStyle(cell, porp.PropertyType.Name, drValue);
                    }
                }
            }
stherix 2019-09-18
  • 打赏
  • 举报
回复
可以使用反射,不过自己要写一些代码 图简单就转Datatable然后转Excel

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧