仅作记录,使用 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”

Selenium 4 中加入了对新版 Chromium Edge 的支持,但其仍处于 alpha 阶段,没有正式发布

如果使用最新稳定版的 Selenium 操作 Chromium Edge,则需要一些其他的操作

环境

  • Windows 1909
    • macOS 和 Linux 的使用方式相同
  • dotnet core 3.1
    • 其他语言可能有其他方法
  • Microsoft Edge (Chromium)
  • Edge Webdriver

参考

步骤

  1. 环境准备
    • 根据已安装的 Edge 版本下载对应的 Webdriver
    • 将 Webdriver(名称为 msedgedriver.exe)移动到与 msedge.exe 同级目录下(默认安装路径为 C:\Program Files (x86)\Microsoft\Edge\Application
    • 将 Webdriver 的路径加入环境变量
  2. 实例
    • 在项目中引入 Selenium.WebDriverMicrosoft.Edge.SeleniumTools
    • 使用 dotnet core 打开一个页面
      using Microsoft.Edge.SeleniumTools;
      //...
      var options = new EdgeOptions();
      options.UseChromium = true;
      // options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
      options.AddArgument("disable-gpu");
      options.AddArgument("--no-sandbox");
      options.AddArgument("--disable-dev-shm-usage");
      options.AddUserProfilePreference("profile.managed_default_content_settings.images", 2);
      
      string source = string.Empty;
      using (var edge = new EdgeDriver(options)) {
          edge.Navigate().GoToUrl(url);
          Thread.Sleep(5000);
          source = edge.PageSource;
          edge.Quit();
      }
      
      Console.WriteLine(source);
      //...

      注意不要引用 Selenium.WebDriver 官方的 OpenQA.Selenium.Edge,会与 Microsoft.Edge.SeleniumTools 包冲突

      前者的 EdgeOptions 内没有定义 UseChromium,无法正常使用新版 Edge

Continue reading “Dotnet Core Selenium 3 自动化 Chromium Edge”