整个导入WORD文档

这样我们在代码里面,就可以获取信息并指定这个Word模板了。

复制代码
InformationInfo info = BLLFactory.Instance.FindByID(id);
if (info != null)
{
string template = "~/Content/Template/政策法规模板.doc";
string templateFile = Server.MapPath(template);
Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);
复制代码
WORD模板的内容,可以使用文本替换方式,如下所示。

SetBookmark(ref doc, "Content", info.Content);
也可以使用书签BookMark方式查询替换,如下代码所示。

        Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[title];
        if (bookmark != null)
        {
            bookmark.Text = value;
        }

对于主体的HTML内容,这需要特殊对待,一般需要使用插入HTML的专用方式进行写入内容,否则就显示HTML代码了,使用专用HTML方法写入的内容,和我们在网页上看到的基本没有什么差异了。如下代码所示。

复制代码
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["Content"];
if (bookmark != null)
{
builder.MoveToBookmark(bookmark.Name);
builder.InsertHtml(info.Content);
}
复制代码
整个导入WORD文档的方法就是利用这些内容的整合,实现一个标准文档的生成,这种业务文档是固定模板的,因此很适合在实际业务中使用,比起使用其他方式自动生成的HTML文件或者文档,有更好的可塑性和美观性。

public FileStreamResult ExportWordById(string id)
{
if (string.IsNullOrEmpty(id)) return null;

        InformationInfo info = BLLFactory<Information>.Instance.FindByID(id);
        if (info != null)
        {
            string template = "~/Content/Template/政策法规模板.doc";
            string templateFile = Server.MapPath(template);
            Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);

            #region 使用文本方式替换
            //Dictionary<string, string> dictSource = new Dictionary<string, string>();
            //dictSource.Add("Title", info.Title);
            //dictSource.Add("Content", info.Content);
            //dictSource.Add("Editor", info.Editor);
            //dictSource.Add("EditTime", info.EditTime.ToString());
            //dictSource.Add("SubType", info.SubType);  
            //foreach (string name in dictSource.Keys)
            //{
            //    doc.Range.Replace(name, dictSource[name], true, true);
            //} 
            #endregion

            //使用书签方式替换
            SetBookmark(ref doc, "Title", info.Title);
            SetBookmark(ref doc, "Editor", info.Editor);
            SetBookmark(ref doc, "EditTime", info.EditTime.ToString());
            SetBookmark(ref doc, "SubType", info.SubType);
            
            //SetBookmark(ref doc, "Content", info.Content);
            //对于HTML内容,需要通过InsertHtml方式进行写入
            DocumentBuilder builder = new DocumentBuilder(doc);
            Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["Content"];
            if (bookmark != null)
            {
                builder.MoveToBookmark(bookmark.Name);
                builder.InsertHtml(info.Content);
            }                
            
            doc.Save(System.Web.HttpContext.Current.Response, info.Title, Aspose.Words.ContentDisposition.Attachment,
                Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

            HttpResponseBase response = ControllerContext.HttpContext.Response;
            response.Flush();
            response.End();
            return new FileStreamResult(Response.OutputStream, "application/ms-word");
        }
        return null;
    }

    private void SetBookmark(ref Aspose.Words.Document doc, string title, string value)
    {
        Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[title];
        if (bookmark != null)
        {
            bookmark.Text = value;
        }
    }
posted @ 2016-01-28 11:17  冰封的心  阅读(357)  评论(0编辑  收藏  举报