【问题标题】:Saving custom Word document using FilePicker in Windows Store app在 Windows 应用商店应用程序中使用 FilePicker 保存自定义 Word 文档
【发布时间】: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


    【解决方案1】:

    我不知道任何可用于 Windows 运行时应用程序的 Word 文档组件(Microsoft 不提供,但可能存在我不知道的第三方组件)。

    您可以获得documentation on the docx 格式,对于简单的文本,它可能不会太复杂(我不确定),或者您可以使用 Word 可以打开的其他格式。

    如果您不需要格式化,我可能会坚持使用 txt。

    如果您需要少量格式化,那么 rtf 是一个不错的选择。自己生成非常简单,或者RichEditBox 可以导出 RTF 格式文本,然后您可以将其保存到 .doc 文件中并在 Word 中打开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2013-11-14
      相关资源
      最近更新 更多