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; }
发现自己的不足,善于利用找到的方法去扬长避短。行动起来。