OpenXML读取文档

时间:2018-09-04 19:56:29

标签: c# uwp stream openxml

因此,我已将一个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

1 个答案:

答案 0 :(得分:2)

  

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  官方文件。