wxwidgets应用手记(6) 之报表

wxwidgets开发系统,缺少很好的报表设计工具,可以借助Word来做报表。

Office自动化参考http://msdn.microsoft.com/en-us/library/78whx7s6(v=vs.80).aspx

一个很好的对COM的操作类http://disphelper.sourceforge.net/

仔细看它的readme.htm文件

贴上一个操作类

void ReportDll_Open(const wchar_t* fileName,bool visible)//打开报表模板

void ReportDll_Close()//关闭

void ReportDll_Write(const wchar_t* data[],int len)//插一行,填一行数据

void ReportDll_SearchReplace(const wchar_t* Find, const wchar_t* Replacement)//查找占位符,用值替换占位符

   1:  
   2: #include "disphelper.h"
   3: #include <string>
   4: #include "ReportDll.h"
   5: using namespace std;
   6:  
   7:  
   8: CDispPtr wdApp, wdDoc;
   9:  
  10: void ReportDll_Open(const wchar_t* fileName,bool visible)
  11: {
  12:     //CDhInitialize init;    /* Initialize DispHelper for this thread. */
  13:     /* Turn on DispHelper exceptions */
  14:     dhToggleExceptions(TRUE);
  15:  
  16:     try
  17:     {
  18:         /* Create an instance of 'Word.Application' on the local machine */
  19:         dhCheck( dhCreateObject(L"Word.Application", NULL, &wdApp) );
  20:  
  21:         /* wdApp.Visible = True */
  22:         dhPutValue(wdApp, L".Visible = %b", visible);
  23:  
  24:         dhCallMethod(wdApp, L".Documents.Open(%S)", fileName);
  25:  
  26:         /* Set wdDoc = wdApp.Documents.Add */
  27:         /* '%o' means we wish to receive an IDispatch object in wdDoc */
  28:         dhCheck( dhGetValue(L"%o", &wdDoc, wdApp, L".ActiveDocument") );
  29:     }
  30:     catch (string errstr)
  31:     {
  32:         MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"ReportDll", MB_OK);
  33:     }
  34: }
  35:  
  36: void ReportDll_Close()
  37: {
  38:     try
  39:     {
  40:         /* wdApp.ActiveDocument.Saved = True */
  41:         /* This prevents the user being prompted when word is closed. */
  42:         //dhPutValue(wdApp, L".ActiveDocument.Saved = %b", TRUE);
  43:  
  44:         dhCallMethod(wdDoc, L".Save");
  45:         dhCallMethod(wdDoc, L".Close");
  46:  
  47:         /* wdApp.Quit() */
  48:         dhCallMethod(wdApp, L".Quit");
  49:  
  50:         wdDoc = NULL;
  51:         wdApp = NULL;
  52:     }
  53:     catch (string errstr)
  54:     {
  55:         MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"ReportDll", MB_OK);
  56:     }
  57:  
  58:     /* Turn off exceptions display for cleanup */
  59:     dhToggleExceptions(FALSE);
  60: }
  61:  
  62: void ReportDll_Write(const wchar_t* data[],int len)
  63: {
  64:     CDispPtr wdTab,wdRow;
  65:  
  66:     try
  67:     {
  68:         dhCheck( dhGetValue(L"%o", &wdTab, wdDoc, L".Tables(%d)",1));
  69:         dhCheck( dhGetValue(L"%o", &wdRow, wdTab, L".Rows(%d)",2));
  70:         //在上面插入一行
  71:         dhCallMethod(wdTab, L".Rows.Add(%o)",wdRow);
  72:  
  73:         for (int i = 0; i <len;i++)
  74:         {
  75:             dhPutValue(wdTab, L".Cell(%d,%d).Range.Text=%S", 2,i+1,data[i]);
  76:         }
  77:     }
  78:     catch (string errstr)
  79:     {
  80:         MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"ReportDll", MB_OK);
  81:     }
  82: }
  83:  
  84:  
  85: void ReportDll_SearchReplace(const wchar_t* Find, const wchar_t* Replacement)
  86: {
  87:     try
  88:     {
  89:         dhCallMethod(wdApp, L".Selection.Find.ClearFormatting");
  90:         dhCallMethod(wdApp, L".Selection.Find.Replacement.ClearFormatting");
  91:  
  92:         dhPutValue(wdApp, L".Selection.Find.Text = %S",Find);
  93:         dhPutValue(wdApp, L".Selection.Find.Replacement.Text = %S",Replacement);
  94:  
  95:         dhPutValue(wdApp, L".Selection.Find.MatchWholeWord = %b",TRUE);
  96:         dhPutValue(wdApp, L".Selection.Find.Forward =  %b",TRUE);
  97:         dhPutValue(wdApp, L".Selection.Find.Wrap = %d",1);
  98:         dhPutValue(wdApp, L".Selection.Find.Format = %b",FALSE);
  99:  
 100:         dhCallMethod(wdApp, L".Selection.Find.Execute(%m,%m,%m,%m,%m,%m,%m,%m,%m,%m,%d,%m,%m,%m,%m)",2);
 101:     }
 102:     catch (string errstr)
 103:     {
 104:         MessageBox(NULL,std::wstring(errstr.begin(), errstr.end()).c_str(),L"ReportDll", MB_OK);
 105:     }
 106: }

调用

   1: void ReportMainFrame::m_button3OnButtonClick( wxCommandEvent& event )
   2: {
   3:     wxFileName file(ty::Common::GetModuleFileName());
   4:     wxString  file1 = file.GetPath() + _T("\\Orders.doc");
   5:     wxString  file2 = file.GetPath() + _T("\\Orders_Test.doc");
   6:     wxCopyFile(file1,file2,true);
   7:  
   8:     wxString str;
   9:     str<<_T("打印时间:");
  10:     str<< ty::Common::DateTimeToString(wxDateTime::Now());
  11:  
  12:     ReportDll_Open(file2.c_str(),true);
  13:  
  14:     ReportDll_SearchReplace(L"打印人:",L"打印人:李明");
  15:     ReportDll_SearchReplace(L"打印时间:",str.c_str());
  16:  
  17:     for (int i = 0;i< 10;i++)
  18:     {
  19:         const wchar_t* data[] = {
  20:             m_grid1->GetCellValue(i,0).c_str(),
  21:             m_grid1->GetCellValue(i,1).c_str(),
  22:             m_grid1->GetCellValue(i,2).c_str(),
  23:             m_grid1->GetCellValue(i,3).c_str(),
  24:             m_grid1->GetCellValue(i,4).c_str(),
  25:             m_grid1->GetCellValue(i,5).c_str(),
  26:             m_grid1->GetCellValue(i,6).c_str(),
  27:             m_grid1->GetCellValue(i,7).c_str(),
  28:             m_grid1->GetCellValue(i,8).c_str(),
  29:             m_grid1->GetCellValue(i,9).c_str(),
  30:             m_grid1->GetCellValue(i,10).c_str(),
  31:             m_grid1->GetCellValue(i,11).c_str(),
  32:             m_grid1->GetCellValue(i,12).c_str(),
  33:             m_grid1->GetCellValue(i,13).c_str()
  34:         } ;
  35:  
  36:         ReportDll_Write(data,14);
  37:     }
  38:  
  39:     ReportDll_Close();
  40: }

报表模板如

1

 

根据生成得到的报表

2

posted @ 2012-01-23 18:05  快乐驿站  阅读(599)  评论(0编辑  收藏  举报