有時候軟件會有報告生成的功能需求,一般都用MS OFFICE開放的接口來開發,但是MS OFFICE不是免費的,可以考慮用WPS開放的接口。MS OFFICE和WPS的接口其實差不太多。
那麼具體要怎樣纔可以用WPS開放的接口生成doc文檔報告呢?
1、添加TypeLib類型庫
比如我現在想要點擊對話框的一個按鈕就開始生成報告,那麼在項目的右鍵菜單中選擇“添加”→“類”→“TypeLib中的MFC庫”,從註冊表中找到關於KingSoft WPS的項, 並在左側選擇要導入的類,如圖:
完成類的導入後,把系統自動生成的類的第一行代碼(#import那行)註釋了。
2、使用
在要用到類型庫的地方包含相應的頭文件,就可以使用了。具體用哪個函數,就找與你要的操作單詞意思接近的函數,多琢磨琢磨就會了,再不行就到WPS的論壇去問好了。
簡單示例代碼:
#include "CApplication.h" #include "CDocuments.h" #include "CDocument0.h" #include "CnlineShapes.h" #include "CRange.h" #include "CSelection.h" #include "CTables0.h" #include "CTable0.h" #include "CCell.h" CApplication app; app.CreateDispatch(_T("WPS.APPLICATION")); app.put_Visible(TRUE); CDocuments docs = app.get_Documents(); docs.Add(COleVariant(_T("")), false, 0, true); CSelection sel = app.get_Selection(); sel.TypeText(_T("hello! 這是個WPS二次開發的簡單例子")); sel.TypeParagraph(); sel.TypeText(_T("測試表格:")); sel.TypeParagraph(); COleVariant vTrue((short)TRUE); COleVariant vFalse((short)FALSE); COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); CnlineShapes shapes = sel.get_InlineShapes(); shapes.AddPicture(_T("G:\\pictures\\girl.jpg"), &vFalse, &vTrue, &vOpt); CDocument0 doc = app.get_ActiveDocument(); CRange range = sel.get_Range(); CTables0 tabs = doc.get_Tables(); COleVariant defaultBehavior((short)1); COleVariant AutoBehavior((short)0); CTable0 tab = tabs.Add(range, 7, 4, defaultBehavior, AutoBehavior); for (int i=0; i<=7; i++) { for (int j=0; j<=4; j++) { CCell cell = tab.Cell(i, j); cell.Select(); CString str; str.Format(_T("%d, %d"), i, j); sel.TypeText(str); } } sel.Move(long(2), long(100)); sel.TypeParagraph(); sel.put_Style(COleVariant(_T("標題 1"))); sel.TypeText(_T("標題 1")); sel.TypeParagraph(); sel.put_Style(COleVariant(_T("標題 3"))); sel.TypeText(_T("標題 3")); sel.TypeParagraph(); sel.put_Style(COleVariant(_T("正文"))); sel.TypeText(_T("正文")); sel.TypeParagraph(); sel.DetachDispatch(); shapes.DetachDispatch(); app.DetachDispatch();
運行效果如下:
示例代碼見附件