OpenXML 读取文档

OpenXML to Read Document

所以我在我的资产文件夹中放了一个 excel 文件用于测试。我正在做一个测试项目,只是为了看看代码是如何工作的。

如果我运行下面的代码:

public async void TestAsync()
        {
            StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile sampleFile = await storageFolder.GetFileAsync(@"Assets\Generator_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:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets' 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:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 68

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

也试过这段代码:

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

并且还收到一个错误:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets\Generator_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:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 105
   at GeneratedCode.MainPage..ctor() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\MainPage.xaml.cs:line 42
   at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\obj\x86\Debug\XamlTypeInfo.g.cs:line 178
   at GeneratedCode.GeneratedCode_XamlTypeInfo.XamlUserType.ActivateInstance() in C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\obj\x86\Debug\XamlTypeInfo.g.cs:line 333

Message=Access to the path 'C:\Users\xxxxx\source\repos\GeneratedCode\GeneratedCode\bin\x86\Debug\AppX\Assets' is denied.

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

一般情况下,我们经常将文件存放在localFolder中,可以直接访问。

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

更多请参考Application data locations 官方文档。