代码之家  ›  专栏  ›  技术社区  ›  SharpCoder

使用NuGet下载包后未加载程序集

  •  0
  • SharpCoder  · 技术社区  · 13 年前

    我创建了一个新的网络项目。添加了使用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=中性,PublicKeyToken=ea159fdaa78159a1'或其一种 依赖关系。找到的程序集的清单定义没有 匹配程序集引用。(HRESULT中的异常:0x80131040)

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

    非常感谢。

    2 回复  |  直到 13 年前
        1
  •  1
  •   SharpCoder    13 年前

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

    谢谢大家的帮助。

        2
  •  0
  •   Brad Rem    13 年前

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

    我怀疑问题在于 CopyLocal 设置为 False 应设置为的时间 True.

    在Visual Studio中,打开项目的引用,选择EPPlus并显示其财产。确保 Copy Local 设置为 没错。