OpenXML读取文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenXML读取文档相关的知识,希望对你有一定的参考价值。

所以我在我的资产文件夹中放了一个excel文件进​​行测试。我正在研究一个测试项目,看看代码是如何工作的。

如果我运行以下代码:

public async void TestAsync()
        {
            StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile sampleFile = await storageFolder.GetFileAsync(@"AssetsGenerator_Run_Data.xlsx");
        byte[] result;
        using (Stream stream = await sampleFile.OpenStreamForReadAsync())
        {
            using (var memoryStream = new MemoryStream())
            {

                stream.CopyTo(memoryStream);
                result = memoryStream.ToArray();
            }
        }
    }

我得到一个结果{byte [48347]}

但是在下面的代码中我得到一个错误:

public async static void CreateSpreadsheetWorkbook()
        {
            StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            storageFolder = await storageFolder.GetFolderAsync(@"Assets");

        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.
        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(storageFolder.Path, SpreadsheetDocumentType.Workbook);

        // Add a WorkbookPart to the document.
        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();

        // Add a WorksheetPart to the WorkbookPart.
        WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());

        // Add Sheets to the Workbook.
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        workbookpart.Workbook.Save();

        // Close the document.
        spreadsheetDocument.Close();
    }

错误是:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeinx86DebugAppXAssets' is denied.
  Source=System.IO.FileSystem
  StackTrace:
   at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.Packaging.ZipPackage..ctor(String path, FileMode packageFileMode, FileAccess packageFileAccess, FileShare share)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.CreateCore(String path)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(String path, SpreadsheetDocumentType type, Boolean autoSave)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Create(String path, SpreadsheetDocumentType type)
   at GeneratedCode.MainPage.<CreateSpreadsheetWorkbook>d__2.MoveNext() in C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeMainPage.xaml.cs:line 68

因此,文件夹可以在一种情况下访问,而不是在另一种情请帮我理解。

尝试了这段代码:

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(sampleFileString, true))

并收到一个错误:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeinx86DebugAppXAssetsGenerator_Run_Data.xlsx' is denied.
  Source=System.IO.FileSystem
  StackTrace:
   at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.Packaging.ZipPackage..ctor(String path, FileMode packageFileMode, FileAccess packageFileAccess, FileShare share)
   at System.IO.Packaging.Package.Open(String path, FileMode packageMode, FileAccess packageAccess, FileShare packageShare)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
   at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
   at GeneratedCode.MainPage.Spreadsheet(String[] args) in C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeMainPage.xaml.cs:line 105
   at GeneratedCode.MainPage..ctor() in C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeMainPage.xaml.cs:line 42
   at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage() in C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeobjx86DebugXamlTypeInfo.g.cs:line 178
   at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlUserType.ActivateInstance() in C:Usersxxxxxsource
eposGeneratedCodeGeneratedCodeobjx86DebugXamlTypeInfo.g.cs:line 333
答案

Message=Access to the path 'C:Usersxxxxxsource eposGeneratedCodeGeneratedCodeinx86DebugAppXAssets' is denied

问题是app's install directory是一个只读位置。您无法通过文件选择器访问安装目录。

通常,我们经常将文件存储在可以直接访问的localFolder中。

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

有关更多信息,请参阅Application data locations官方文档。

以上是关于OpenXML读取文档的主要内容,如果未能解决你的问题,请参考以下文章

dotnet OpenXML 读取形状轮廓线条样式序号超过主题样式列表数

读取word文档,暂时不能读取图片,案例代码

dotnet OpenXML 读取 PPT 形状边框定义在 Style 的颜色画刷

dotnet OpenXML 读取 PPT 形状边框定义在 Style 的颜色画刷

dotnet OpenXML 读取 PPT 形状边框定义在 Style 的颜色画刷

dotnet OpenXML 读取 PPT 形状边框定义在 Style 的颜色画刷