[英]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 来执行此操作。
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
实例,您可以使用它。
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
}
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.请注意,即使使用此功能,您仍然需要使用StorageFile
和StorageFolder
API 执行所有文件操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.