在百度找了一下资料,关于c#调用word中的文本对比,我的妈呀都写的是什么鬼东西,没有一个能用的,于是就去google了一下,果然有我想要的。
string processnameaa = "";
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
processnameaa += (p.ProcessName+ ";"+p.MainWindowTitle)+"\n";
if (p.ProcessName== "WINWORD")//找到word的进程
{
if (p.MainWindowTitle.Contains("结果.rtf")| p.MainWindowTitle.Contains("原始文本.rtf")| p.MainWindowTitle.Contains("最新文本.rtf"))
{
p.Kill();//杀死相关的进程
}
}
}
richTextBox3.SaveFile("最新文本.rtf");//将richtextbox中的文本保存为rtf格式
richTextBox4.SaveFile("原始文本.rtf");//将richtextbox中的文本保存为rtf格式
string cur_path = Directory.GetCurrentDirectory(); //获取当前工作目录
object mv = System.Reflection.Missing.Value;
object readonlyobj = false;
object myOriginalFile = cur_path+ "\\原始文本.rtf";
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref myOriginalFile, ref mv, ref readonlyobj, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv);
string myNewFile = cur_path+"\\最新文本.rtf";
object myResultFile = cur_path+"\\结果.rtf";
doc.ActiveWindow.View.ShowInsertionsAndDeletions = true;//设置显示插叙和删除痕迹
doc.TrackRevisions = true;
doc.ShowRevisions = false;
doc.PrintRevisions = true;
doc.Compare(myNewFile);
doc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
app.ActiveDocument.SaveAs(ref myResultFile, ref mv, ref readonlyobj, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv);
app.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges);
Process.Start(cur_path + "\\结果.rtf");
通过实践发现,保存为rtf的时候,当word版本是2007时,会提示转化rtf,这个就很烦,针对这个问题我优化了一下,关键一点是通过document.activewindow可以设置审阅的属性,doc.ActiveWindow.View.ShowInsertionsAndDeletions = true;
读者可以根据各种需要设置word中的各种属性,在查文档的时候光查询到审阅功能中的接口,不知道通过document.activewindow来设置,个人感觉这个地方是个坑。
progressBar2.Value = 0;
string processnameaa = "";
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
processnameaa += (p.ProcessName+ ";"+p.MainWindowTitle)+"\n";
if (p.ProcessName== "WINWORD")
{
if (p.MainWindowTitle.Contains("结果.doc")| p.MainWindowTitle.Contains("原始文本.doc")| p.MainWindowTitle.Contains("最新文本.doc"))
{
p.Kill();//杀死相关的进程
}
}
}
progressBar2.Value = 10;
string cur_path = Directory.GetCurrentDirectory();
//生成word文档
Microsoft.Office.Interop.Word.Application app1 = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc1 = new Microsoft.Office.Interop.Word.Document();
Microsoft.Office.Interop.Word.Document doc2 = new Microsoft.Office.Interop.Word.Document();
doc1 = app1.Documents.Add();
doc1.Content.Text = richTextBox3.Text;//读取最新文本
doc1.SaveAs(cur_path + "\\最新文本.doc");
doc1.Close();
doc2= app1.Documents.Add();
doc2.Content.Text = richTextBox4.Text;//读取最新文本
doc2.SaveAs(cur_path + "\\原始文本.doc");
doc2.Close();
app1.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges);
progressBar2.Value = 30;
//string cur_path = Directory.GetCurrentDirectory();
object mv = System.Reflection.Missing.Value;
object readonlyobj = false;
object myOriginalFile = cur_path+ "\\原始文本.doc";
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref myOriginalFile, ref mv, ref readonlyobj, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv);
//v.ShowRevisionsAndComments = true;
//v.ShowComments = true;
//v.ShowFormatChanges = true;
//v.ShowInkAnnotations = true;
//v.ShowInsertionsAndDeletions = true;
doc.ActiveWindow.View.ShowInsertionsAndDeletions = true;//可以设置审阅方式,其中插入内容和删除内容都显示出来,有的电脑默认是没有的。
string myNewFile = cur_path+"\\最新文本.doc";
object myResultFile = cur_path+"\\结果.doc";
doc.TrackRevisions = true;
doc.ShowRevisions = false;
doc.PrintRevisions = true;
doc.Compare(myNewFile);
doc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
app.ActiveDocument.SaveAs(ref myResultFile, ref mv, ref readonlyobj, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv, ref mv);
app.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges);
Process.Start(cur_path + "\\结果.doc");
progressBar2.Value = 100;