vc 6.0 生成word报表

可以用于将数据 生产一个报表:

// CMDIDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CMDIDemo.h"
#include "CMDIDemoDlg.h"

#include"comdef.h"
#include"atlbase.h"
#include"msword.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
        // No message handlers
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCMDIDemoDlg dialog

CCMDIDemoDlg::CCMDIDemoDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CCMDIDemoDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CCMDIDemoDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCMDIDemoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CCMDIDemoDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCMDIDemoDlg, CDialog)
    //{{AFX_MSG_MAP(CCMDIDemoDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_MakeReport, OnMakeReport)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCMDIDemoDlg message handlers

BOOL CCMDIDemoDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application‘s main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCMDIDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCMDIDemoDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCMDIDemoDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

BOOL CCMDIDemoDlg::OnMakeReport()
{
    //Initialize COM context

    //if(!AfxOleInit())
    //{
    //    AfxMessageBox("无法初始化COM的动态连接库");
    //    return FALSE;
    //}
    if (FAILED(::CoInitialize(NULL)))
    {
         AfxMessageBox("Initialize COM context failed!");
         return FALSE;
    }
    // TODO: Add your control notification handler code here
        _Application wordApp;

       Documents wordDocs;//创建一个新的word文档
       _Document wordDoc;

       Selection wordSelection;

       Range wordRange;//

       Tables wordTables;
       Table wordTable; 

       Cell wordCell;
       Cells wordCells;
       //Paragraphs wordParagraphs;
       //Paragraph wordParagraph;
       _Font wordFont;
       Shading wordShading;
       //
       int nRow=0;
       //
       COleVariant  vTrue((short)TRUE),
                    vFalse((short)FALSE),
                    vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

       CComVariant defaultBehavior(1),AutoFitBehavior(0);

       //创建word application实例
       if (!wordApp.CreateDispatch(_T("Word.Application")))
       {
              AfxMessageBox("Word CreateDispatch Failed!");
              return FALSE;
       }    

       wordApp.SetVisible(true);//make visible
       wordApp.Activate();//激活
       wordDocs=wordApp.GetDocuments();//get documents object

       //
       CString strDocTemplate;
       CString strPath;

       char szFullPath[_MAX_PATH];
       ::GetModuleFileName(NULL,szFullPath,_MAX_PATH);
       strDocTemplate.Format("%s",szFullPath);

       int nPos=strDocTemplate.ReverseFind(‘""‘);
       strDocTemplate=strDocTemplate.Left(nPos);
       strPath=strDocTemplate;
       TRACE1("%s\n",strDocTemplate);
         //  TRACE1();
//     strDocTemplate+="""Template""Report.doc";
//     wordDoc=wordDocs.Open(COleVariant(strDocTemplate),&vOpt,&vOpt,&vOpt,&vOpt,
//            &vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vTrue,&vOpt,&vOpt,&vOpt,&vOpt);
       CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
       wordDoc=wordDocs.Add(&tpl,&NewTemplate,&DocType,&Visble);

       wordSelection=wordApp.GetSelection();
       //wordTables.AttachDispatch(wordDoc.GetTables());
       wordTables=wordDoc.GetTables();
       //wordParagraphs = wordDoc.GetParagraphs();
       //wordParagraph=wordParagraphs.GetLast();
       //wordRange=wordParagraph.GetRange();
       //wordSelection.EndKey(COleVariant((short)6),COleVariant((short)0));
       //move insertion point to the end of the story
       /*
       wdUnits:
       wdCharacter=1
    wdWord=2
    wdSentence=3
    wdParagraph=4
    wdSection=8
    wdStory=6
    wdCell=12
    wdColumn=9
    wdRow=10
    wdTable=15
       wdMovementType:

       wdMove=0
    wdExtend=1
       */
       //move to end of story
       wordSelection.EndOf(COleVariant((short)6),COleVariant((short)0));
       //1.1 RxLev Full
       wordSelection.TypeText("1. 统计报告");
       wordSelection.HomeKey(&CComVariant(5),&CComVariant(1));
       //Format the line with selection
       wordFont = wordSelection.GetFont();
       wordFont.SetBold(9999998);//wdToggle
       wordSelection.EndOf(&CComVariant(5),&CComVariant(0));
       wordSelection.TypeParagraph();
       wordSelection.TypeText("(1.1). 分段统计");
       wordSelection.TypeParagraph();
       wordFont.SetBold(9999998);//wdToggle
       wordRange=wordSelection.GetRange();
       //add table
       //nRow=m_nRange1+1;
       wordTable=wordTables.Add(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);
       wordRange=wordTable.GetRange();
       //wordRange.MoveEnd(COleVariant((short)15),COleVariant((short)1));
       //wordRange.Select();
       //move end of table
       //wordSelection.EndOf(COleVariant((short)15),COleVariant((short)0));
       //insert rows
       //wordSelection.InsertRowsBelow(COleVariant((short)5));
       //选择第一个单元,进而选择第一行进行格式化
       wordCell=wordTable.Cell(1,1);
       wordCell.Select();
       //select the row with current selection
       wordSelection.EndKey(&CComVariant(10),&CComVariant(1));
       //Format the row with selection
       //wordFont = wordSelection.GetFont();
       wordFont.SetBold(9999998);//wdToggle
       wordCells=wordSelection.GetCells();
       wordShading = wordCells.GetShading();
       wordShading.SetTexture(0);
       wordShading.SetBackgroundPatternColor(14737632);//15987699 14737632 adColorBlue
       wordShading.SetForegroundPatternColor(-16777216);//-16777216 wdColorAutomatic
       //move to end of table
       //wordSelection.EndOf(COleVariant((short)15),COleVariant((short)0));
       //wordParagraph=wordParagraphs.GetLast();
       //wordRange=wordParagraph.GetRange();
       //wordRange.MoveEnd(COleVariant((short)4),COleVariant((short)1));
       //wordRange.SetText("Test");
       //wordSelection=wordApp.GetSelection();
       //wordSelection.MoveEnd(COleVariant((short)6),COleVariant((short)1));
       wordCell=wordTable.Cell(1,1);
       wordCell.Select();
       wordSelection.TypeText("统计项目");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("采样");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("百分比");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("累计百分比");
       //
       //
       wordSelection.EndOf(COleVariant((short)6),COleVariant((short)0));
       wordSelection.TypeParagraph();
       wordSelection.TypeText("(1.2). 分段统计");
       wordSelection.HomeKey(&CComVariant(5),&CComVariant(1));
       //Format the line with selection
       wordFont = wordSelection.GetFont();
       wordFont.SetBold(9999998);//wdToggle
       wordSelection.EndOf(&CComVariant(5),&CComVariant(0));
       wordSelection.TypeParagraph();
       wordFont.SetBold(9999998);//wdToggle
       wordRange=wordSelection.GetRange();
       //add table
       //nRow=m_nRange1+1;
       wordTable=wordTables.Add(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);
       wordRange=wordTable.GetRange();
       //选择第一个单元,进而选择第一行进行格式化
       wordCell=wordTable.Cell(1,1);
       wordCell.Select();
       //select the row with current selection
       wordSelection.EndKey(&CComVariant(10),&CComVariant(1));
       //Format the row with selection
       wordFont = wordSelection.GetFont();
       wordFont.SetBold(9999998);
       wordCells=wordSelection.GetCells();
       wordShading = wordCells.GetShading();
       wordShading.SetTexture(0);
       wordShading.SetBackgroundPatternColor(14737632);//15987699
       wordShading.SetForegroundPatternColor(-16777216);
       wordCell=wordTable.Cell(1,1);
       wordCell.Select();
       wordSelection.TypeText("Range");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("Samples");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("Percentage");
       wordSelection.MoveRight(&CComVariant(12),&CComVariant(1),&CComVariant(0));
       wordSelection.TypeText("Calculation");
       //
       //Save document as report
       SYSTEMTIME sysTime;
       GetLocalTime(&sysTime);
       CString strReport;
       strReport.Format("%d年-%d月-%d日 %d-%d-%d 报告",sysTime.wYear,sysTime.wMonth,
              sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
       strReport=strPath+""""+strReport;
       wordDoc.SaveAs(COleVariant(strReport),COleVariant((short)0),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt
              ,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
       //Release com
       wordFont.ReleaseDispatch();
       wordCells.ReleaseDispatch();
       wordShading.ReleaseDispatch();
       wordTable.ReleaseDispatch();
       wordTables.ReleaseDispatch();
       wordRange.ReleaseDispatch();
       wordSelection.ReleaseDispatch();
       wordDoc.RecheckSmartTags();
       wordDocs.ReleaseDispatch();
       wordApp.ReleaseDispatch();

        CoUninitialize();
       return TRUE;

}
时间: 2024-10-25 13:42:11

vc 6.0 生成word报表的相关文章

使用java Apache poi 根据word模板生成word报表

项目开发过程中,客户提出一堆导出报表的需求,需要导出word格式,页眉还需要加上客户公司的logo,试了几种方案,最后选择了用 Apache poi 加上自定义标签的方式实现. 目前功能还比较简单,一些复杂的表格做不了,但是已经基本满足项目需求了. 使用poi读取word模板,替换word中的{text}标签,并根据自定义标签循环生成表格或表格中的行. 代码示例下载:https://download.csdn.net/download/u012775558/10306308 注意,仅支持docx

C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档.时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用.感谢这位博友 静下心,认真做,

VC++6.0生成DLL

一.生成DLL文件 1.菜单项“文件”——>“新建”——>“工程”——>“Win32 Dynamic-Link Library”.创建一个工程项目,为这个工程取个名字为“test”.在这里我选择“创建一个简单的DLL”,此时在VC++界面上会有一个树形文件结构. 2.看到这个文件结构就大概明白,Source Files是放源码的,也就是以.cpp为后缀的文件:Header Files就是以放头文件,也就是以.h为后缀的文件.这个StdAfx.h和StdAfx.cpp都是自动生成的,这个S

Word报表及二次开发总结

在各类应用系统开发中,和Word相关的应用可谓相当广泛.如各类MIS系统.各种和实际业务结合紧密的系统.需要制式报表的系统等,都需要对Word进行操作,典型的应用包括: 1.内嵌Word.在系统中内嵌Word,这样,既可以利用Word强大的功能进行文档的新建.编辑.修改.排版,同时还节省了用户对于编辑器操作的学习成本,提高了文档格式的通用性. 2.Word的二次开发.通过Word自带的宏,利用VBA(Visual Basic Appplication)进行开发,实现各种复杂的自动化功能. 3.前

用Aspose.Words for .NET动态生成word文档中的数据表格

1.概述 最近项目中有一个这样的需求:导出word 文档,要求这个文档的格式不是固定的,用户可以随便的调整,导出内容中的数据表格列是动态的,例如要求导出姓名和性别,你就要导出这两列的数据,而且这个文档不是导出来之后再调整而是导出来后已经是调整过了的.看到这里,您也许马上想到用模板导出!而且.NET中自带有这个组件:Microsoft.Office.Interop.Word,暂且可以满足需求吧.但这个组件也是有局限性的,例如客户端必须装 office组件,而且编码复杂度高.最麻烦的需求是后面那个-

C#简单实现动态数据生成Word文档并保存

今天正好有人问我,怎么生成一个报表式的Word文档. 就是文字的样式和位置相对固定不变,只是里面的内容从数据中读取. 我觉得类似这种的一般用第三方报表来做比较简便.但既然要求了Word,只好硬着头皮来. 网上的方法大多数都是从一个GridView或表中获得数据后向Word中添加一个表格. (图1) 我们使用Word模板来实现,方法如下: 1.首先需要向工程中的“引用”加入Word类库的引用(图2).我是Office 2003.其他版本可能略有不同.在COM里面 (图2) 2.用Word设计一个模

velocity模板技术生成word文档

本文介绍采用velocity技术在Java中生成word文档的方法. 1.新建一个word文档,编辑内容如下: 2.将上述word文档另存为htm格式的文件 3.新建一个Java Project项目velocityTest,加入Velocity所依赖的Jar包velocity-dep-1.5-beta2.jar. 4.在src目录下新建一个velocity.vm文件,将第二步生成的velocity.htm用文本编辑器打开,复制内容到velocity.vm中.内容如下: <html xmlns:v

Nmon工具的使用以及通过nmon_analyse生成分析报表

Nmon工具的使用以及通过nmon_analyse生成分析报表 在我们监控我们的操作系统的时候如果可以把各个硬件的监控信息生成形象化的分析报表图对于我们来说是件太好的事情了,而通过ibm的nom和nmon_analyser两者的结合完全可以实现我们的要求.首先对nmon和nmon_analyser做一个大概介绍. Nmon 是一个分析aix和linux性能的免费工具(其主要是ibm为自己的aix操作系统开发的,但是也可以应用在linux操作系统上),而nmon_analyser是nmon的一个工

在.NET中使用EPPlus生成Excel报表 .

--摘抄自:http://blog.csdn.net/zhoufoxcn/article/details/14112473 在开发.NET应用中可能会遇到需要生成带图表(Chart)的Excel报表的需求,特别是在一些ASP.NET网站中,有时候我们并不能保证Web服务器上一定安装了Office组件,所以使用微软的Office来生成Excel并不保证在所有情况下都使用,有时候即使Web服务器上安装了Office也会出现一些运行权限方面的原因到导致调用Excel组件生成Excel失败,所以在这里介