asp.net 替换word内容 ,里边含图片 替换后样式 和图片不能丢失Microsoft.Office.Interop.Word.Application app =new Microsoft.Office.Interop.Word.Application();
object nullobj = System.Reflection.Missing.Value;
object file = filePath;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj); 
 doc.Content.Text = doc.Content.Text.Replace(strOld, strNew);
 
 
doc.Close(ref nullobj, ref nullobj, ref nullobj);
//关闭应用
app.Quit(ref nullobj, ref nullobj, ref nullobj);
app=null;
GC.Collect();
我用的这个方法,但是替换后样式变了,图片也没了,
请教高手有没有其他方法

解决方案 »

  1.   

    有种方法可以将word转换成html页面,不知道能不能满足lz的要求,
      

  2.   

    Microsoft.Office.Interop.Word.Application app =new Microsoft.Office.Interop.Word.Application();
    object MissingValue = Type.Missing; 
    object file = Server.MapPath("Word表头.doc");
    Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
    ref file, ref MissingValue, ref MissingValue,
    ref MissingValue, ref MissingValue, ref MissingValue,
    ref MissingValue, ref MissingValue, ref MissingValue,
    ref MissingValue, ref MissingValue, ref MissingValue,
    ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue); 
    //doc.Content.Find.Text = strOldText ;
    object FindText,  ReplaceWith, Replace ;// 
    string strold="$表头标题;$标题头;$工程名称;$报价单号;$报价日期;$合计";
    string[] str_old=strold.Split(';');
    string strnew="表头标题00;标题头01;工程名称02;报价单号03;报价日期04;合计05";
    string[] str_new=strnew.Split(';');
    for(int i=0;i<str_old.Length;i++)
    {
    doc.Content.Find.Text =str_old[i];// strOldText ;
    FindText =str_old[i];// strOldText ;//要查找的文本
    ReplaceWith =str_new[i];// strNewText ;//替换文本
    Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll ;/**//*wdReplaceAll - 替换找到的所有项。
                                                          * wdReplaceNone - 不替换找到的任何项。
                                                        * wdReplaceOne - 替换找到的第一项。
                                                        * */
    doc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
    if (doc.Content.Find.Execute(
    ref FindText,ref MissingValue,
    ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue,ref MissingValue,
    ref ReplaceWith,ref Replace,
    ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue))
    {

    }
    }
     
    doc.Save();
    doc.Close(ref MissingValue, ref MissingValue, ref MissingValue);
    //关闭应用
    app.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
    app=null;
    GC.Collect(); 问题解决了,上边的方法。