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 );
}
}