visual studio 2010 c ++:导出数据到excel

在我的项目中,我希望以excel表格或CSV格式导出结果。 在其中一个教程项目中,使用CListCtrl对象显示结果。 这段代码的头文件:

#include "CProBlob.h" #include "CProBlobParamsResource.h" ///////////////////////////////////////////////////////////////////////////// // CBlobResultsPage dialog class CBlobResultsPage : public CDialog { //DECLARE_DYNCREATE(CBlobResultsPage) // Construction public: CBlobResultsPage(CWnd* pParentWnd, CProBlob* pBlob = NULL); ~CBlobResultsPage(); // Dialog Data //{{AFX_DATA(CBlobResultsPage) enum { IDD = IDD_BLOB_RESULTS }; CListCtrl m_List; //}}AFX_DATA // Overrides // ClassWizard generate virtual function overrides //{{AFX_VIRTUAL(CBlobResultsPage) public: virtual BOOL Create(CWnd* pParentWnd); protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual void OnCancel( ); //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CBlobResultsPage) virtual BOOL OnInitDialog(); afx_msg void OnSize(UINT nType, int cx, int cy); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: void UpdateList(); private: void UpdateItem(int item, bool insert); void FitListToWindow(); protected: CProBlob* m_pBlob; int m_ColumnCount; }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_BLOBRESULTSPAGE_H__A1AF73AA_E071_41D2_BA2E_40FCF16E8630__INCLUDED_) /////////////////////////////////////// 

和相应的cpp文件是:

  BOOL CBlobResultsPage::OnInitDialog() { CDialog::OnInitDialog(); // Initialize icon from parent SetIcon(m_pParentWnd->GetIcon(FALSE), FALSE); SetIcon(m_pParentWnd->GetIcon(TRUE), TRUE); // Insert colums in the list (the first time only) if (!m_List.GetItemCount()) { m_ColumnCount = 0; // Basic Blob Features m_List.InsertColumn(m_ColumnCount++, "Index"); m_List.InsertColumn(m_ColumnCount++, "Area"); m_List.InsertColumn(m_ColumnCount++, "BBoxArea"); m_List.SetExtendedStyle(m_List.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); } UpdateList(); FitListToWindow(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CBlobResultsPage::UpdateList() { int numItems = __min(m_List.GetItemCount(), m_pBlob->GetNumBlobs()); // Update already existing items int i; for (i=0; i < numItems; i++) { UpdateItem(i, false); } // Is the new list larger than the existing one? if (m_pBlob->GetNumBlobs() > m_List.GetItemCount()) { // Insert new items at the end of the list for (; i < m_pBlob->GetNumBlobs(); i++) { UpdateItem(i, true); } } else { // Delete unused items int item = m_pBlob->GetNumBlobs(); while (m_List.GetItemCount() > m_pBlob->GetNumBlobs()) { m_List.DeleteItem(item); } } // Adjust columns width for (i=0; i < m_ColumnCount; i++) m_List.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER); } void CBlobResultsPage::UpdateItem(int item, bool insert) { CString str; LPCTSTR NotComputedStr = "NC"; int col = 1; // Index str.Format("%d", item); if (insert) m_List.InsertItem(item, str); else m_List.SetItemText(item, 0, str); // Blob Area str.Format("%d", (int)m_pBlob->GetArea(item)); m_List.SetItemText(item, col++, str); // Blob BBox Area str.Format("%d", (int)m_pBlob->GetBBoxArea(item)); m_List.SetItemText(item, col++, str); } 

到目前为止,我可以修改代码来使用本网站上提供的信息打开Excel表。

并希望修改updatelist()函数。 但是,我不知道excel对象中的相应函数。

1)如何获得使用的列数。 它的types应该是int。

2)如何自动增加列。

3)如何在单元格(i,j)格式的Excel表格中访问单元格,以便在循环中使用

  Updated code : BOOL CBlobResultsPage::OnInitDialog() { CDialog::OnInitDialog(); // Initialize icon from parent SetIcon(m_pParentWnd->GetIcon(FALSE), FALSE); SetIcon(m_pParentWnd->GetIcon(TRUE), TRUE); if(!AfxOleInit()) { AfxMessageBox(_T("cannot initialise COM dll")); return FALSE; } COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); CApplication app; if(!app.CreateDispatch(TEXT("Excel.Application"))) { AfxMessageBox(TEXT("Couldn't start excel and get application object")); return FALSE; } app.put_Visible(TRUE); app.put_UserControl(TRUE); books = app.get_Workbooks(); book = books.Add(covOptional); //Get the first sheet. sheets =book.get_Sheets(); sheet = sheets.get_Item(COleVariant((short)1)); COleSafeArray saRet; DWORD numElements[]={1,3}; //4x1 element array saRet.Create(VT_BSTR, 2, numElements); FillSafeArray(L"Blob_no", 0, 0, &saRet); FillSafeArray(L"X-cor",0, 1, &saRet); FillSafeArray(L"Y-corr",0, 2, &saRet); range = sheet.get_Range(COleVariant(TEXT("A1")), COleVariant(TEXT("C1"))); range.put_Value2(COleVariant(saRet)); saRet.Detach(); UpdateList(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }