首页  编辑  

数据集和文本间转换

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:
(* //
  标题:数据集和文本间转换
  说明:不支持多行文本类型和其他二进制类型
  设计:Zswang
  日期:2002-01-24
  支持:wjhu111@21cn.com
  // *)
/// ////Begin Source
function StrLeft(const mStr: string; mDelimiter: string): string;
{ 返回左分隔字符串 }
begin
    Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }

function ListCount(mList: string; mDelimiter: string = ','): Integer;
{ 返回列表数 }
var
    I, L: Integer;
begin
    Result := 0;
    if mList = '' then
        Exit;
    L := Length(mList);
    I := Pos(mDelimiter, mList);
    while I > 0 do
    begin
        mList := Copy(mList, I + Length(mDelimiter), L);
        I := Pos(mDelimiter, mList);
        Inc(Result);
    end;
    Inc(Result);
end; { ListCount }

function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
{ 返回列表指定位置的元素 }
var
    I, L, K: Integer;
begin
    L := Length(mList);
    I := Pos(mDelimiter, mList);
    K := 0;
    Result := '';
    while (I > 0) and (K <> mIndex) do
    begin
        mList := Copy(mList, I + Length(mDelimiter), L);
        I := Pos(mDelimiter, mList);
        Inc(K);
    end;
    if K = mIndex then
        Result := StrLeft(mList + mDelimiter, mDelimiter);
end; { ListValue }

function DataSetToText(mDataSet: TDataSet; mStrings: TStrings; mDelimiter: string = #9): Boolean; { 返回数据集转换到文本中是否成功 }
var
    vBookmark: string;
    I: Integer;
    S: string;
begin
    Result := False;
    if (not Assigned(mDataSet)) or (not mDataSet.Active) or (not Assigned(mStrings)) then
        Exit;
    mStrings.Text := StringReplace(Trim(mDataSet.FieldList.Text), #13#10, mDelimiter, [rfReplaceAll]);
    vBookmark := mDataSet.Bookmark;
    mDataSet.DisableControls;
    try
        mDataSet.First;
        while not mDataSet.Eof do
        begin
            S := '';
            for I := 0 to mDataSet.FieldList.Count - 1 do
                S := S + mDelimiter + mDataSet.FieldList[I].AsString;
            Delete(S, 1, Length(mDelimiter));
            mStrings.Add(S);
            mDataSet.Next;
        end;
    finally
        mDataSet.Bookmark := vBookmark;
        mDataSet.EnableControls;
    end;
    Result := True;
end; { DataSetToText }

function TextToDataSet(mStrings: TStrings; mDataSet: TDataSet; mDelimiter: string = #9): Boolean; { 返回文本转换到数据集中是否成功 }
var
    I, J, C: Integer;
    vFieldNames: string;
begin
    Result := False;
    if (not Assigned(mDataSet)) or (not mDataSet.Active) or (mStrings.Count <= 0) then
        Exit;
    vFieldNames := mStrings[0];
    C := ListCount(vFieldNames, mDelimiter);
    for I := 1 to mStrings.Count - 1 do
        try
            mDataSet.Append;
            for J := 0 to C - 1 do
                if mDataSet.FieldList.IndexOf(ListValue(vFieldNames, J, mDelimiter)) >= 0 then
                    mDataSet[ListValue(vFieldNames, J, mDelimiter)] := ListValue(mStrings[I], J, mDelimiter);
            mDataSet.Post;
        except
            Exit;
        end;
    Result := True;
end; { TextToDataSet }

/// ////End Source
/// ////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
    DataSetToText(Table1, Memo1.Lines);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    TextToDataSet(Memo1.Lines, Table1);
end;
/// ////End Demo