简体   繁体   English

在 UWP 中使用 OPEN-XML SDK 生成 Doc 文件

[英]Generate Doc file using OPEN-XML SDK in UWP

I want to Create a Microsoft Word Document in my UWP App and I've successfully created doc file in APP Local folder.我想在我的 UWP 应用程序中创建一个 Microsoft Word 文档,并且我已经在 APP Local 文件夹中成功创建了 doc 文件。 But i want to create this doc file in a specific folder location chosen by user.但我想在用户选择的特定文件夹位置创建此文档文件。

I mean user browse a location and then create doc file into that location.我的意思是用户浏览一个位置,然后在该位置创建 doc 文件。

Here is my code to create Doc file in App local Path:这是我在 App 本地路径中创建 Doc 文件的代码:

  string docFileName = "TestDoc.docx";
  Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

  string Filepath = localFolder.Path + "\\" + docFileName;

  using (var wordprocessingDocument = WordprocessingDocument.Create(file.Path, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
  {
      MainDocumentPart mainPart = wordprocessingDocument.AddMainDocumentPart();
      mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
      Body body = mainPart.Document.AppendChild(new Body());
      DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
      DocumentFormat.OpenXml.Wordprocessing.Run run = para.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Run());
      run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text("Hello !!! ."));
      wordprocessingDocument.MainDocumentPart.Document.Save();
  }

I've got below error while trying to create doc file in a specific location like below:尝试在特定位置创建 doc 文件时出现以下错误,如下所示:

Error错误

Message=The media is write protected : 'D:\\Training\\TestDoc.docx' Message=媒体被写保护:'D:\\Training\\TestDoc.docx'

Code代码

string path = @"D:\Training\TestDoc.docx";

using (var wordprocessingDocument = WordprocessingDocument.Create(path, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = wordprocessingDocument.AddMainDocumentPart();
    mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
    Body body = mainPart.Document.AppendChild(new Body());
    DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
    DocumentFormat.OpenXml.Wordprocessing.Run run = para.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Run());
    run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text("Hello !!! ."));
    wordprocessingDocument.MainDocumentPart.Document.Save();
}

Because UWP apps run in a sandbox, you cannot directly access filesystem paths using classic APIs.由于 UWP 应用在沙箱中运行,因此您无法使用经典 API 直接访问文件系统路径。 However, you can use Windows.Storage APIs to do so.但是,您可以使用Windows.Storage API 来执行此操作。

Let user choose the save file让用户选择保存文件

If you want to let the user choose the save file, you can use the FileSavePicker API.如果要让用户选择保存文件,可以使用FileSavePicker API。 This Docs page describes this in detail. 此文档页面详细描述了这一点。 Once done, the API will give you a StorageFile instance, which you can use.完成后,API 将为您提供一个StorageFile实例,您可以使用它。

Write in the file写入文件

As you cannot use the Create method which takes a file path as an argument, you need to use the Stream based one instead.由于您不能使用将文件路径作为参数的Create方法,因此您需要改用基于Stream方法。

To get a Stream instance from the file, add using System.IO;要从文件中获取Stream实例,请添加using System.IO; to the top of your code file and then do:到代码文件的顶部,然后执行以下操作:

using(var stream = await file.OpenStreamForWriteAsync())

With this stream you can then do:使用此流,您可以执行以下操作:

using (var wordprocessingDocument =
               WordprocessingDocument.Create(
                  stream, WordprocessingDocumentType.Document))
{
      //... your code
}

Note: Broad filesystem access注意:广泛的文件系统访问

If you really need to create the file at an arbitrary (not user selected) location, you can use the broadFileSystemAccess capability .如果您确实需要在任意(非用户选择)位置创建文件,则可以使用broadFileSystemAccess系统broadFileSystemAccess功能 This is however only for apps that really require it.然而,这仅适用于真正需要它的应用程序。 Note, that even with this capability, you still need to perform all file operations using the StorageFile and StorageFolder APIs.请注意,即使使用此功能,您仍然需要使用StorageFileStorageFolder API 执行所有文件操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用类似 json 格式的 C# open-xml SDK 从 Word 文档中获取文本? - How to get text out of Word Document using C# open-xml SDK in json like format? Open-XML 保存 word 文档产生损坏的文件 - Open-XML saving word document produces corrupted file 如何在c#中使用open xml sdk将doc转换为docx - How to convert doc to docx using open xml sdk in c# 将docx,doc转换为使用open xml sdk 2.0来打开xml,在转换doc时出错,可以与docx正常工作,其错误是文件已损坏数据 - converting docx,doc to open xml using open xml sdk 2.0 its working fine with docx when converting doc its error that file has corrupted data Open-XML中的Section元素最合适的等效项是什么? - What's the most appropriate equivalent of the Section element in Open-XML? 如何在 open-xml 文字处理文档中添加“锚点”图像? - How to add ''anchor'' images in open-xml wordprocessing document? 使用开放XML文件格式API的Word 2003(doc)文件 - Open word 2003 (doc) file using open xml file format API Microsoft Open SDK 2.0使用C#生成Excel文件 - Microsoft Open SDK 2.0 to generate excel file using c# 使用Open XML SDK替换Word文件中的书签文本 - Replace bookmark text in Word file using Open XML SDK 使用Open XML SDK将图从EA导出到文档文件 - Diagram export from EA to document file using Open XML SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM