【问题标题】:How do I add the NPOI library to a .NET Core 1.0 project?如何将 NPOI 库添加到 .NET Core 1.0 项目?
【发布时间】:2016-08-19 14:06:07
【问题描述】:

我想将 NPOI 库添加到我的 .NET Core 项目中。我想使用 xls 和 xlsx 文件。

【问题讨论】:

    标签: asp.net .net asp.net-core npoi


    【解决方案1】:

    将 NPOI 库添加到 .NET Core 项目有 4 个步骤。

    1. project.json 文件的 frameworks 属性下添加 net451 作为依赖项,并包含对 NPOI 库的引用:

      "frameworks": {
       "netcoreapp1.0": {
         "imports": [
           "dotnet5.6",
           "portable-net45+win8"
         ]
       },
       "net451": {
         "dependencies": {
            "NPOI": "2.2.1"
         }
        }
      },
      
    2. project.json 文件中,添加runtimes 作为顶级属性:

       "runtimes": {
       "win7-x64": {},
       "win7-x86": {},
       "osx.10.11-x64": {},
       "ubuntu.14.04-x64": {},
       "centos.7-x64": {},
       "rhel.7.2-x64": {},
       "debian.8-x64": {}
      }
      
    3. project.json 文件中,从顶级属性dependencies 中删除Microsoft.NETCore.App 属性

      "Microsoft.NETCore.App": {
        "version": "1.0.0",
        "type": "platform"
      },
      
    4. project.json 文件中,删除顶级frameworks 属性下的netcoreapp1.0 属性:

       "netcoreapp1.0": {
        "imports": [
          "dotnet5.6",
          "portable-net45+win8"
        ]
      },
      

    完整的project.json文件示例:

     {
      "userSecretsId": "aspnet-MyProjectName-xxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxx",
    
      "dependencies": {
        "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
        "Microsoft.AspNetCore.Diagnostics": "1.0.0",
        "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
        "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
        "Microsoft.AspNetCore.Mvc": "1.0.0",
        "Microsoft.AspNetCore.Razor.Tools": {
          "version": "1.0.0-preview2-final",
          "type": "build"
        },
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0",
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": {
          "version": "1.0.0",
          "type": "build"
        },
        "Microsoft.EntityFrameworkCore.Tools": {
          "version": "1.0.0-preview2-final",
          "type": "build"
        },
        "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": "1.0.0",
        "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Logging.Debug": "1.0.0",
        "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
        "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
        "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
          "version": "1.0.0-preview2-final",
          "type": "build"
        },
        "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
          "version": "1.0.0-preview2-final",
          "type": "build"
        },
        "MailKit": "1.4.1"
      },
    
      "tools": {
        "BundlerMinifier.Core": "2.0.238",
        "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
        "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
        "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
        "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
          "version": "1.0.0-preview2-final",
          "imports": [
            "portable-net45+win8"
          ]
        }
      },
    
      "frameworks": {
        "net451": {
          "dependencies": {
            "NPOI": "2.2.1"
          }
        }
      },
    
      "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true
      },
    
      "runtimeOptions": {
        "configProperties": {
          "System.GC.Server": true
        }
      },
    
      "publishOptions": {
        "include": [
          "wwwroot",
          "Views",
          "Areas/**/Views",
          "appsettings.json",
          "web.config"
        ]
      },
    
      "scripts": {
        "prepublish": [ "bower install", "dotnet bundle" ],
        "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
      },
    
      "runtimes": {
        "win7-x64": {},
        "win7-x86": {},
        "osx.10.11-x64": {},
        "ubuntu.14.04-x64": {},
        "centos.7-x64": {},
        "rhel.7.2-x64": {},
        "debian.8-x64": {}      
      }
    }
    

    【讨论】:

    • 这里要清楚.. 你已经把一个 .NET 核心应用程序变成了一个标准的 asp.net 应用程序,对吗? IOW,.net 核心功能已被删除。
    • @paqogomez 没错。当我这样做时,我想使用新的 .net core app 项目类型。这让我可以在单个项目中使用 DI 容器和 MVC/Web API 控制器。现在我相信有一个网络标准项目可以为您提供所有这些。但我没有这样做。
    【解决方案2】:

    NPOI 自 NPOI 2.4.1 以来一直支持 .NET 标准。

    【讨论】:

      猜你喜欢
      • 2016-11-01
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2023-03-06
      • 2021-05-22
      相关资源
      最近更新 更多