当前位置: 首页 > 工具软件 > Npoi.Mapper > 使用案例 >

记录使用Npoi.Mapper导出文件

黄凌龙
2023-12-01
namespace 导出导入.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
        [HttpGet(Name = "GetWeatherForecast")]
        public ActionResult Get()
        {
            List<Table> students = new List<Table>
            {
            new Table{ Id = 1,Name="夫子",Sex="男",BirthDay=new DateTime(1999,10,11),Type="语文",Number=96 },
          new Table{ Id = 2,Name="余帘",Sex="女",BirthDay=new DateTime(1999,12,12) ,Type="语文",Number=91 },
          new Table{ Id = 3,Name="李慢慢",Sex="男",BirthDay=new DateTime(1999,11,11) ,Type="语文",Number=92 },
          new Table{ Id = 4,Name="叶红鱼",Sex="女",BirthDay=new DateTime(1999,10,10) ,Type="语文",Number=96 }
            };
            var mapper = new Mapper();
            MemoryStream stream = new MemoryStream();
            //将students集合生成的Excel直接放置到Stream中
            mapper.Save(stream, students, "sheet1", overwrite: true, xlsx: true);
            return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "C:\\Users\\cjkja\\Desktop\\file\\Students.xlsx");
        }
        /// <summary>
        /// 导入
        /// </summary>
        //public void tolead() {

        //    //Excel文件的路径
        //    var mapper = new Mapper("Students.xlsx");
        //    //读取的sheet信息
        //    var studentRows = mapper.Take<Table>("sheet1");
        //    foreach (var row in studentRows)
        //    {
        //        //映射的数据保留在value中
        //        Table student = row.Value;
        //        Console.WriteLine($"姓名:[{student.Name}],学号:[{student.Id}],性别:[{student.Sex}],生日:[{student.BirthDay:yyyy-MM-dd}]");
        //    }

        //}
        //  [HttpGet(Name = "GetWeatherForecast")]
        //保存到本地
        //  public IEnumerable<WeatherForecast> Get()
        //  {
        //      List<Table> students = new List<Table>
        //{
        //    new Table{ Id = 1,Name="夫子",Sex="男",BirthDay=new DateTime(1999,10,11),Type="语文",Number=96 },
        //    new Table{ Id = 2,Name="余帘",Sex="女",BirthDay=new DateTime(1999,12,12) ,Type="语文",Number=91 },
        //    new Table{ Id = 3,Name="李慢慢",Sex="男",BirthDay=new DateTime(1999,11,11) ,Type="语文",Number=92 },
        //    new Table{ Id = 4,Name="叶红鱼",Sex="女",BirthDay=new DateTime(1999,10,10) ,Type="语文",Number=96 }
        //};
        //      //声明mapper操作对象
        //      var mapper = new Mapper();
        //      //第一个参数为导出Excel文件地址
        //      //第二个参数为Excel数据来源
        //      //第三个参数为导出的Sheet名称
        //      //overwrite参数如果是要覆盖已存在的Excel或者新建Excel则为true,如果在原有Excel上追加数据则为false
        //      //xlsx参数是用于区分导出的数据格式为xlsx还是xls
        //      mapper.Save("C:\\Users\\cjkja\\Desktop\\file\\Students.xlsx", students, "sheet1", overwrite: true, xlsx: true);
        //      Console.WriteLine("执行完成");
        //      return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        //      {
        //          Date = DateTime.Now.AddDays(index),
        //          TemperatureC = Random.Shared.Next(-20, 55),
        //          Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        //      })
        //      .ToArray();
        //  }

    }

    public class Table
    {
        public int Id { get; set; }
        [Column("姓名")]
        public string Name { get; set; }
        [Column("性别")]
        public string Sex { get; set; }
        [Column("科目")]
        public string Type { get; set; }
        [Column("分数")]
        public int Number { get; set; }
        [Column("生日")]
        public DateTime BirthDay { get; set; }
    }
}
 类似资料: