利用dotm模板动态添加web报表数据

原创 2015年11月10日 14:17:28

1. .dot是word 2007的模板,.dotm是word 2007以上的模板。

2. 主模块代码.

 CCWordApp test;
 test = new CCWordApp();
 test.Open(temppath);


test.GotoBookMark(BookMarkName);
 test.InsertText(VOY);

test.GotoBookMark(BookMarkName2);

 test.InsertImage(barFileName, 140, 40);

test.SaveAsPDF(exportFilefullname);

 test.Quit();

3.Word类,注意,oWordApplic.Selection.InlineShapes.AddPicture这个功能属于宏功能,所以需要用到.dotm模板,要不会报错,当时纠结这个bug 很久。

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using BarcodeLib;
using Microsoft.Office.Interop.Word;
/// <summary>
///CCWordApp 的摘要说明
/// </summary>
public class CCWordApp
{
    private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic; // a reference to Word application 引用Word应用程序  
    private Microsoft.Office.Interop.Word.Document oDoc;                    // a reference to the document 引用文档       
    public CCWordApp()
    {
        // activate the interface with the COM object of Microsoft Word  
        //激活与Microsoft Word的COM对象的接口  
        oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
    }

   /// <summary>  
    /// 插入图片  
    /// </summary>  
    /// <param name="picPath"></param>  
    public void InsertPicture(string picPath)
    {
        object missing = System.Reflection.Missing.Value;
        oWordApplic.Selection.InlineShapes.AddPicture(picPath, ref missing, ref missing, ref missing);
    }
    // Open a file (the file must exists) and activate it  打开一个文件(该文件必须存在),并激活它  
    public void Open(string strFileName)
    {
        object fileName = strFileName;
        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value;
        oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
              ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);  
  
        oDoc.Activate();
    }
    // Open a new document打开一个新文档  
    public void Open()
    {
        object missing = System.Reflection.Missing.Value;
        oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        oDoc.Activate();
    }

    public void Quit()
    {
        object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
        object missing = System.Reflection.Missing.Value;
        if (oDoc != null)
            oDoc.Close(ref saveChange, ref missing, ref missing);
        oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
    }


    public void Save()
    {
        oDoc.Save();
    }


    public void SaveAs(string strFileName)
    {
        object missing = System.Reflection.Missing.Value;
        object fileName = strFileName;
        oDoc.SaveAs(ref fileName, 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);   
    }

    // Save the document in HTML format 以HTML格式保存文档  
    public void SaveAsHtml(string strFileName)
    {
        object missing = System.Reflection.Missing.Value;
        object fileName = strFileName;
        object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
        oDoc.SaveAs(ref fileName, ref Format, 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);
    }
    public void SaveAsPDF(string strFileName)
    {
        object missing = System.Reflection.Missing.Value;
        object fileName = strFileName;
        object fileformat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
        oDoc.SaveAs(ref fileName, ref fileformat, 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);
    }


    public void InsertText(string strText)
    {
        oWordApplic.Selection.TypeText(strText);
        oWordApplic.Selection.SelectCurrentFont();
    }

    public void InsertLineBreak()
    {
        oWordApplic.Selection.TypeParagraph();
    }
    public void InsertLineBreak(int nline)
    {
        for (int i = 0; i < nline; i++)
            oWordApplic.Selection.TypeParagraph();
    }

    // Change the paragraph alignement 更改段落对齐键相  
    public void SetAlignment(string strType)
    {
        switch (strType)
        {
            case "Center":
                oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                break;
            case "Left":
                oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                break;
            case "Right":
                oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                break;
            case "Justify":
                oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
                break;
        }
    }

    // if you use thif function to change the font you should call it again with 如果您使用此功能来改变字体,你应该再次调用它   
    // no parameter in order to set the font without a particular format 为了不带参数设置字体没有特定的格式  
    public void SetFont(string strType)
    {
        switch (strType)
        {
            case "Bold":
                oWordApplic.Selection.Font.Bold = 1;
                break;
            case "Italic":
                oWordApplic.Selection.Font.Italic = 1;
                break;
            case "Underlined":
                oWordApplic.Selection.Font.Subscript = 0;
                break;
        }
    }


    // disable all the style  禁用所有的风格  
    public void SetFont()
    {
        oWordApplic.Selection.Font.Bold = 0;
        oWordApplic.Selection.Font.Italic = 0;
        oWordApplic.Selection.Font.Subscript = 0;
    }


    public void SetFontName(string strType)
    {
        oWordApplic.Selection.Font.Name = strType;
    }

    public void SetFontSize(int nSize)
    {
        oWordApplic.Selection.Font.Size = nSize;
    }

    public void InsertPagebreak()
    {
        // VB : Selection.InsertBreak Type:=wdPageBreak  
        object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
        oWordApplic.Selection.InsertBreak(ref pBreak);
    }

    // Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error   
    //去到一个预先定义的书签,如果书签不存在应用程序将引发错误  
    public void GotoBookMark(string strBookMarkName)
    {
        // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"  
        object missing = System.Reflection.Missing.Value;
        object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
        object NameBookMark = strBookMarkName;
        oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
    }

    public void GoToTheEnd()
    {
        // VB :  Selection.EndKey Unit:=wdStory  
        object missing = System.Reflection.Missing.Value;
        object unit;
        unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
        oWordApplic.Selection.EndKey(ref unit, ref missing);
    }
    public void GoToTheBeginning()
    {
        // VB : Selection.HomeKey Unit:=wdStory  
        object missing = System.Reflection.Missing.Value;
        object unit;
        unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
        oWordApplic.Selection.HomeKey(ref unit, ref missing);
    }


    public void GoToTheTable(int ntable)
    {
        object missing = System.Reflection.Missing.Value;
        object what;
        what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
        object which;
        which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
        object count;
        count = 1;
        oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
        oWordApplic.Selection.Find.ClearFormatting();
       oWordApplic.Selection.Text = "";
    }

    public void GoToRightCell()
    {
        object missing = System.Reflection.Missing.Value;
        object direction;
        direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
        oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
    }


    public void GoToLeftCell()
    {
      object missing = System.Reflection.Missing.Value;
        object direction;
        direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
        oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
    }


    public void GoToDownCell()
    {
        object missing = System.Reflection.Missing.Value;
        object direction;
        direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
        oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
    }


    public void GoToUpCell()
    {
        object missing = System.Reflection.Missing.Value;
        object direction;
        direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
        oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
    }
    // this function doesn't work 这个功能不起作用  
    public void InsertPageNumber(string strType, bool bHeader)
    {
        object missing = System.Reflection.Missing.Value;
        object alignment;
        object bFirstPage = false;
        object bF = true;
        //if (bHeader == true)  
        //WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF;  
        switch (strType)
        {
            case "Center":
                alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                    break;
            case "Right":
                alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                //oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;  
                break;
            case "Left":
                alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
                oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
                break;
        }
    }
    public void InsertImage(string picPath, float picWidth, float picHeight)
    {
        object LinkToFile = false;
        object SaveWithDocument = true;//
        object missing = System.Reflection.Missing.Value;
        InlineShape inlineShape = oWordApplic.Selection.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref missing);
        inlineShape.Width = picWidth;
        inlineShape.Height = picHeight;
    } 
}


相关文章推荐

JasperReport学习笔记5-其它数据生成动态的报表(WEB)

1.空数据(Empty Datasources)  就是说JRXML文件里面的内容都是静态的,直接使用  JREmptyDataSource就行了 Java代码   J...

Silverlight动态添加数据模板(以RadGridView为例子)

我在做项目时需要动态添加一些列来显示数据,在网上查找了一些资料,总结了一下,希望对大家有帮助   1.不显示第1列即列指示器(Row Indicators)             在 tele...

基于web的动态图形报表(毕设)

  • 2009年03月16日 17:20
  • 1020KB
  • 下载

extjs在panel中动态添加模板并加载数据

废话不多说,直接上代码: // Ext的panel Ext.define('MyApp.view.misDetailPanel', { extend: 'Ext.panel.Panel', ...

JasperReport学习笔记4-查询数据库生成动态的报表(WEB)

分两种方法  第一种  1.模版  Xml代码   xml version="1.0" encoding="UTF-8"?>   "http://jasperre...

今天报表模板突然获取不到数据

package ssoc.report.ds; /**  * Created by IntelliJ IDEA.  * User: hongqypackage ssoc.report.ds; ...
  • a0you
  • a0you
  • 2012年07月04日 17:36
  • 636
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:利用dotm模板动态添加web报表数据
举报原因:
原因补充:

(最多只允许输入30个字)