仅作记录,使用 EPPlus 组件,文字、图片、代码均来自参考链接

参考

示例

EPPlus 官方示例

In most cases you probably have some data that you want to move to an Excel spreadsheet, do some styling, maybe add a formula or a chart.

But before we get started, here’s something to keep in mind when you work with EPPlus:
Cell addresses, number formats and formulas are culture-insensitive, meaning things might look a little bit different when you write your code. This is the way OOXML is stored and is then translated too your culture when the workbook is opened in Excel.

Addresses are separated by a comma (,).
Example worksheet.Cells["A1:C1,C3"].Style.Font.Bold = true.

Numberformats use dot for decimal (.) and comma (,) for thousand separator.
Example worksheet.Cells["B2:B3"].Style.NumberFormat.Format = "#,##0.00";.

Formulas use comma (,) to separate parameters.
Example worksheet.Cells["C11"].Formula="SUBTOTAL(9,\"C1:C10\")";.

The first thing you do is to create an instance to the ExcelPackage class. To do that you first need to add a using directive to OfficeOpenXml namespace in the top of your file. This is the top namespace in EPPlus;

using OfficeOpenXml;

You can now reference the Excelpackage class directly for your class. The ExcelPackage class has few different constructors depending on what you want to do…

Continue reading “.NET Core 导入导出 Excel”

环境

  • .NET SDK 2.2

参考

步骤

  1. 在项目内使用NuGet添加AspNetCoreRateLimit包
    • 注意.NET Core版本必须为2.2及以上,2.1版本安装会报错
  2. 编辑Startup.cs,DI组件,并启用中间件
    //using ...
    using AspNetCoreRateLimit;
    
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddOptions();
        services.AddMemoryCache();
    
        services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
        services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
    
        services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
        services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
    
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        // ...   
        app.UseIpRateLimiting(); //注意在UseMvc()前注册
    
        app.UseMvc();
    }
  3. 编辑appsettings.json,设置具体的IP限制
    {
        ...
        "IpRateLimiting": {
            "EnableEndpointRateLimiting": true, //此字段设定为false则全局有效,会限制所有*的规则。例如:如果频率限制到每秒5次,则会纪录向任何端点的任何HTTP请求
                                                //这里设置为true可以对每个端点分别设置请求频率
            "StackBlockedRequests": false,
            "RealIpHeader": "X-Real-IP",
            "ClientIdHeader": "X-ClientId",
            "HttpStatusCode": 429,
            "GeneralRules": [
                { //设置hello路径的请求频率为每30秒3次请求,"*:"代表任何HTTP VERB,"/*"代表其后的参数
                    "Endpoint": "*:/hello/*",
                    "Period": "30s",
                    "Limit": 3
                }
            ]
        }
    }
    • 可以配置更多功能,例如白名单,更改计数方式,自定义返回内容等等,详细信息可以查看GIthub WIki
  4. 配置Action的路由
    namespace IP_restrict_test.Controllers {
        public class HomeController: Controller {
            //...
    
            [Route("/hello/")]
            public IActionResult Index() {
                return View();
            }
            
            //...
        }
    }
  5. 生成并运行,测试频率限制功能
    • 可以看到,连续打开或者刷新页面3次后,第4次打开页面会提示访问频率过快。