MFC vc++6 读写Excel

1.Ctrl+W 执行 ClassWizard

2.Add Class.../From a type Library... 在 Office 目录中,找到你想使用的类型库。(excel.exe)根据需要添加,(可以全部)(2003)

3.包含头文件#include "excel.h"

4.详见msdn:Automating Microsoft Office 97 and Microsoft Office 2000(搜索主题)->参见MFC example

5.拷贝msdn代码测试

//添加第5列
oRange = oSheet.GetRange(COleVariant("F1"), vOpt);
oRange.SetValue2(COleVariant("YYY"));
Font oFont = oRange.GetFont();
oFont.SetSize(COleVariant((long)50));
oFont.SetColor(COleVariant((long)0xFF0000));

//设置链接

  1. void CExcelTest8Dlg::OnBtnExcel()
  2. {
  3. // TODO: Add your control notification handler code here
  4. COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  5. _Application oApp;
  6. oApp.CreateDispatch("Excel.Application");
  7. if (!oApp)
  8. {
  9. AfxMessageBox("Cannot start Excel.");
  10. return;
  11. }
  12. //Get the Workbooks collection so that you can add a new workbook
  13. Workbooks oBooks = oApp.GetWorkbooks();
  14. _Workbook oBook = oBooks.Add(vOpt);
  15. //Get the Worksheets collection of the new Workbook so that
  16. //you can get the IDispatch for the first WorkSheet
  17. Worksheets oSheets = oBook.GetWorksheets();
  18. _Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
  19. //*** ADD DATA TO THE WORKSHEET
  20. //Add Headers to Row 1 of the worksheet
  21. Range oRange;
  22. oRange = oSheet.GetRange(COleVariant("A1"), vOpt);
  23. oRange.SetValue2(COleVariant("Date"));
  24. oRange = oSheet.GetRange(COleVariant("B1"), vOpt);
  25. oRange.SetValue2(COleVariant("Order #"));
  26. oRange = oSheet.GetRange(COleVariant("C1"), vOpt);
  27. oRange.SetValue2(COleVariant("Amount"));
  28. oRange = oSheet.GetRange(COleVariant("D1"), vOpt);
  29. oRange.SetValue2(COleVariant("Tax"));
  30. //Create a safe array that is NUMROWS x 3 --
  31. //column 1 will contain dates column 2 will contain strings
  32. //and column 2 will contain numbers
  33. COleSafeArray sa;
  34. DWORD dwElements[2];
  35. dwElements[0]= NUMROWS; //Number of rows
  36. dwElements[1]= 3; //Number of columns
  37. sa.Create(VT_VARIANT, 2, dwElements);
  38. //Populate the safe array with the data
  39. long index[2];
  40. long lRow;
  41. COleVariant vTemp;
  42. COleDateTime vDateTime;
  43. CString s;
  44. for(lRow=0;lRow<=NUMROWS-1;lRow++)
  45. {
  46. index[0] = lRow;
  47. //Fill the first column with dates
  48. index[1] = 0;
  49. vDateTime.SetDate(1999, rand()%12, rand()%28);
  50. sa.PutElement(index, (COleVariant)vDateTime);
  51. //Fill the second column with strings
  52. index[1] = 1;
  53. s.Format("ORDR%d", lRow+1000);
  54. vTemp = s;
  55. sa.PutElement(index, vTemp);
  56. //Fill the third column with numbers
  57. index[1] = 2;
  58. vTemp = (long)rand();
  59. sa.PutElement(index, vTemp);
  60. }
  61. //Fill a range, starting at A2 with the data in
  62. //the safe array
  63. oRange = oSheet.GetRange(COleVariant("A2"), vOpt);
  64. oRange = oRange.GetResize(COleVariant((short)NUMROWS),
  65. COleVariant((short)3));
  66. oRange.SetValue2(sa);
  67. sa.Detach();
  68. //*** ADD FORMULAS TO THE WORKSHEET
  69. //Fill the fourth column with a formula to compute the
  70. //sales tax. Note that the formula uses a "relative"
  71. //cell reference so that it fills properly.
  72. oRange = oSheet.GetRange(COleVariant("D2"), vOpt);
  73. oRange = oRange.GetResize(COleVariant((long)NUMROWS),
  74. COleVariant((long)1));
  75. oRange.SetFormula(COleVariant("=C2*0.07"));
  76. //*** FORMAT THE WORKSHEET
  77. oRange = oSheet.GetRange(COleVariant("A1"), COleVariant("D1"));
  78. Font oFont = oRange.GetFont();
  79. oFont.SetBold(COleVariant((short)TRUE));//Apply Bold to Headers
  80. oRange = oRange.GetEntireColumn();
  81. oRange.AutoFit(); //AutoFit the columns 1:4
  82. //Make Excel visible and give the user control
  83. oApp.SetVisible(TRUE);
  84. oApp.SetUserControl(TRUE);
  85. }

补充:

HYPERLINK的格式和Excel中的文件格式差不多,只是在vc中要在字符串中加入转义字符


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值