我正在开发一个实用程序,用户来到该应用程序并将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);
注意: - 上面代码将执行的操作,它会将doc文件转换为HTML并将文件及其支持的文件保存到指定位置。 Just like this
第2步:现在用户将更新文本,为此我正在使用rangy highligher。用户可以更新文本,将文本突出显示到这个html文件中并保存回来。[这一切都很好]
步骤3:现在遇到最困难的事情,任何用户都可以随时下载并从服务器下载最新文件。所以我现在的工作是将HTML转换回word文件并发布下载警报。 我尝试使用上面的代码执行此操作,但它说,applicationclass无法打开指定的文件,当我尝试使用bytearray读取和写入新文件时,下载的文件显示"无法打开,某些数据已损坏"
注意:简单的word文件正在转换为HTML和从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"));
请在云端服务器上找到示例DOC文件https://drive.google.com/folderview?id=0B_wGZDVWpHQcaEhMMWpMNXFMT1U&usp=sharing