VC下操作Excel源码示例

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. //
  3. // This source code is only intended as a supplement to the
  4. // Microsoft Visual C++ Language Reference and related
  5. // electronic documentation provided with Microsoft Visual C++.
  6. // See these sources for detailed information regarding the
  7. // Microsoft Visual C++ product.
  8. // NOTE: This example will only work with Excel10 in OfficeXP
  9. // Compile with cl /GX comexcel.cpp
  10. #include "stdafx.h"
  11. #define OFFICE2007 1
  12. #define OFFICEXP 2
  13. #define OFFICE2000 3
  14. // Default Settings
  15. #define OFFICE_VER OFFICE2007
  16. #define USE_PROGID 1
  17. #define USE_LIBID 0
  18. #define _M2STR(x) #x
  19. #define M2STR(x) _M2STR(x)
  20. // Ignore hard wired default paths if MSDLL_PATH is
  21. // defined from the command line
  22. #ifndef MSDLL_PATH
  23. // Paths to required MS OFFICE files.
  24. // Make sure these paths are correct for your machine
  25. #pragma message ("Make sure the path to MSO DLL is correct.")
  26. #if OFFICE_VER == OFFICE2007
  27. #define _MSDLL_PATH "C:\Program Files\Common Files\Microsoft Shared\Office12\MSO.DLL"
  28. #elif OFFICE_VER == OFFICEXP
  29. #define _MSDLL_PATH "C:\Program Files\Common Files\Microsoft Shared\Office11\MSO.DLL"
  30. #elif OFFICE_VER == OFFICE2000
  31. #define _MSDLL_PATH "C:\Program Files\Microsoft Office\Office\MSO9.dll"
  32. #endif
  33. #else
  34. #define _MSDLL_PATH M2STR(MSDLL_PATH)
  35. #endif
  36. //
  37. // Delete the *.tlh files when changing import qualifiers
  38. #import _MSDLL_PATH rename("RGB", "MSRGB")
  39. #ifdef VBE6EXT_PATH
  40. #import M2STR(VBE6EXT_PATH)
  41. #else
  42. #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
  43. #endif
  44. #if USE_PROGID
  45. #import "progid:Excel.Sheet" auto_search auto_rename rename_search_namespace("Office12")
  46. #elif USE_LIBID
  47. #import "libid:{00020813-0000-0000-C000-000000000046}" auto_search auto_rename version(1.3) lcid(0) no_search_namespace
  48. #else
  49. // Ignore hard wired default paths if MSDLL_PATH is
  50. // defined from the command line
  51. #ifndef MSEXCEL_PATH
  52. #pragma message ("Make sure the path to excel.exe is correct")
  53. #if OFFICE_VER == OFFICE2007
  54. #define _MSEXCEL_PATH "C:\Program Files\Microsoft Office\Office12\excel.exe"
  55. #elif OFFICE_VER == OFFICEXP
  56. #define _MSEXCEL_PATH "C:\Program Files\Microsoft Office\Office11\excel.exe"
  57. #elif OFFICE_VER == OFFICE2000
  58. #define _MSEXCEL_PATH "C:\Program Files\Microsoft Office\Office\excel.exe"
  59. #endif
  60. #else
  61. #define _MSEXCEL_PATH M2STR(MSEXCEL_PATH)
  62. #endif
  63. #import _MSEXCEL_PATH auto_search auto_rename dual_interfaces
  64. #endif // USE_LIBID
  65. void dump_com_error(_com_error &e)
  66. {
  67. _tprintf(_T("Oops - hit an error!\n"));
  68. _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  69. _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  70. _bstr_t bstrSource(e.Source());
  71. _bstr_t bstrDescription(e.Description());
  72. _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  73. _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
  74. }
  75. // If this is placed in the scope of the smart pointers, they must be
  76. // explicitly Release(d) before CoUninitialize() is called. If any reference
  77. // count is non-zero, a protection fault will occur.
  78. struct StartOle {
  79. StartOle() { CoInitialize(NULL); }
  80. ~StartOle() { CoUninitialize(); }
  81. } _inst_StartOle;
  82. void main()
  83. {
  84. using namespace Excel;
  85. _ApplicationPtr pXL;
  86. try {
  87. pXL.CreateInstance(L"Excel.Application");
  88. pXL->Visible[0] = VARIANT_TRUE;
  89. WorkbooksPtr pBooks = pXL->Workbooks;
  90. _WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
  91. _WorksheetPtr pSheet = pXL->ActiveSheet;
  92. try {
  93. // This one will fail; it is done on purpose to demonstrate the error.
  94. pSheet->Name = "Market Share?";
  95. } catch (_com_error &e) {
  96. dump_com_error(e);
  97. }
  98. pSheet->Name = "Market Share!";
  99. // When using parameterized properties, optional args must be explicitly dealt with.
  100. pSheet->Range["A2"][vtMissing]->Value2 = "Company A";
  101. pSheet->Range["B2"][vtMissing]->Value2 = "Company B";
  102. pSheet->Range["C2"][vtMissing]->Value2 = "Company C";
  103. pSheet->Range["D2"][vtMissing]->Value2 = "Company D";
  104. // Of course, you can call a parameterized property as a method and then optional args are implicit.
  105. pSheet->GetRange("A3")->Value2 = 75.0;
  106. pSheet->GetRange("B3")->Value2 = 14.0;
  107. pSheet->GetRange("C3")->Value2 = 7.0;
  108. pSheet->GetRange("D3")->Value2 = 4.0;
  109. Sleep(1000);
  110. RangePtr pRange = pSheet->Range["A2:D3"][vtMissing];
  111. _ChartPtr pChart = pBook->Charts->Add();
  112. pChart->ChartWizard((Range*) pRange, (long) xl3DPie, 7L, (long) xlRows,
  113. 1L, 0L, 2L, "Market Share");
  114. Sleep(6000);
  115. pBook->Saved[0] = VARIANT_TRUE;
  116. pXL->Quit();
  117. } catch(_com_error &e) {
  118. dump_com_error(e);
  119. }
  120. }   
来源:http://read.pudn.com/downloads100/doc/fileformat/411493/C++/ComTypeLibfor7/comexcel/excel10/comexcel.cpp__.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值