首页 > 编程语言 >C# NPOI reflection import data into excel file

C# NPOI reflection import data into excel file

时间:2024-02-03 17:12:05浏览次数:21  
标签:set reflection get C# into ToString using public string

using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using System.Reflection;
using NPOI.SS.Formula.Functions;
using NPOI.XSSF;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;


namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ImportStuListToExcel(100);
            PrintLog();
        }

        static void PrintLog(string msg = null, [System.Runtime.CompilerServices.CallerLineNumber] int lineNum = -1,
          [CallerMemberName] string func = null, [CallerFilePath] string fileName = null)
        {
            Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")},{msg},{lineNum},{func},{fileName}");
        }

        static void ImportStuListToExcel(int len)
        {
            var list = InitStudentList(len);
            ImportListTToExcel<Student>(list, $"aaa{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.xlsx");
        }

        static void ImportListTToExcel<T>(List<T> dataList, string excelFilePath) where T : class
        {
            if(File.Exists(excelFilePath))
            {
                File.Delete(excelFilePath);
            }

            //create work book
            IWorkbook workbook = new XSSFWorkbook();  

            //create sheet
            ISheet sheet = workbook.CreateSheet("Sheet1");

            //Header row
            int rowIdx = 0;
            IRow headerRow = sheet.CreateRow(rowIdx++);
            var props = typeof(T).GetProperties();
            int propCount=props.Length;

            int columnIdx = 0;
            foreach (var prop in props)
            {
                var cell = headerRow.CreateCell(columnIdx++);
                cell.SetCellValue(prop.Name);
            }  
            
            foreach (var stu in dataList) 
            {
                IRow dataRow=sheet.CreateRow(rowIdx++);
                columnIdx = 0;
                foreach (var prop in props)
                {
                    var cell = dataRow.CreateCell(columnIdx++);                    
                    cell.SetCellValue(prop.GetValue(stu).ToString());                        
                }
            }

            using (FileStream file = new FileStream(excelFilePath, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(file); 
            }
            workbook.Close();             
        }

        static List<Student> InitStudentList(int len)
        {
            List<Student> list = new List<Student>();
            for (int i = 0; i < len; i++)
            {
                list.Add(new Student()
                {
                    Id = i + 1,
                    Name = Guid.NewGuid().ToString(),
                    Description = Guid.NewGuid().ToString(),
                    Title = Guid.NewGuid().ToString(),
                    Topic = Guid.NewGuid().ToString(),
                    Author = Guid.NewGuid().ToString(),
                    ISBN = Guid.NewGuid().ToString(),
                    Summary = Guid.NewGuid().ToString(),
                });
            }
            return list;
        }
    } public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
        public string ISBN { get; set; }
        public string Summary { get; set; }
        public string Author { get; set; }
    }
}
 

 

标签:set,reflection,get,C#,into,ToString,using,public,string
From: https://www.cnblogs.com/Fred1987/p/18004960

相关文章

  • [SWPUCTF 2021 新生赛]easy_md5--php弱类型比较
    <?phphighlight_file(__FILE__);include'flag2.php';if(isset($_GET['name'])&&isset($_POST['password'])){$name=$_GET['name'];$password=$_POST['password'];if($name!=......
  • EasyCVR智能视频监控平台云台降低延迟小tips
    关于EasyCVR的使用tips很多,今天小编就分享一下在使用EasyCVR云台控制时出现延迟的解决方法。很多用户在将设备通过国标协议或者sdk以及onvif的方法接入到EasyCVR后,使用云台控制的时会出现转动的延迟。其实这个延迟和当前选择的播放协议是有关联。由于默认的是选择flv的播放格式,一般......
  • 无涯教程-splice()函数
    splice()方法从数组中添加或删除元素。splice()-语法array.splice(index,howMany,[element1][,...,elementN]);index                   -开始更改数组的索引。howMany               -......
  • HDMI2.1之eARC简介-Dolby Atmos和DTS:X
    eARCeARC代表“enhancedAudioReturnChannel”(增强型音频返回通道),是一种用于音频传输的高级技术标准。它是HDMI(HighDefinitionMultimediaInterface,高清晰度多媒体接口)规范的一部分,旨在提供更高质量的音频传输和更多的功能。HDMI论坛提出HDMI2.1版时,一并新增的功能,其全名是E......
  • Scratch课程体系
    万奇机器人课程体系结构序号课程名课程概述1机器人动起来万奇机器人2迷宫探险万奇机器人3神奇的事件积木万奇机器人4绘制多边形万奇机器人5神奇的事件积木万奇机器人6绘制复杂图形万奇机器人7巡线导航万奇机器人 Scratch图形......
  • Adobe Photoshop 2024 v25.4 (macOS, Windows) - 照片和设计软件
    AdobePhotoshop2024v25.4(macOS,Windows)-照片和设计软件Acrobat、AfterEffects、Animate、Audition、Bridge、CharacterAnimator、Dimension、Dreamweaver、Illustrator、InCopy、InDesign、LightroomClassic、MediaEncoder、Photoshop、PremierePro、AdobeXD请......
  • Splunk Enterprise 9.2.0 (macOS, Linux, Windows) - 机器数据管理和分析
    SplunkEnterprise9.2.0(macOS,Linux,Windows)-机器数据管理和分析SIEM、全面的日志管理和分析平台请访问原文链接:https://sysin.org/blog/splunk-9/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org混合世界的数据平台快速、大规模地从可见性转向行动,以实现安......
  • Spring Boot无需Dockerfile创建Docker镜像三种方法
    SpringBoot无需Dockerfile创建Docker镜像三种方法        随着技术的发展,服务于终端用户请求的应用程序的部署方式也发生了变化。传统上,你需要用一台机器来部署应用程序,然后决定机器的配置,以保证应用程序的运行。容器的引入,即把整个软件堆栈打包成一个单一的实体并进......
  • CF1789F Serval and Brain Power 题解
    题目链接点击打开链接题目解法好有技巧性的题(感觉\(n\le80\)这个数据范围就很奇怪啊)首先可以发现\(k\le3\)是好做的,只要枚举断点,然后\(dp\)做一遍\(lcs\)即可,时间复杂度为\(O(n^{2k+1})\),不过严重跑不满,所以可以跑\(k=4\)的情况和\(k=2\)的情况是相同的,所以不......
  • 爬虫逆向案列---《某采购网ck反爬》
    网站接口:aHR0cDovL3d3dy55bmdwLmNvbS9wYWdlL3Byb2N1cmVtZW50L3Byb2N1cmVtZW50TGlzdC5odG1s首先分析ck是怎么生成的?访问首页获取通过js代码逆向获取其它页面返回访问接口,返回一段js代码,分析js代码获取ck所以,ck一般是在网页端的文档中的html可以观察到。刷新网址我们可以......