使用OpenXML将Word文件另存为HTML格式

时间:2014-02-08 15:36:27

标签: c# ms-word openxml

我有一个使用Microsoft.Office.Interop.Word以HTML格式保存Word文件的代码。

这是我的代码

void ConvertDocument(string fileName)
{
    object m = System.Reflection.Missing.Value;
    object oldFileName = (object)fileName;
    object readOnly = (object)false;
    ApplicationClass ac = null;

    try
    {
        // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.
        ac = new ApplicationClass();

        // Now we open the document.
        Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m,
             ref m, ref m, ref m, ref m, ref m, ref m);

        // Create a temp file to save the HTML file to. 
        tempFileName = GetTempFile("html");

        // Cast these items to object.  The methods we're calling 
        // only take object types in their method parameters. 
        object newFileName = (object)tempFileName;

        // We will be saving this file as HTML format. 
        object fileType = (object)WdSaveFormat.wdFormatHTML;

        // Save the file. 
        doc.SaveAs(ref newFileName, ref fileType,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m);

    }
    finally
    {
        // Make sure we close the application class. 
        if (ac != null)
            ac.Quit(ref readOnly, ref m, ref m);
    }
}    

string GetTempFile(string extension)
{
    // Uses the Combine, GetTempPath, ChangeExtension, 
    // and GetRandomFile methods of Path to 
    // create a temp file of the extension we're looking for. 
    return Path.Combine(Path.GetTempPath(),
        Path.ChangeExtension(Path.GetRandomFileName(), extension));
}

我想知道如何使用OpenXML执行此操作?

我用Google搜索但未找到相关解决方案

0 个答案:

没有答案