解决方案 »
- 有人用过TMS的 AdvStringGrid吗?有个急问题请教。在线等。
- ASTA客户端经常死掉
- ~~~~~~~~~~兄弟姐妹们,别人刚给我给我介绍个女朋友,准备周末出去第一次见面,这大冷天的出去喝点什么好啊,大家教教我吧~~~~~~~~~~~
- OpenDialog能选择文件,我如果想选择文件夹怎么做?
- 请教如何安装 COM 组件?
- bde访问数据库的权限问题,急
- EOleSysError错误,
- 急,SOCKET连接上了也接收到服务端发的数据,但客户端发送数据后再也接收不到数据
- DELPHI 6诞生了,感问DELPHI能完成VC百分之几的工作,是100%吗?问DELPHI高手...
- 为什么用create创建一对象而用destroy销毁对象会产生错误?
- BDE连接ORACLE报错
- 使用Delphi获取Word文档奇偶数页眉页脚
var
RttiType: TRttiType;
RttiProperty: TRttiProperty;
begin
Result := nil;
RttiType := Ref.GetType(aObject.ClassType);
RttiProperty := RttiType.GetProperty(PropName);
if (RttiProperty <> nil) and (RttiProperty.GetValue(aObject).IsObject) and (RttiProperty.GetValue(aObject).AsObject is TCaption) then
begin
Result := TCaption(RttiProperty.GetValue(aObject).AsObject);
end;
end;
非常感谢,但可以详细点吗?小女子刚接触Delphi,ps:字符串是从数据库里取出来的。“把ChartTitle.Font.Name拆分”这个我会的。
但倒也不是很大。例如:Ref.GetType(aObject.ClassType);
这里只要是TObject即可
Result := TCaption(RttiProperty.GetValue(aObject).AsObject);
这里也不一定要知道TCaption,
用RttiProperty.GetValue(aObject).AsObject也可以继续处理下去,
直到最终获得string值为止。
不过楼上给的方法都是针对Property是可以的,但是你这用的是com对象,所以是无效的
我首先把ExcelApp.ActiveSheet.后面的属性都以字符串类型存放在数据库里,要做某道题时,提取出相应的属性,再拼接成ExcelApp.ActiveSheet.Rang['A1:B1'].Font.Name可运行代码去Office上获取答案,这样就不用每道题都在Delphi上写一遍嘛。看样子你是有办法的,能私聊吗?
我首先把ExcelApp.ActiveSheet.后面的属性都以字符串类型存放在数据库里,要做某道题时,提取出相应的属性,再拼接成ExcelApp.ActiveSheet.Rang['A1:B1'].Font.Name可运行代码去Office上获取答案,这样就不用每道题都在Delphi上写一遍嘛。看样子你是有办法的,能私聊吗?
1,如果非要按你原设计,就是要利用QueryInterface这个接口,以及对象名字符串查找出对象,这个我再研究下 一起学习
2,我觉得你没必要实现这么麻烦,其实某道题 你只需记录它在excel上面的 col row 就行了吧
unit Unit2;interfaceuses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type
TCustomFont = class
public
function Name : string;
function Size : integer;
end; TCustomChartTitle = class
private
FFont : TCustomFont;
public
destructor Destroy; override;
function Font : TCustomFont;
end; TCustomChart = class
private
FChartTitle : TCustomChartTitle;
public
destructor Destroy; override;
function ChartTitle : TCustomChartTitle;
end; TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FChart : TCustomChart;
public
{ Public declarations }
end;var
Form2: TForm2;implementation
uses System.Rtti, System.TypInfo;
{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
function Process(const Value : string; AObject : TObject) : TObject;
var
obj: TCustomChart;
rttiType: TRttiType;
rttiMethod: TRttiMethod;
rttiValue : TValue;
begin
result := nil;
rttiType := nil;
rttiMethod := nil;
rttiValue := nil; rttiType := TRttiContext.Create.GetType(AObject.ClassType);
if rttiType = nil then
exit; rttiMethod := rttiType.GetMethod(Value);
if rttiMethod = nil then
exit; rttiValue := rttiMethod.Invoke(AObject, []);
if rttiValue.IsObject then
result := rttiValue.AsObject
else
if rttiValue.TypeInfo^.Kind = tkInteger then
ShowMessage(IntToStr( rttiValue.AsInteger))
else
ShowMessage(rttiValue.AsString);
end;
const
//注意: 如果要反射的字符串和方法名称不相同, 则需要转义
C_Font : array[0..1] of string = ('ChartTitle.Font.Name', 'ChartTitle.Font.Size');
var
i, j : integer;
sList : Tstringlist;
oObj : TObject;
begin
sList := Tstringlist.Create;
FChart := TCustomChart.Create;
try
sList.Delimiter := '.';
for i := Low(C_Font) to High(C_Font) do
begin
oObj := FChart;
sList.DelimitedText := C_Font[i];
for j := 0 to sList.Count - 1 do
begin
oObj := Process(sList.Strings[j], oObj);
if oObj = nil then
Break;
end;
end;
finally
FChart.Free;
sList.Free;
end;
end;{ TCustomFont }function TCustomFont.Size: integer;
begin
Result := 12;
end;function TCustomFont.Name: string;
begin
Result := 'a';
end;{ TCustomChartTitle }destructor TCustomChartTitle.Destroy;
begin
if Assigned(FFont) then
FreeAndNil(FFont); inherited;
end;function TCustomChartTitle.Font: TCustomFont;
begin
if not Assigned(FFont) then
FFont := TCustomFont.Create; Result := FFont;
end;{ TCustomChart }function TCustomChart.ChartTitle: TCustomChartTitle;
begin
if not Assigned(FChartTitle) then
FChartTitle := TCustomChartTitle.Create; Result := FChartTitle;
end;destructor TCustomChart.Destroy;
begin
if Assigned(FChartTitle) then
FreeAndNil(FChartTitle); inherited;
end;end.
比如你执行这句,ExcelApp.ActiveSheet.Rang['A1:B1'].Font.Name delphi是怎么找到这些属性呢?有一份excel, 数据库里存属性名 程序查找数据库中的属性 再执行获取想要的值。
你目前的设计不太好的,1 要存储的属性很多,还有这样真的很怪,设计程序是要找共性,不能这么直接 因为要访问什么东西就直接存起来 2,通过名字访问属性这个没有解决办法 你可以改一下,数据库只用存 某一道题对应excel上的的行列(col row) ,再想获取内容,直接通过行列到excel去找你想要的属性
如果你需要从字符串转成变量的并不多,我也想不到有什么理由你需要excel所有属性,可以参照我下面的这种。以你要获取某个单元格value为例,你数据库中存col row value(你要用到的属性名字)
function getvalue:string;
var
col,row :integer;
valuestr:string; //这三个值从数据库中查
begin
if valuestr='value' then
Result:=ExcelApp.Cells[col,row].Value;
if valuestr='Font.Name' then
Result:=ExcelApp.Cells[col,row].Font.Name;
end;