OkTool.Excel 1.0.2.16

dotnet add package OkTool.Excel --version 1.0.2.16
NuGet\Install-Package OkTool.Excel -Version 1.0.2.16
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OkTool.Excel" Version="1.0.2.16" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OkTool.Excel --version 1.0.2.16
#r "nuget: OkTool.Excel, 1.0.2.16"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OkTool.Excel as a Cake Addin
#addin nuget:?package=OkTool.Excel&version=1.0.2.16

// Install OkTool.Excel as a Cake Tool
#tool nuget:?package=OkTool.Excel&version=1.0.2.16

数据导入

  • 1 导入解析为JSON

    var array= OkTool.Excel.ExcelUtil.ReadMultipartFile(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
  • 2 导入解析为对象(基础)

    public class User
    {
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
  • 3 导入解析为对象(字段自动映射)

    public class User
    {
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
    
  • 4 导入解析为对象(获取行号)

    public class User
    {
        public int RowNum { get; set; }
    
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
    
  • 5 导入解析为对象(获取原始数据)

    public class User
    {
        public string RowData { get; set; }
    
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
  • 6 导入解析为对象(获取错误提示)

    public class User
    {
        public string RowTips { get; set; }
    
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
    
  • 7 导入解析为对象(限制字段长度)

    public class User
    {
        public string RowTips { get; set; }
    
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话",MaxLength =11)]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
    
  • 8 导入解析为对象(必填字段验证)

    public class User
    {
        public string RowTips { get; set; }
    
        [ExcelImport("姓名")]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话",Required =true)]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
  • 9 导入解析为对象(数据唯一性验证)

    public class User
    {
        public string RowTips { get; set; }
    
        [ExcelImport("姓名",Unique =true)]
        public string Name { get; set; }
    
        [ExcelImport("年龄")]
        public int Age { get; set; }
    
        [ExcelImport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelImport("电话")]
        public string Tel { get; set; }
    
        [ExcelImport("城市")]
        public string City { get; set; }
    }
    var array = OkTool.Excel.ExcelUtil.ReadMultipartFile<User>(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    
  • 10 导入多Sheet页

    var array = OkTool.Excel.ExcelUtil.ReadFileManySheet(new MultipartFile { Stream = new FileStream("Data/Excel/122.xlsx", FileMode.Open, FileAccess.Read), FileName = "122.xlsx" });
    

数据导出

  • 1 动态导出(基础)

    List<object> head = new List<object>() { "姓名", "年龄", "性别", "头像" };
    List<object> user1 = new List<object>() { "诸葛亮", 60, "男", "https://profile-avatar.csdnimg.cn/d5c930c1a787417984d58d9ca5ad2692_wangyinlon.jpg" };
    List<object> user2 = new List<object>() { "大乔", 28, "女", "/d5c930c1a787417984d58d9ca5ad2692_wangyinlon.jpg" };
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user1);
    sheetDataList.Add(user2);
    OkTool.Excel.ExcelUtil.Export(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_base.xlsx"), "用户表", sheetDataList);
    
  • 2 动态导出(导出图片)

    List<object> head = new List<object>() { "姓名", "年龄", "性别", "头像" };
    //支持本地图片和网络图片
    List<object> user1 = new List<object>() { "诸葛亮", 60, "男", new Uri("https://profile-avatar.csdnimg.cn/d5c930c1a787417984d58d9ca5ad2692_wangyinlon.jpg") };
    List<object> user2 = new List<object>() { "大乔", 28, "女", new Uri("/d5c930c1a787417984d58d9ca5ad2692_wangyinlon.jpg") };
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user1);
    sheetDataList.Add(user2);
    OkTool.Excel.ExcelUtil.Export(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_base.xlsx"), "用户表", sheetDataList);
    
  • 3 动态导出(实现下拉列表)

    List<object> head = new List<object>() { "姓名", "年龄", "性别", "城市" };
    List<object> user1 = new List<object>() { "诸葛亮", 60, "男", "成都" };
    List<object> user2 = new List<object>() { "大乔", 28, "女", "上海" };
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user1);
    sheetDataList.Add(user2);
    
    Dictionary<int,List<string>> selectMap= new Dictionary<int,List<string>>();
    //设置下拉列表(键为第几列(从0开始),为下拉列数据)
    selectMap.Add(2, new List<string>() { "男","女"});
    selectMap.Add(3, new List<string>() { "北京","上海","成都","重庆"});
    
    OkTool.Excel.ExcelUtil.Export(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_base.xlsx"), "用户表", sheetDataList, selectMap);
    
  • 4 动态导出(横向合并)

    List<object> head = new List<object>() { "姓名", "年龄", "性别", "地址", OkTool.Excel.ExcelUtil.COLUMN_MERGE };
    List<object> user1 = new List<object>() { "诸葛亮", 60, "男", "蜀国", "卧龙岗" };
    List<object> user2 = new List<object>() { "大乔", 28, "女", "东吴", "小聚" };
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user1);
    sheetDataList.Add(user2);
    OkTool.Excel.ExcelUtil.Export(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_merge_x.xlsx"), "111", sheetDataList);
    
  • 5 动态导出(纵向合并)

    List<object> head = new List<object>() { "归属地", "姓名", "年龄", "性别", "地址", OkTool.Excel.ExcelUtil.COLUMN_MERGE };
    List<object> user1 = new List<object>() { "蜀国", "诸葛亮", 60, "男", "蜀国", "卧龙岗" };
    List<object> user2 = new List<object>() { "吴国", "大乔", 29, "女", "东吴", "小聚" };
    List<object> user3 = new List<object>() { OkTool.Excel.ExcelUtil.ROW_MERGE, "小乔", 28, "女", "东吴", "小聚" };
    
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user1);
    sheetDataList.Add(user2);
    sheetDataList.Add(user3);
    
    OkTool.Excel.ExcelUtil.Export(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_merge_y.xlsx"), "111", sheetDataList);
    
  • 6 导出模板(基础)

    public class User
    {
        [ExcelExport("姓名")]
        public string Name { get; set; }
    
        [ExcelExport("年龄")]
        public int Age { get; set; }
    
        [ExcelExport("性别")]
        public int Sex { get; set; }
    
        [ExcelExport("电话")]
        public string Tel { get; set; }
    
        [ExcelExport("城市")]
        public string City { get; set; }
    }
    
    OkTool.Excel.ExcelUtil.ExportTemplate<User>(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), "111");
    
  • 7 导出模板(附示例数据)

    public class User
    {
        [ExcelExport("姓名",Example ="银龙")]
        public string Name { get; set; }
    
        [ExcelExport("年龄",Example ="18")]
        public int Age { get; set; }
    
        [ExcelExport("性别",Example ="女")]
        public int Sex { get; set; }
    
        [ExcelExport("电话",Example ="1888888")]
        public string Tel { get; set; }
    
    
        [ExcelExport("城市",Example ="上海")]
        public string City { get; set; }
    
    }
    OkTool.Excel.ExcelUtil.ExportTemplate<User>(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), "111",true);
    
    
  • 8 按对象导出(基础)

    public class User
    {
        [ExcelExport("姓名")]
        public string Name { get; set; }
    
        [ExcelExport("年龄")]
        public int Age { get; set; }
    
        [ExcelExport("性别")]
        public int Sex { get; set; }
    
        [ExcelExport("电话")]
        public string Tel { get; set; }
    
        [ExcelExport("城市")]
        public string City { get; set; }
    }
    
    List<User> userList = new List<User>();
    userList.Add(new User()
    {
        Name = "张三",
        Age = 18,
        Sex = 1,
        City = "河北",
        Tel = "13999"
    }); userList.Add(new User()
    {
        Name = "李四",
        Age = 25,
        Sex = 2,
        City = "河北",
        Tel = "13999"
    });
    
    OkTool.Excel.ExcelUtil.Export<User, User>(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), "111", userList);
    
  • 9 按对象导出(数据映射)

    public class User
    {
        [ExcelExport("姓名")]
        public string Name { get; set; }
    
        [ExcelExport("年龄")]
        public int Age { get; set; }
    
        [ExcelExport("性别",Kv ="1-男;2-女")]
        public int Sex { get; set; }
    
        [ExcelExport("电话")]
        public string Tel { get; set; }
    
        [ExcelExport("城市")]
        public string City { get; set; }
    
    }
    List<User> userList = new List<User>();
    userList.Add(new User()
    {
        Name = "张三",
        Age = 18,
        Sex = 1,
        City = "河北",
        Tel = "13999"
    }); userList.Add(new User()
    {
        Name = "李四",
        Age = 25,
        Sex = 2,
        City = "河北",
        Tel = "13999"
    });
    
    OkTool.Excel.ExcelUtil.Export<User, User>(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), "111", userList);
    
  • 10 按对象导出(调整表头顺序)

    public class User
    {
        [ExcelExport("姓名",Sort =1)]
        public string Name { get; set; }
    
        [ExcelExport("年龄",Sort =3)]
        public int Age { get; set; }
    
        [ExcelExport("性别",Kv ="1-男;2-女",Sort =5)]
        public int Sex { get; set; }
    
        [ExcelExport("电话",Sort =2)]
        public string Tel { get; set; }
    
        [ExcelExport("城市",Sort =4)]
        public string City { get; set; }
    }
      OkTool.Excel.ExcelUtil.Export<User, User>(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), "111", userList);
    
  • 11 多Sheet页导出

    List<object> head = new List<object>() { "姓名", "年龄", "性别", "地址", OkTool.Excel.ExcelUtil.COLUMN_MERGE };
    List<object> user11 = new List<object>() { "诸葛亮", 60, "男", "蜀国", "卧龙岗" };
    List<object> user12 = new List<object>() { "大乔", 28, "女", "东吴", "小聚" };
    List<List<object>> sheetDataList = new List<List<object>>();
    sheetDataList.Add(head);
    sheetDataList.Add(user11);
    sheetDataList.Add(user12);
    
    List<object> head2 = new List<object>() { "姓名", "年龄", "性别", "地址", OkTool.Excel.ExcelUtil.COLUMN_MERGE };
    List<object> user21 = new List<object>() { "诸葛亮", 60, "男", "蜀国", "卧龙岗" };
    List<object> user22 = new List<object>() { "大乔", 28, "女", "东吴", "小聚" };
    List<List<object>> sheetDataList2 = new List<List<object>>();
    sheetDataList2.Add(head2);
    sheetDataList2.Add(user21);
    sheetDataList2.Add(user22);
    
    Dictionary<string, List<List<object>>> sheets = new Dictionary<string, List<List<object>>>();
    sheets.Add("用户1", sheetDataList);
    sheets.Add("用户2", sheetDataList2);
    
    OkTool.Excel.ExcelUtil.ExportManySheet(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "export_object_base.xlsx"), sheets);
    
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2.16 76 5/1/2024
1.0.2.15 68 5/1/2024