4,011
社区成员
发帖
与我相关
我的任务
分享
#import "MSO9.DLL" rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
#import "VBE6EXT.OLB" no_namespace no_auto_exclude
#import "EXCEL9.OLB" rename("RGB", "RBGXL") rename("DialogBox", "DialogBoxXL") \
rename("CopyFile", "CopyFileXL") rename("ReplaceText", "ReplaceTextXL") rename("IPicture", "IPictureXL") \
rename("IFont", "IFontXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces no_auto_exclude
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
using namespace Excel;
_ApplicationPtr pXL;
try
{
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
_WorksheetPtr pSheet = pXL->ActiveSheet;
try {
// This one will fail
pSheet->Name = "Market Share?";
} catch (_com_error &e) {
dump_com_error(e);
}
pSheet->Name = "Market Share!";
pSheet->Range["A2"]->Value = "Company A";
pSheet->Range["B2"]->Value = "Company B";
pSheet->Range["C2"]->Value = "Company C";
pSheet->Range["D2"]->Value = "Company D";
pSheet->Range["A3"]->Value = 75.0;
pSheet->Range["B3"]->Value = 14.0;
pSheet->Range["C3"]->Value = 7.0;
pSheet->Range["D3"]->Value = 4.0;
Sleep(1000);
RangePtr pRange = pSheet->Range["A2:D3"];
_ChartPtr pChart = pBook->Charts->Add();
pChart->ChartWizard((Range*) pRange, (long) xl3DPie, 7L, (long) xlRows,
1L, 0L, 2L, "Market Share");
Sleep(6000);
pBook->Saved = VARIANT_TRUE;
pXL->Quit();
} catch(_com_error &e) {
dump_com_error(e);
}
}
m_books.AttachDispatch(m_app.get_Workbooks());
lpDisp = m_books.Open(filepath, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,covOptional, covOptional,
covOptional,covOptional);
//得到workbook
m_book.AttachDispatch(lpDisp);
m_sheets.AttachDispatch(m_book.get_Worksheets());
lpDisp = m_sheets.get_Item(COleVariant((long)1));
m_sheet.AttachDispatch(lpDisp);//打开对应sheet
for (i = 0;i < num;i ++)
{
m_range = m_sheet.get_Cells();
info = vecInfo[i];
str.Format(_T("%.4f"), info.d_data);
m_range.put_Item(COleVariant(long(i + 1)), COleVariant(long(1)), COleVariant(str));
}