Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:2k
源码类别:

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <comobj.hpp>
  4. #include <utilcls.h>
  5. #pragma hdrstop
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13.         : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17. void __fastcall TForm1::Button1Click(TObject *Sender)
  18. {
  19.   TStringGrid *sgrid;
  20.   int ii,jj,pp;
  21.   int CC;
  22.   AnsiString ass,assn;
  23.   Variant my_excel;
  24.   Variant all_workbooks;
  25.   Variant my_workbook;
  26.   Variant my_worksheet;
  27.   Variant my_range;
  28. //
  29.   PropertyGet Range("Range");
  30.   PropertySet SetValue("Value");
  31. //
  32.   sgrid=StringGrid1;
  33. //
  34.   try
  35.   {
  36.     my_excel=GetActiveOleObject("excel.application");
  37.   }
  38.   catch(...)
  39.   {
  40.     try
  41.     {
  42.       my_excel=CreateOleObject("excel.application");
  43.     }
  44.     catch(...)
  45.     {
  46.       Application->MessageBox("不能连接Excel!","提示",MB_OK);
  47.       return;
  48.     }
  49.   }
  50.   my_excel.OlePropertySet("Visible",true);
  51.   all_workbooks=my_excel.OlePropertyGet("Workbooks");
  52. //  my_excel.OlePropertySet("SheetsInNewWorkbook",1);
  53.   my_workbook=all_workbooks.OleFunction("Add");
  54.   my_worksheet=my_excel.OlePropertyGet("ActiveSheet");
  55. //
  56.   my_range=my_worksheet.Exec(Range<<"A1");
  57.   my_range.Exec(SetValue<<"输出自己的表");
  58.   Range.ClearArgs();
  59.   SetValue.ClearArgs();
  60. //表头
  61.   CC='A';
  62. //表内容
  63.   pp=2;
  64.   for(jj=0;jj<sgrid->RowCount;jj++)
  65.   {
  66.     for(ii=0;ii<sgrid->ColCount;ii++)
  67.     {
  68.       assn=char(CC+ii);
  69.       assn=assn+IntToStr(pp);
  70.       ass="";
  71.       ass=sgrid->Cells[ii][jj];
  72.       my_range=my_worksheet.Exec(Range<<assn);
  73.       my_range.Exec(SetValue<<ass);
  74.       Range.ClearArgs();
  75.       SetValue.ClearArgs();
  76.     }
  77.     pp++;
  78.   }
  79. }
  80. //---------------------------------------------------------------------------
  81.