📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
pTitleList = new TStringList();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete pTitleList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DataToListView(ListView1,"d:\\January.xls");
DataToListView(ListView2,"d:\\January.xls");
GetLsvTitle(pTitleList,ListView1);
cbxCaption->Items->AddStrings(pTitleList);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DataToListView(TListView *LstView,AnsiString XlsName)
{
int Row,Col,ColCount,RowStart;
Variant Ex,Wb,Sh1,ExRows,ExCols;
TListItem *ListItem;
TListColumn *ListColumn;
AnsiString Temp,Str1,Str2,Str3;
Ex=Variant::CreateObject("Excel.Application");
Ex.PS("Visible",(Variant)false);
//Ex.PG("WorkBooks").PR("Open","d:\January.xls");
Ex.PG("WorkBooks").PR("Open",XlsName.c_str());
Wb=Ex.PG("ActiveWorkBook");
Sh1=Wb.PG("ActiveSheet");
try
{
ExCols=Sh1.PG("UsedRange");
ExCols=ExCols.PG("Columns");
ColCount=ExCols.PG("Count");
LstView->Columns->Clear();
for(RowStart=1;true;RowStart++)
{
Str1=Sh1.PG("Cells",RowStart,1).PG("Value");
Str2=Sh1.PG("Cells",RowStart,2).PG("Value");
Str3=Sh1.PG("Cells",RowStart,3).PG("Value");
if(Str1.Trim().IsEmpty()||Str1.Trim().IsEmpty()||Str1.Trim().IsEmpty())
continue;
ListColumn=LstView->Columns->Add();
ListColumn->Caption="序号";
for(int i=1;i<ColCount;i++)
{
ListColumn=LstView->Columns->Add();
ListColumn->Caption=Sh1.PG("Cells",RowStart,i).PG("Value");
}
RowStart++;
break;
}
for(Row=RowStart;true;Row++)
{
ListItem=LstView->Items->Add();
ListItem->Caption=IntToStr(LstView->Items->Count);
for(Col=1;Col<=ColCount;Col++)
{
Temp=Sh1.PG("Cells",Row,Col).PG("Value");
if(Temp.Trim().IsEmpty())
{
ListItem->Delete();
return;
}
else
{
ListItem->SubItems->Add(Temp);
}
}
}
}
__finally
{
//Wb.PR("Close");
//Ex.FN("Quit");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetLsvTitle(TStringList *pTitleList,TListView *LstView)
{
pTitleList->Clear();
for(int i=0;i<LstView->Columns->Count;i++)
{
pTitleList->Add(LstView->Columns->Items[i]->Caption);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddTitle(TListView *LstView,AnsiString Title)
{
(LstView->Columns->Add())->Caption=Title;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AddTitle(lsvData,cbxCaption->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
AddTitle(lsvData,cbxCaption->Text+"【A】");
AddTitle(lsvData,cbxCaption->Text+"【B】");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AddTitle(lsvData,edtRsltCaption->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
TListItem *ListItem;
int DataA,DataB,Result;
AnsiString CacStr,ResultStr;
#define CaculateStr DataA-DataB
ListItem=lsvData->Items->Add();
ListItem->Caption="盖红骄子";
ListItem->SubItems->Add("100"); //A
ListItem->SubItems->Add("200"); //B
DataA=100;
DataB=200;
//CacStr="A-B";
//CacStr="%d-%d";
//ResultStr.printf(CacStr.c_str(),DataA,DataB);
Result=CaculateStr;
ResultStr.printf("%d",Result);
ListItem->SubItems->Add(ResultStr);
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Caculate(int DataA,int DataB,AnsiString CacStr)
{
int Ret;
ResultStr.
return Ret;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -