C# 将word文件转换为HTML,并在HTML中进行一些编辑后将其转换回docx

C# 将word文件转换为HTML,并在HTML中进行一些编辑后将其转换回docx,c#,office-interop,C#,Office Interop,我正在开发一个实用程序,用户可以将word文档上传到应用程序并分配给特定的人,分配后,特定的用户将进入应用程序并提出一些更改建议,以及自己进行一些格式化并保存。现在,任何人都可以下载最近更新的文件以供参考 我为实现这一目标所做的是—— 步骤1-当用户上载word文件(docx/doc)时,我的代码使用以下代码将其转换为HTML文件- object FileName; object MissingType = Type.Missing; object ReadOnly = false; objec

我正在开发一个实用程序,用户可以将word文档上传到应用程序并分配给特定的人,分配后,特定的用户将进入应用程序并提出一些更改建议,以及自己进行一些格式化并保存。现在,任何人都可以下载最近更新的文件以供参考

我为实现这一目标所做的是—— 步骤1-当用户上载word文件(docx/doc)时,我的代码使用以下代码将其转换为HTML文件-

object FileName;
object MissingType = Type.Missing;
object ReadOnly = false;
object IsVisible = false;
object DocumentFormat = 8; 

//Original file location where my application is uploaded the document for future reference
FileName = ConfigurationManager.AppSettings["WordFilePath"].ToString() + item.GUID + item.DocumentExtention;

//HTML file location where the application will save the file as html   
object HtmlDirectoryPath =   ConfigurationManager.AppSettings["HtmlFilePath"].ToString() + item.GUID +  ".htm";


//Conversion process
ApplicationClass ApplicationClass = new ApplicationClass();
ApplicationClass.Documents.Open(ref FileName, ref ReadOnly, ref MissingType,
                  ref MissingType, ref MissingType, ref     MissingType,
                  ref MissingType, ref MissingType, ref MissingType,
                  ref MissingType, ref IsVisible, ref MissingType,
                  ref MissingType, ref MissingType, ref MissingType,
                  ref MissingType);
ApplicationClass.Visible = false;
Document Document = ApplicationClass.ActiveDocument;
Document.SaveAs(ref HtmlDirectoryPath, ref DocumentFormat, ref MissingType,
                ref MissingType, ref MissingType, ref MissingType,
                ref MissingType, ref MissingType, ref MissingType,
                ref MissingType, ref MissingType, ref MissingType,
                ref MissingType, ref MissingType, ref MissingType,
                ref MissingType);
Document.Close(ref MissingType, ref MissingType, ref MissingType);
注意:-上面的代码将做什么,它将把文档文件转换成HTML,并将其与文件及其支持的文件一起保存到指定位置的文件夹中

步骤2:现在用户将更新文本,为此我使用rangy highligher。用户可以更新文本,将文本突出显示在此html文件中并将其保存回来。[一切正常]

第三步:现在是最困难的事情,任何用户都可以随时从服务器下载最新的文件。因此,我现在的工作是将HTML转换回word文件,并发出下载警报。 我曾尝试使用上述代码执行此操作,但它指出,applicationclass无法打开指定的文件,当我尝试使用bytearray读写新文件时,下载的文件显示“无法打开,某些数据已损坏”

注意:简单的word文件正在与HTML进行转换,但当涉及到复杂的word文档时,其中包含图像和其他内容,则会出现上述错误

string FilePathHTML = System.Configuration.ConfigurationManager.AppSettings["HTMLFilePath"] + ObjDocument.GUID + ".htm";
        //object FileHTMLPathTemp = System.Configuration.ConfigurationManager.AppSettings["HTMLFilePath"] + "temp\\" + ObjDocument.GUID + ".htm";
        string FilePathTemp = System.Configuration.ConfigurationManager.AppSettings["HTMLFilePath"] + "temp\\" + GUID + ".doc";


        StreamReader objReader;
        objReader = new StreamReader(FilePathHTML);
        string content = objReader.ReadToEnd();

        var path = System.Configuration.ConfigurationManager.AppSettings["LivePathWithContent"] + "documents/html/" + ObjDocument.GUID + "_files";
        content = content.Replace(ObjDocument.GUID + "_files", System.Configuration.ConfigurationManager.AppSettings["LivePathWithContent"] + "documents/html/" + ObjDocument.GUID + "_files");

        StreamWriter objWriter = new StreamWriter(FilePathTemp);
        objWriter.Write(content);
        objWriter.Close();

        string contentType = "";
        if (ObjDocument.DocumentExtention.ToLower().Replace(".docx",".doc") == ".doc")
        {
            contentType = "application/msword;charset=utf-8";
        }
        else
        {
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        }
        //byte[] arrBytes = FileToByteArray(FilePathHTML.ToString());
        //System.IO.File.WriteAllBytes(FilePathTemp.ToString(), arrBytes); // Requires System.IO

        return File(FilePathTemp.ToString(), contentType, ObjDocument.DocumentName.Replace(".docx", ".doc"));

请在您的位置找到cloud server上的示例文档文件

,我要做的第一件事是下载这些HTML文件中的一个,并尝试直接在Word中打开它,而无需对代码进行所有处理,以查看您是否遇到相同的错误。在提供任何意见之前,我还想查看将文档保存为HTML时在变量
DocumentFormat
中传递的值@Cindymister
对象文档格式=8也请在从Word位置转换后对HTML文件进行优化否,我不会下载您的文件并在我的系统上打开它们。我的意思是你应该试试。有什么需要进一步澄清的吗?我还在努力,没有答案@Cindymister-你需要更多的信息吗?在你的位置上,我要做的第一件事是下载这些HTML文件中的一个,并尝试直接在Word中打开它,而不需要你的代码所做的所有处理,以查看你是否会遇到同样的错误。在提供任何意见之前,我还想查看将文档保存为HTML时在变量
DocumentFormat
中传递的值
@Cindymister
对象文档格式=8也请在从Word位置转换后对HTML文件进行优化否,我不会下载您的文件并在我的系统上打开它们。我的意思是你应该试试。有什么需要进一步澄清的吗?我还在努力,没有答案@Cindymister-你需要更多的信息吗?