delphi tips and articles 5

1 判断字符串中的中文字符是否存在等问题。

procedure TForm1.Button1Click(Sender: TObject);
var
  t:string;
  s,s1:WideString;
v:WideChar;
begin
  s  := '_3到8年';
  s1:='年';

v:=s[1];

showmessage(v);


    if ansiContainsText(s,s1[1]) then
    begin
     s:= AnsiReplaceText(s,s1[1],'');
     s1 := '到';
     s := ansireplaceText(s,s1[1],'');
     s := ansireplaceText(s,'_','');
     showmessage(s);
    end;


showmessage(inttostr( Ord(v)));
end;

对WideString而言,s[1]意即:第一个字符。

2String到WideString
http://groups.google.com/group/borland.public.delphi.winapi/browse_frm/thread/7920cdc4c4d4f1ab/2bed31bcb703d614?lnk=st&q=delphi+string+to+widestring&rnum=1#2bed31bcb703d614
I just wasted 4 hours so maybe I can save some of your time.
Assigning a string to a widestring such as in:

var
  wString: wideString;
  nString: string;
begin
  nString := 'hello';
  wString := nString;
end;

works fine UNLESS the nString is greater than 4096 characters, in
which case the assignment to wString results in a empty string.
3 捕捉窗口最小化等类似事件
在Form上放一个TApplicationEvents,然后在它的OnMinimize事件里捕获最小化消息。
TApplicationEvents,在additional面板中
4 几种常用Grid导出到excel

a cxgrid:

uses
    cxExportGrid4Link;

   procedure TForm1.Button1Click(Sender: TObject);
   begin
    ExportGrid4ToEXCEL('d:/wang.xsl',cxGrid1,True,True);
    ExportGrid4ToTEXT('d:/wang.txt',cxGrid1,True,True);
    ExportGrid4ToXML('d:/wang.xml',cxGrid1,True,True);
    ExportGrid4ToHTML('d:/wang.html',cxGrid1,True,True);
   end;

b dbgrid和stringgrid

procedure TDM.CopyDbDataToExcel(Target: TDBGridEh;mb,FileName: string);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
 XLApp.DisplayAlerts := False;
 XLApp.Quit;
 VarClear(XLApp);
end;
//--------------------------选择模板------
// CopyFile(pChar(Trim(ExtractFilePath(Application.ExeName))+mb+'.xls'),pChar(FileName+'1.xls'),false);
//------------------------
//通过ole创建Excel对象
try
 XLApp := CreateOleObject('Excel.Application');
except
 Screen.Cursor := crDefault;
 Exit;
end;
if mb = '统计-项目信息前' then
  XLApp.WorkBooks.Add[Trim(ExtractFilePath(Application.ExeName))+mb+'.xls']    //你要把数据放在那里啊,先生成个文件在e:/1.xls
else
 XLApp.WorkBooks.Add;
XLApp.WorkBooks[1].WorkSheets[1].Name := 'sheet1';
Sheet := XLApp.Workbooks[1].WorkSheets['sheet1'];
if not Target.DataSource.DataSet.Active then
begin
 Screen.Cursor := crDefault;
 Exit;
end;
Target.DataSource.DataSet.first;

for iCount := 0 to Target.Columns.Count - 1 do
begin
 Sheet.cells[1, iCount + 1] := trim(Target.Columns.Items[iCount].Title.Caption);
end;

jCount := 1;
while not Target.DataSource.DataSet.Eof do
begin

 for iCount := 0 to Target.Columns.Count - 1 do   //
 begin
  if iCount = 0 then
   Sheet.cells[jCount + 1, iCount + 1] := IntToStr(jCount)
  else
   Sheet.cells[jCount + 1, iCount + 1] := trim(Target.Columns.Items[iCount].Field.AsString);
 end;
Inc(jCount);
Target.DataSource.DataSet.Next;
end;

//--------------------可以在此添加Excel的宏-----------

XLApp.ActiveWorkbook.SaveAs(FileName:=FileName);                                                                                                            ;
Screen.Cursor := crDefault;
XLApp.ActiveWorkbook.Close;
end;
-----------------------------------------StringGridToExcel

procedure TDM.CopySGDataToExcel(Target: TStringGrid;FileName: string);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;
//通过ole创建Excel对象
try
XLApp := CreateOleObject('Excel.Application');
except
Screen.Cursor := crDefault;
Exit;
end;
XLApp.WorkBooks.Add;    //你要把数据放在那里啊,先生成个文件在e:/1.xls
XLApp.WorkBooks[1].WorkSheets[1].Name := 'sheet1';
Sheet := XLApp.Workbooks[1].WorkSheets['sheet1'];
Target.Row := 0;
for iCount := 0 to Target.RowCount - 1 do
begin
 for jCount := 0 to Target.ColCount - 1 do
   Sheet.cells[iCount + 1, jCount + 1] := Target.Cells[jCount,iCount];
 Target.Row := iCount;
end;
XLApp.ActiveWorkbook.SaveAs(FileName:=FileName);
Screen.Cursor := crDefault;
XLApp.ActiveWorkbook.Close;
end;


 

dbgridtoexcel参考2:

procedure CopyDbDataToExcel(Target: TDbgrid);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;
//通过ole创建Excel对象
try
XLApp := CreateOleObject('Excel.Application');
except
Screen.Cursor := crDefault;
Exit;
end;
XLApp.WorkBooks.Add;
XLApp.WorkBooks[1].WorkSheets[1].Name := '测试工作薄';
Sheet := XLApp.Workbooks[1].WorkSheets['测试工作薄'];
if not Target.DataSource.DataSet.Active then
begin
Screen.Cursor := crDefault;
Exit;
end;
Target.DataSource.DataSet.first; 

for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[1, iCount + 1] := Target.Columns.Items[iCount].Title.Caption;
end;
jCount := 1;
while not Target.DataSource.DataSet.Eof do
begin
for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[jCount + 1, iCount + 1] := Target.Columns.Items[iCount].Field.AsString;
end;
Inc(jCount);
Target.DataSource.DataSet.Next;
end;
XlApp.Visible := True;
Screen.Cursor := crDefault;
end;

 

 

5 TStringList使用object

http://forums.devshed.com/delphi-programming-90/namevaluecollection-equivalent-in-delphi-6-0t-299165.html

1.Note that TStringList has an AddObject method, as well as an Add method. You can use AddObject to associate an object with a string.
2. The trick with AddObject is that the second object must be a descendant of TObject. This is no problem, since we can declare our value type as a TObject descendant:

Code:
type:
TMyValue = class(TObject)
public
nValue : integer;
end;

This is assuming that the data we want to store (nValue) is an integer. It can be a more complex type if you like (say, a bunch of values).
3. Then to add an item to the StringList, do something like this:
Code:
procedure TForm1.AddNameValue(sName: string; nValue: integer);
var
oMyValue : TMyValue;
begin
oMyValue := TMyValue.Create;
oMyValue.nValue := nValue;
stl.AddObject(sName, oMyValue);
end;

4. Now to get a value off the StringList, you can get the object associated with the string and cast it back to a TMyValue and extract the values from the object. It might be faster if you set StringList.sorted := true, so that the stringlist is sorted to start with.
5. To clear the list, you go through each entry in the stringlist, get the object (via the StringList.Objects[i] method) and free each one first. Then clear the stringlist.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值