Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:2k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <comobj.hpp>
- #include <utilcls.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- TStringGrid *sgrid;
- int ii,jj,pp;
- int CC;
- AnsiString ass,assn;
- Variant my_excel;
- Variant all_workbooks;
- Variant my_workbook;
- Variant my_worksheet;
- Variant my_range;
- //
- PropertyGet Range("Range");
- PropertySet SetValue("Value");
- //
- sgrid=StringGrid1;
- //
- try
- {
- my_excel=GetActiveOleObject("excel.application");
- }
- catch(...)
- {
- try
- {
- my_excel=CreateOleObject("excel.application");
- }
- catch(...)
- {
- Application->MessageBox("不能连接Excel!","提示",MB_OK);
- return;
- }
- }
- my_excel.OlePropertySet("Visible",true);
- all_workbooks=my_excel.OlePropertyGet("Workbooks");
- // my_excel.OlePropertySet("SheetsInNewWorkbook",1);
- my_workbook=all_workbooks.OleFunction("Add");
- my_worksheet=my_excel.OlePropertyGet("ActiveSheet");
- //
- my_range=my_worksheet.Exec(Range<<"A1");
- my_range.Exec(SetValue<<"输出自己的表");
- Range.ClearArgs();
- SetValue.ClearArgs();
- //表头
- CC='A';
- //表内容
- pp=2;
- for(jj=0;jj<sgrid->RowCount;jj++)
- {
- for(ii=0;ii<sgrid->ColCount;ii++)
- {
- assn=char(CC+ii);
- assn=assn+IntToStr(pp);
- ass="";
- ass=sgrid->Cells[ii][jj];
- my_range=my_worksheet.Exec(Range<<assn);
- my_range.Exec(SetValue<<ass);
- Range.ClearArgs();
- SetValue.ClearArgs();
- }
- pp++;
- }
- }
- //---------------------------------------------------------------------------
-