我在專案檔案中有一個包含以下內容的控制臺應用程式:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>basic_example</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<StartupObject>basic_example.SpreadSheetWriterExample</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
</ItemGroup>
</Project>
以及以下內容SpreadsheetWriterExample.cs:
using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using System.Collections.Generic;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text;
namespace basic_example
{
class SpreadSheetWriterExample
{
public static void CreateSpreadsheetWorkbook(string filepath)
{
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
Console.WriteLine("creating spreadsheet document");
try
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook))
{
spreadsheetDocument.Close();
}
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("DirectoryNotFoundException");
}
catch (IOException e)
{
Console.WriteLine("IOException");
}
}
static void Main(string[] args)
{
CreateSpreadsheetWorkbook("test.xlsx");
}
}
}
當我嘗試運行解決方案時,出現以下錯誤:
未處理的例外:System.IO.FileNotFoundException:無法加載檔案或程式集“DocumentFormat.OpenXml,Version=2.14.0.0,Culture=neutral,PublicKeyToken=8fb06cb64d019a17”或其依賴項之一。該系統找不到指定的檔案。
在 basic_example.SpreadSheetWriterExample.CreateSpreadsheetWorkbook(字串檔案路徑)
在 SpreadSheetWriterExample.cs:line 39 中的 basic_example.SpreadSheetWriterExample.Main(String[] args)
我真的不知道出了什么問題,因為我已經OpenXml在專案檔案中明確設定了對的參考。此外,我不能真正關注堆疊,因為例外只是告訴我它來自CreateSpreadSheetWorkbook方法內部的某個地方。
是什么導致了這個錯誤,我該如何解決?
uj5u.com熱心網友回復:
我已經復制了這個問題。我設法通過將以下內容添加到主專案來修復它PropertyGroup:
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
請注意,如果您的專案以 .NET5.0 或 .NET6.0 為目標,則不需要這樣做。
在 Nuget 的 GitHub 上似乎有一個未解決的問題。
這是CopyLocalLockFileAssemblies.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/512771.html
標籤:C#xml打开xml
上一篇:SearchView圖示和查詢文本在黑色背景下不可見
下一篇:將XSLT屬性轉換為元素
