把富文本里的html插入到word中。各位高人,求具体的实现方式或demo,不盛感激。

解决方案 »

  1.   

     public void ToWord(string html)
            {            string htmlSaveText = "<html><head><title></title></head><body>";
                htmlSaveText += html;
                htmlSaveText += "</body></html>";
                //建立html文件
                string path = System.Web.HttpContext.Current.Server.MapPath("/UploadFiles/Export/") + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".html";
                using (FileStream fs = File.Create(path))
                {
                    byte[] info = new UTF8Encoding(true).GetBytes(htmlSaveText);
                    fs.Write(info, 0, info.Length);
                }
                //File.AppendAllText(path, htmlSaveText, Encoding.Default);
                object Nothing = Missing.Value;
                wDoc.Paragraphs.Last.Range.InsertFile(path, ref Nothing, ref Nothing,
                                          ref Nothing, ref Nothing);
            }
    现在这个方法,可以在word段落最后加上html显示的内容,怎么在指定的地方加?
      

  2.   

    Document doc = new Document(Server.MapPath(tempFile));
                    DocumentBuilder builder = new DocumentBuilder(doc);                foreach (var key in dic.Keys)
                    {
                        var repStr = string.Format("{0}", key);                    if (key == "$合同内容$")
                        {
                            doc.Range.Replace(repStr, "", false, false);
                            Book book = doc.Range.Books[0];
                            builder.MoveToBook(book.Name);
                            builder.InsertHtml(dic[key]);
                            
                        }
                        else if (key == "$附件内容$")
                        {
                            doc.Range.Replace(repStr, "", false, false);
                            Book book = doc.Range.Books[0];
                            builder.MoveToBook(book.Name);
                            builder.InsertHtml(dic[key]);
                        }
                        else
                        {
                            doc.Range.Replace(repStr, dic[key], false, false);
                        }
                    }现在遇到个问题,二个定位到了一起。doc.Range.Books[1]肯定是错的。怎么正确分别定位