• List<T>导出表格


    /// <summary>
            /// 导出excle文件
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list"></param>
            /// <param name="filePath"></param>
            /// <param name="sheetName"></param>
            public static void WriteExcel<T>(List<T> list, string filePath, string sheetName)
            {
                try
                {
                    if (!string.IsNullOrEmpty(filePath) && null != list && list.Count > 0)
                    {
                        NPOI.SS.UserModel.IWorkbook book;
                        NPOI.SS.UserModel.ISheet sheet;
                        if (filePath.EndsWith(".xlsx"))
                        {
                            book = new NPOI.XSSF.UserModel.XSSFWorkbook();
                            sheet = book.CreateSheet(sheetName);
                        }
                        else
                        {
                            book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                            sheet = book.CreateSheet(sheetName);
                        }
                        NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
                        Type itemType = typeof(T);
                        System.Reflection.PropertyInfo[] itemPropertyies = itemType.GetProperties();
                        for (int i = 0; i < itemPropertyies.Length; i++)
                        {
                            var attribute = itemPropertyies[i].GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().Single();
                            row.CreateCell(i).SetCellValue(attribute.DisplayName);
                        }
                        for (int i = 0; i < list.Count; i++)
                        {
                            NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
                            for (int j = 0; j < itemPropertyies.Length; j++)
                            {
    
                                row2.CreateCell(j).SetCellValue(Convert.ToString(itemPropertyies[j].GetValue(list[i])));
                            }
                        }
                        // 写入到客户端  
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                        {
                            book.Write(ms);
                            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                            {
                                byte[] data = ms.ToArray();
                                fs.Write(data, 0, data.Length);
                                fs.Flush();
                            }
                            book = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("写入excel时报错:" + ex.Message);
                }
            }
  • 相关阅读:
    Windows编译openssl3
    【转】FFmpeg采集设备
    构建FFmpeg项目时链接报错avformat_alloc_context未定义
    anaconda代理设置
    静态链接导致的一个bug分析
    Qt如果发送信号过快会如何?
    关闭Edge浏览器多窗口Alt+Tab组合键切换
    [转]Windows上的valgrinddeleaker
    在qt项目中编译错误error ::clock未声明
    使用单元测试驱动开发的方式编写flask应用
  • 原文地址:https://www.cnblogs.com/LLLLoveLLLLife/p/8295465.html
Copyright © 2020-2023  润新知