【发布时间】:2015-01-01 16:36:02
【问题描述】:
我正在制作一个 Windows 应用商店应用程序,我希望允许按下“导出到 Word”按钮的用户将他们输入到应用程序中的所有数据显示在 Word 文档中并保存到所需位置在他们的电脑上。下面的代码是一段测试代码,几乎可以满足我的要求,但是在保存文档并使用 Word 而不是应用程序打开它后,由于文件明显损坏,它无法打开文件。但是,当您在记事本中打开它时,文本会按我想要的方式显示。
private async void exportToWord_Click(object sender, RoutedEventArgs e)
{
await ExportToWord();
}
private async Task ExportToWord()
{
// Create the picker object and set options
Windows.Storage.Pickers.FileSavePicker savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Word", newList<string>{".docx"});
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "Test";
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
MessageDialog mD;
if (file != null)
{
// Prevent updates to the remote version of the file until we finish
// making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);
// write to file
await Windows.Storage.FileIO.WriteTextAsync(file, createContentsOfFile());
// Let Windows know that we're finished changing the file so the other
// app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.Provider.FileUpdateStatus updateStatus = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (updateStatus == Windows.Storage.Provider.FileUpdateStatus.Complete)
{
mD = newMessageDialog("Connect exported to:" + file, "Export Successful");
}
else
{
mD = newMessageDialog("Could not save file. Try again", "Export Unsuccessful");
}
}
else
{
mD = newMessageDialog("Operation canceled because the file could not be found. Try again", "Export Unsuccessful");
}
await mD.ShowAsync();
}
private string createContentsOfFile()
{
return "Testing...";
}
我认为问题在于我将纯文本输出到 Word 文档,但它需要采用某种格式才能正确输出并显示在 Word 文档中。有没有办法在 Windows 应用商店应用中做到这一点?
任何帮助将不胜感激。
【问题讨论】:
标签: windows ms-word windows-store-apps