【问题标题】:Assembly not loading after downloading package using NuGet使用 NuGet 下载包后程序集未加载
【发布时间】:2013-08-24 12:54:42
【问题描述】:

我创建了一个新的网络项目。使用 NuGet 包添加 EPPlus 库,然后从 codeplex EPPlus 页面添加以下代码。

    FileInfo newFile = new FileInfo(@"c:\sample1.xlsx");
    if (newFile.Exists)
    {
        newFile.Delete();  // ensures we create a new workbook
        newFile = new FileInfo(@"c:\sample1.xlsx");
    }
    using (ExcelPackage package = new ExcelPackage(newFile))
    {
        // add a new worksheet to the empty workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Inventory");
        //Add the headers
        worksheet.Cells[1, 1].Value = "ID";
        worksheet.Cells[1, 2].Value = "Product";
        worksheet.Cells[1, 3].Value = "Quantity";
        worksheet.Cells[1, 4].Value = "Price";
        worksheet.Cells[1, 5].Value = "Value";

        //Add some items...
        worksheet.Cells["A2"].Value = 12001;
        worksheet.Cells["B2"].Value = "Nails";
        worksheet.Cells["C2"].Value = 37;
        worksheet.Cells["D2"].Value = 3.99;

        worksheet.Cells["A3"].Value = 12002;
        worksheet.Cells["B3"].Value = "Hammer";
        worksheet.Cells["C3"].Value = 5;
        worksheet.Cells["D3"].Value = 12.10;

        worksheet.Cells["A4"].Value = 12003;
        worksheet.Cells["B4"].Value = "Saw";
        worksheet.Cells["C4"].Value = 12;
        worksheet.Cells["D4"].Value = 15.37;

        //Add a formula for the value-column
        worksheet.Cells["E2:E4"].Formula = "C2*D2";

        //Ok now format the values;
        using (var range = worksheet.Cells[1, 1, 1, 5])
        {
            range.Style.Font.Bold = true;
            range.Style.Fill.PatternType = ExcelFillStyle.Solid;
            range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
            range.Style.Font.Color.SetColor(Color.White);
        }

        worksheet.Cells["A5:E5"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
        worksheet.Cells["A5:E5"].Style.Font.Bold = true;

        worksheet.Cells[5, 3, 5, 5].Formula = string.Format("SUBTOTAL(9,{0})", new ExcelAddress(2, 3, 4, 3).Address);
        worksheet.Cells["C2:C5"].Style.Numberformat.Format = "#,##0";
        worksheet.Cells["D2:E5"].Style.Numberformat.Format = "#,##0.00";

        //Create an autofilter for the range
        worksheet.Cells["A1:E4"].AutoFilter = true;

        worksheet.Cells["A2:A4"].Style.Numberformat.Format = "@";   //Format as text
        worksheet.Cells.AutoFitColumns(0);  //Autofit columns for all cells

        // lets set the header text 
        worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Inventory";
        // add the page number to the footer plus the total number of pages
        worksheet.HeaderFooter.OddFooter.RightAlignedText =
            string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);
        // add the sheet name to the footer
        worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName;
        // add the file path to the footer
        worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;

        worksheet.PrinterSettings.RepeatRows = worksheet.Cells["1:2"];
        worksheet.PrinterSettings.RepeatColumns = worksheet.Cells["A:G"];

        // Change the sheet view to show it in page layout mode
        worksheet.View.PageLayoutView = true;

        // set some document properties
        package.Workbook.Properties.Title = "Invertory";
        package.Workbook.Properties.Author = "Jan Källman";
        package.Workbook.Properties.Comments = "This sample demonstrates how to create an Excel 2007 workbook using EPPlus";

        // set some extended property values
        package.Workbook.Properties.Company = "AdventureWorks Inc.";

        // set some custom property values
        package.Workbook.Properties.SetCustomPropertyValue("Checked by", "Jan Källman");
        package.Workbook.Properties.SetCustomPropertyValue("AssemblyName", "EPPlus");
        // save our new workbook and we are done!
        package.Save();

    }

问题:执行此代码后出现以下错误:

这是我的 csproj 文件中 EPPLus 的条目:

错误的 ..\packages\EPPlus.3.1.3.3\lib\net35\EPPlus.dll

这是 packages.config 的外观:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EPPlus" version="3.1.3.3" targetFramework="net35" />
</packages>

无法加载文件或程序集 'EPPlus,版本 = 3.0.0.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1' 或其之一 依赖项。定位程序集的清单定义不 匹配程序集引用。 (HRESULT 异常:0x80131040)

我不确定,我该怎么办??请帮忙。

谢谢

【问题讨论】:

  • 如果您查看项目的参考资料,您应该会看到 EPPlus 列在那里。查看其属性,版本应该与您在错误消息中看到的相匹配。还可以尝试update-package 以确保您拥有最新版本。还要确认解决方案下 .packages 文件夹中的存在和正确的版本号。如果一切顺利,我会发布您的 .csproj 和 packages.config 文件的相关部分。
  • @BradRem 感谢您的回复。我刚刚更新了我的问题以包含来自 csproj 的一些详细信息。这是我刚刚使用 NuGet 包下载的最新包。该项目没有任何其他参考。我创建了一个新项目,使用 NUGet 下载了 EPPlus lib 并进入了这个问题。

标签: c# dll nuget .net-assembly nuget-package


【解决方案1】:

无论听起来多么愚蠢,但我犯的错误是,我将解决方案命名为 EPPlus,并且系统在加载 DLL 时感到困惑:(。我创建了另一个以“xyz”为名称的解决方案,并且能够创建文件。

谢谢大家的帮助。

【讨论】:

  • 我做了同样的事情!!我更改了我的项目的名称,现在它可以工作了!!!你并不孤单!
  • i@user4566715:我希望你没有在这件事上浪费很多时间。
  • 好吧,这不是完全的损失,我现在不知道该怎么办。哈哈
【解决方案2】:

需要注意的一点是,异常告诉您,您的程序正在寻找与您在项目和包文件中安装和引用的版本不同的版本。

我怀疑问题是 CopyLocal 设置为 False 而应该设置为 True.

在 Visual Studio 中,打开项目的引用,选择 EPPlus 并显示其属性。确保 Copy Local 设置为 True.

【讨论】:

  • 我刚刚检查了 DLL 的属性。 CopyToLocal 选项设置为 true。谢谢你的回复。
猜你喜欢
  • 2017-01-08
  • 2019-12-02
  • 2017-10-22
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
相关资源
最近更新 更多