我想在我的 UWP 应用程序中创建一个 Microsoft Word 文档,并且我已经成功地在 APP Local 文件夹中创建了 doc 文件。但我想在用户选择的特定文件夹位置创建这个 doc 文件。
我的意思是用户浏览一个位置,然后在该位置创建 doc 文件。
这是我在 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();
}
尝试在特定位置创建 doc 文件时出现以下错误,如下所示:
错误
Message=媒体写保护:'D:\Training\TestDoc.docx'
代码
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();
}