Win7环境C#使用Word模板生成报告(二)

2012-03-10 12:13:49|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C#, Office套件|来源:唯设编程网

在文章Win7环境C#使用Word模板生成报告(一)中,对C#开发Word的配置环境进行了详细的介绍,本文将对其进行完善和补充,详细介绍C#使用Word模板生成报告的具体方法,包括通过模板创建新的Word文档,保存文档,使用文本内容替换Word模板中的书签,在模板中插入图片、段落、表格等。

1、通过模板创建新的Word文档

    /// <summary>
    /// 通过模板创建新文档
    /// </summary>
    /// <param name="filePath"></param>
    public void CreateNewDocument(string filePath) {
        killWinWordProcess();
        wordDoc = new ApplicationClass();
        wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
        wordApp.Visible = false;
        object missing = System.Reflection.Missing.Value;
        object templateName = filePath;
        wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing);
    }

2、保存生成的Word文档

    /// <summary>
    /// 保存新文件
    /// </summary>
    /// <param name="filePath"></param>
    public void SaveDocument(string filePath) {
        object fileName = filePath;
        object format = WdSaveFormat.wdFormatDocument;  //保存格式
        object miss = System.Reflection.Missing.Value;
        wordDoc.SaveAs(ref fileName, ref format, ref miss,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing);
        //关闭wordDoc,wordApp对象
        object SaveChanges = WdSaveOptions.wdSaveChanges;
        object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
        object RouteDocument = false;
        wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
        wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
    }

3、用实际的内容替换书签

    /// <summary>
    /// 在书签处插入值
    /// </summary>
    /// <param name="bookmark"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool InsertValue(string bookmark, string value) {
        object bkObj = bookmark;
        if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)) {
            wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
            wordApp.Selection.TypeText(value);
            return true;
        }
        return false;
    }

4、用实际的内容替换书签

    /// <summary>
    /// 在书签处插入值
    /// </summary>
    /// <param name="bookmark"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool InsertValue(string bookmark, string value) {
        object bkObj = bookmark;
        if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)) {
            wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
            wordApp.Selection.TypeText(value);
            return true;
        }
        return false;
    }
<12>
发表评论0条 】
网友评论(共?条评论)..
Win7环境C#使用Word模板生成报告(二)