请各位大大帮忙看看代码~~
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1_Txt.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include<FileCtrl.hpp>
#define PG OlePropertyGet
#define PS OlePropertySet
#define PR OleProcedure
#define FN OleFunction
TFrm_Main *Frm_Main;
TStringList *C_File=new TStringList;
Variant ExcelApp,WorkBook,Sheet;
TSearchRec F;
AnsiString dir;
int n=0;
int j=6;
AnsiString temp,temp1,temp2,temp3,temp4,temp5,temp6;
//---------------------------------------
void Search(AnsiString w)
{
TSearchRec F;
if(FindFirst(w+"\\*.*",faAnyFile,F)==0)
do
{
if((F.Name!=".") && (F.Name!=".."))//暗ヘ魁縵匡
{
if(F.Attr!=faDirectory) //狦ぃ琌ゅンЖ妮┦
{
if(F.Name.Pos(".txt") > 0)//縵匡ゅンЖńTXT郎
{
AnsiString FileName=w+"\\"+F.Name;
C_File->LoadFromFile(FileName);
n++;//﹚竡跑秖i穓碝TXT郎糤Ω
}
}
else
{
Search(w+"\\"+F.Name);//狦琌ゅンЖ妮┦穓碝ゅンЖ齙
}
}
}
while(FindNext(F)==0);
FindClose(F);
Frm_Main->Panel1->Caption=n;
}
void write()
{
int p=0;
for(int i=0;i<C_File->Count;i++)
{
if(C_File->Strings[i].Pos("Passed Tests")>0)
{
break;
}
else if((C_File->Strings[i].Pos("===")==0)&&(C_File->Strings[i].Trim()!=""))
{
temp1 = C_File->Strings[i].SubString(1,4);
Sheet.PG("Cells",p+1,1).PS("Value",temp1.c_str());
temp2 = C_File->Strings[i].SubString(6,4);
Sheet.PG("Cells",p+1,2).PS("Value",temp2.c_str());
temp3 = C_File->Strings[i].SubString(10,4);
Sheet.PG("Cells",p+1,3).PS("Value",temp3.c_str());
temp4 = C_File->Strings[i].SubString(14,31);
Sheet.PG("Cells",p+1,4).PS("Value",temp4.c_str());
temp5 = C_File->Strings[i].SubString(56,13);
Sheet.PG("Cells",p+1,5).PS("Value",temp5.c_str());
temp6 = C_File->Strings[i].SubString(69,10);
Sheet.PG("Cells",p+1,6).PS("Value",temp6.c_str());
p++;
}
}
}
__fastcall TFrm_Main::TFrm_Main(TComponent* Owner)
: TForm(Owner)
{
ExcelApp=Variant::CreateObject("Excel.Application");
ExcelApp.PS("Visible",false);
ExcelApp.PG("Workbooks").FN("Add");
WorkBook=ExcelApp.PG("ActiveWorkbook");
WorkBook.PG("Sheets",1).PG("Select");
Sheet=WorkBook.PG("ActiveSheet");
Btn_write->Enabled=false;
Btn_Save->Enabled=false;
}
void Add(AnsiString w)//﹚竡ㄧ计龟瞷穓碝
{
int Position=0;
TSearchRec F;
AnsiString temp;
if(FindFirst(w+"\\*.*",faAnyFile,F)==0)
do
{
if((F.Name!=".") && (F.Name!=".."))//暗ヘ魁縵匡
{
if(F.Attr!=faDirectory) //狦ぃ琌ゅンЖ妮┦
{
if(F.Name.Pos(".txt") > 0)//縵匡ゅンЖńTXT郎
{
AnsiString FileName=w+"\\"+F.Name;
C_File->LoadFromFile(FileName);
int p=0;
for(int i=1;i<C_File->Count;i++)
{
if(C_File->Strings[i].Pos("Passed Tests")>0)
{
break;
}
else if((C_File->Strings[i].Pos("===")==0)&&(C_File->Strings[i].Trim()!=""))
{
temp = C_File->Strings[i].SubString(45,11);
Sheet.PG("Cells",p+2,j+1).PS("Value",temp.c_str());
Sheet.PG("Cells",1,j+1).PS("Value",F.Name.SubString(1,18).c_str());
p++;
}
}
j++;
Frm_Main->ProgressBar1->Max=n;
Position++;
Frm_Main->ProgressBar1->Position++;
Frm_Main->Panel2->Caption=Position+1;
}
}
else
{
Add(w+"\\"+F.Name);//狦琌ゅンЖ妮┦穓碝ゅンЖ齙
}
}
}
while(FindNext(F)==0);
FindClose(F);
}
//---------------------------------------
void __fastcall TFrm_Main::Btn_RunClick(TObject *Sender)
{
SelectDirectory(AnsiString("Select a Folder"),"",dir);
Search(dir);
Btn_write->Enabled=true;
}
//---------------------------------------
void __fastcall TFrm_Main::Btn_SaveClick(TObject *Sender)
{
ExcelApp.FN("Save");
ExcelApp.FN("Quit");
}
//---------------------------------------
void __fastcall TFrm_Main::Btn_writeClick(TObject *Sender)
{
write();
Add(dir);
Btn_Save->Enabled=true;
ShowMessage("succeed Loading File");
}
//---------------------------------------
void __fastcall TFrm_Main::FormClose(TObject *Sender, TCloseAction &Action)
{
ExcelApp.FN("Quit");
}
//---------------------------------------
主要是把文件夹里的TXT搜索出来,再导入到Excel当中去,加个ProgressBar显示进度,但我不知道怎样将进度条上显示百分比,还有就是每导入完一个TXT我想在Panel2中显示当前导入完成的个数,这个数据还可以供ProgressBar 显示百分比用~~~
[解决办法]
Application->ProcessMessages();//加上这句就可以了
用这个解决你那个问题