BCB抓Excel容出??
以下是改前的提供的code:
//---------------------------------------
#include <vcl.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)
{
//需使用OpenDialog1, Memo1
AnsiString filepath;
if(OpenDialog1->Execute()) //若案成功
{
filepath= OpenDialog1->FileName.c_str(); //把案路存 filepath
}
Memo1->Lines->Add("filepath="+filepath ); //印出路
//declaration of variables of type variant 宣告
Variant XL,v0,v1,vcell,i,j;
//a string where you will temporarily put the content of a single Cell 存Excel格料
AnsiString tmp;
//create an object which is an excel application and store it to XL
XL=Variant::CreateObject("excel.application");
//Set the Excel Application as invisible once you have opened it Excel用程序置不可
XL.OlePropertySet("Visible",false);
//Get the workbooks while has a path stored in “file” variable and open it.
XL.OlePropertyGet("Workbooks").OleProcedure("Open", filepath.c_str() );
//Get the Sheet which has a title “Sheet1〃
v0=XL.OlePropertyGet("Sheets","Sheet1");
//Get the Cells of that particular Sheet. 取Excel格料表。
v1=v0.OlePropertyGet("Cells");
//Get the content of the Cell located at row i and column j 取容的胞位於第i行第j
for(i=10;i<=488,i++)
{
for(j=2;j<=4;j++)
}
vcell=v1.OlePropertyGet("Item",i,j); //定要取的位置 at row i and column j
}
}
//store that content to ansistring “tmp”
tmp=vcell.OlePropertyGet("Value");
//重 上行定要的字
//印出
Memo1->Lines->Add("xls[i][j]="+tmp );
XL.OleProcedure("Quit"); //案
XL=Unassigned;
}
//---------------------------------------希望可以用loop出Excel的每cell料
是哪出??
[解决办法]
循环中,一个低级错误!
for(i=10;i<=488,i++)
{
for(j=2;j<=4;j++)
}
vcell=v1.OlePropertyGet("Item",i,j); ////定要取的位置 at row i and column j
//下面两行代码移动到循环里面
tmp=vcell.OlePropertyGet("Value");
Memo1->Lines->Add("xls[i][j]="+tmp );
}
}