⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qimport2common.pas

📁 Delphi Advanced Import Component_v2.48.With Full Source.
💻 PAS
📖 第 1 页 / 共 4 页
字号:
    end;

    is_separator := Str[i] = Separator;
    if is_separator and not in_quote then
    begin
      AStrings.Add(sss);
      is_first := true;
      sss := EmptyStr;
      Continue;
    end;

    if wait_separator then
      if is_separator then
      begin
        wait_separator := false;
        is_first := true;
        AStrings.Add(sss);
        Continue;
      end
      else begin
        Continue;
      end;

    is_quote := (Quote <> #0) and (Str[i] = Quote);

    if quote_in_quote then
    begin
      if is_quote then
      begin
        sss := sss + Quote;
        quote_in_quote := false;
        Continue;
      end
      else if is_separator then
      begin
        quote_in_quote := false;
        in_quote := false;
        is_first := true;
        AStrings.Add(sss);
        Continue;
      end
      else begin
        wait_separator := true;
        quote_in_quote := false;
        Continue;
      end;
    end;

    if is_quote and quote_in_quote then
    begin
      sss := sss + Quote;
      Continue;
    end;

    if is_quote and in_quote then
    begin
      quote_in_quote := true;
      Continue;
    end;

    sss := sss + Str[i];
  end;
  AStrings.Add(sss);
end;

{procedure CSVStringToStrings(const Str, LeftQuotation,
  RightQuotation, Separator: string; AStrings: TStrings);
var
  S, SS, Quot: string;
  P: integer;
begin
  AStrings.Clear;
  S := Str;
  while Length(S) > 0 do begin
    Quot := LeftQuotation;
    if (Quot <> EmptyStr) and
       (AnsiCompareStr(Quot, Copy(S, 1, Length(Quot))) = 0) then begin
      Delete(S, 1, Length(Quot));
      if RightQuotation <> EmptyStr then Quot := RightQuotation;
      P := Pos(Quot, S);
      if P = 0 then begin
        AStrings.Add(S);
        S := EmptyStr;
      end
      else begin
        SS := Copy(S, 1, P - 1);
        Delete(S, 1, P + Length(Quot) - 1);
        if S <> EmptyStr then begin
          P := Pos(Separator, S);
          if P = 0 then begin
            AStrings.Add(SS + S);
            S := EmptyStr;
          end
          else begin
            AStrings.Add(SS + Copy(S, 1, P - 1));
            Delete(S, 1, P + Length(Separator) - 1);
          end;
        end
        else begin
          AStrings.Add(SS);
        end;
      end;
    end
    else begin
      P := Pos(Separator, S);
      if P = 0 then begin
        AStrings.Add(S);
        S := EmptyStr;
      end
      else begin
        AStrings.Add(Copy(S, 1, P - 1));
        Delete(S, 1, P + Length(Separator) - 1);
      end;
    end;
  end;
end;}

function GetListSeparator: Char;
begin
  {$IFDEF WIN32}
  Result := GetLocaleChar(GetThreadLocale, LOCALE_SLIST, ',');
  {$ELSE}
  Result := ',';
  {$ENDIF}
end;

procedure ClearIniFile(IniFile: TIniFile);
var
  AStrings: TStrings;
  i: integer;
begin
  AStrings := TStringList.Create;
  try
    IniFile.ReadSections(AStrings);
    for i := 0 to AStrings.Count - 1 do
      IniFile.EraseSection(AStrings[i]);
  finally
    AStrings.Free;
  end;
end;

// ImportDestination routines
procedure QImportCheckDestination(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF});
begin
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet:
      if not Assigned(DataSet) then raise EQImportError.Create({$IFDEF WIN32}QImportLoadStr(QIE_NoDataSet){$ENDIF}
                                                               {$IFDEF LINUX}QIE_NoDataSet{$ENDIF});
    {$IFNDEF NOGUI}
    qidDBGrid:
      if not Assigned(DBGrid) then raise EQImportError.Create({$IFDEF WIN32}QImportLoadStr(QIE_NoDBGrid){$ENDIF}
                                                              {$IFDEF LINUX}QIE_NoDBGrid{$ENDIF});
    qidListView:
      if not Assigned(ListView) then raise EQImportError.Create({$IFDEF WIN32}QImportLoadStr(QIE_NoListView){$ENDIF}
                                                                {$IFDEF LINUX}QIE_NoListView{$ENDIF});
    qidStringGrid:
      if not Assigned(StringGrid) then raise EQImportError.Create({$IFDEF WIN32}QImportLoadStr(QIE_NoStringGrid){$ENDIF}
                                                                  {$IFDEF LINUX}QIE_NoStringGrid{$ENDIF});
    {$ENDIF}  
  end;
end;

function QImportIsDestinationActive(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF}): boolean;
begin
  Result := false;
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: Result := DataSet.Active;
    {$IFNDEF NOGUI}
    qidDBGrid:
      Result := Assigned(DBGrid.DataSource) and Assigned(DBGrid.DataSource.DataSet)and
        DBGrid.DataSource.DataSet.Active;
    else Result := true;
    {$ENDIF}
  end;
end;

procedure QImportIsDestinationOpen(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF});
begin
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: DataSet.Open;
    {$IFNDEF NOGUI}
    qidDBGrid:
      if Assigned(DBGrid.DataSource) and
         Assigned(DBGrid.DataSource.DataSet) then
        DBGrid.DataSource.DataSet.Open;
    {$ENDIF}	
  end;
end;

procedure QImportIsDestinationClose(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF});
begin
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: DataSet.Close;
    {$IFNDEF NOGUI}
    qidDBGrid:
      if Assigned(DBGrid.DataSource) and
         Assigned(DBGrid.DataSource.DataSet) then
        DBGrid.DataSource.DataSet.Close;
    {$ENDIF}	
  end;
end;

function QImportDestinationColCount(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF}): integer;
begin
  Result := 0;
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: Result := DataSet.FieldCount;
    {$IFNDEF NOGUI}
    qidDBGrid: Result := DBGrid.Columns.Count;
    qidListView: Result := ListView.Columns.Count;
    qidStringGrid: Result := StringGrid.ColCount;
    {$ENDIF}
  end;
end;

function QImportDestinationColName(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet; 
  {$IFNDEF NOGUI}DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid;
 GridCaptionRow,{$ENDIF} Index: integer): string;
begin
  Result := EmptyStr;
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: Result := DataSet.Fields[Index].FieldName;
    {$IFNDEF NOGUI}
    qidDBGrid: Result := DBGrid.Columns[Index].Title.Caption;
    qidListView: Result := ListView.Columns[Index].Caption;
    qidStringGrid:
      if (GridCaptionRow > -1) and (GridCaptionRow < StringGrid.RowCount) then
        Result := StringGrid.Cells[Index, GridCaptionRow]
      else Result := IntToStr(Index);
    {$ENDIF}  
  end;
end;

function QImportDestinationAssigned(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet
  {$IFNDEF NOGUI}; DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid{$ENDIF}): boolean;
begin
  Result := false;
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet:    Result := Assigned(DataSet);
    {$IFNDEF NOGUI}
    qidDBGrid:     Result := Assigned(DBGrid);
    qidListView:   Result := Assigned(ListView);
    qidStringGrid: Result := Assigned(StringGrid);
    {$ENDIF}
  end;
end;

function QImportDestinationFindColumn(IsCSV: boolean;
  ImportDestination: TQImportDestination; DataSet: TDataSet; 
  {$IFNDEF NOGUI}DBGrid: TDBGrid;
 ListView: TListView; 
  StringGrid: TStringGrid; GridCaptionRow: integer;
{$ENDIF}
  const ColName: string): integer;
var
  Field: TField;
{$IFNDEF NOGUI}
  i: integer;
{$ENDIF}
begin
  Result := -1;
  if IsCSV then Exit;
  case ImportDestination of
    qidDataSet: begin
      Field := DataSet.FindField(ColName);
      if Assigned(Field)  then
         Result := Field.Index;
    end;
    {$IFNDEF NOGUI}
    qidDBGrid:
      for i := 0 to DBGrid.Columns.Count - 1 do
        if AnsiCompareText(DBGrid.Columns[i].Title.Caption, ColName) = 0 then begin
          Result := i;
          Exit;
        end;
    qidListView:
      for i := 0 to ListView.Columns.Count - 1 do
        if AnsiCompareText(ListView.Columns[i].Caption, ColName) = 0 then begin
          Result := i;
          Exit;
        end;
    qidStringGrid: begin
      i := StrToIntDef(ColName, -1);
      if i > -1 then begin
        if  i < StringGrid.ColCount then Result := i;
      end
      else begin
        if GridCaptionRow > -1 then
          for i := 0 to StringGrid.ColCount - 1 do
            if AnsiCompareStr(StringGrid.Cells[i, GridCaptionRow], ColName) = 0 then begin
              Result := i;
              Exit;
            end;
      end;
    end;
    {$ENDIF}
  end;
end;

function IncludePathDelimiter(const S: string): string;
begin
  Result := S;
  if not IsPathDelimiter(Result, Length(Result)) then
    Result := Result + PathDelim;
end;

function GetNumericString(const Str: string): string;
var
  p: Integer;
begin
  Result := Str;
  p := Pos(CurrencyString, Result);
  if p > 0 then
    Delete(Result, p, 1);
  p := Pos(ThousandSeparator, Result);
  while p > 0 do
  begin
    Delete(Result, p, 1);
    p := Pos(ThousandSeparator, Result);
  end;
end;

{$IFDEF WIN32}
function FormatStrToDateTime(ADateTimeStr, AFormatStr: string): TDateTime;

  function NormalizeSubString(const ASubStr: string): string;
  var
    i: Integer;
  begin
    Result := '';
    for i := 1 to Length(ASubStr) do
    begin
      if StrToIntDef(ASubStr[i], -1) <> -1 then
        Result := Result + ASubStr[i];
    end;
  end;

var
  i, j: Integer;
  st: TSystemTime;
  fChar, subStr: string;
begin
  Result := 0;

  if (Length(ADateTimeStr) = 0) or (Length(AFormatStr) < 5) then Exit;

  AFormatStr := UpperCase(AFormatStr);

  st.wHour := 0;
  st.wMinute := 0;
  st.wSecond := 0;
  st.wMilliSeconds := 0;

  while Length(AFormatStr) > 0 do
  begin
    fChar := Copy(AFormatStr, 1, 1);

    if fChar = 'Y' then
    begin
      i := Pos('YYYY', AFormatStr);
      if (i > 0) and (i < Length(ADateTimeStr)) then
      begin
        subStr := Copy(ADateTimeStr, i, 4);
        st.wYear := StrToIntDef(subStr, 0);
        if st.wYear = 0 then
        begin
          subStr := NormalizeSubString(subStr);
          st.wYear := StrToIntDef(subStr, 0);
          if st.wYear = 0 then Exit;
        end;
        if (Length(ADateTimeStr) < Length(subStr)) or
          (Length(AFormatStr) < 4) then Exit;
        Delete(ADateTimeStr, 1, Length(subStr));
        Delete(AFormatStr, 1, 4);
      end
      else begin
        i := Pos('YY', AFormatStr);
        if (i > 0) and (i < Length(ADateTimeStr)) then
        begin
          subStr := Copy(ADateTimeStr, i, 2);
          st.wYear := StrToIntDef(subStr, 0);
          if st.wYear = 0 then
          begin
            subStr := NormalizeSubString(subStr);
            st.wYear := StrToIntDef(subStr, 0);
            if st.wYear = 0 then Exit;
          end;
          st.wYear := 2000 + st.wYear;
          if (Length(ADateTimeStr) < Length(subStr)) or
            (Length(AFormatStr) < 2) then Exit;
          Delete(ADateTimeStr, 1, Length(subStr));
          Delete(AFormatStr, 1, 2);
        end
        else begin
          i := Pos('Y', AFormatStr);
          if (i > 0) and (i <= Length(ADateTimeStr)) then
          begin
            subStr := Copy(ADateTimeStr, i, 1);
            st.wYear := StrToIntDef(subStr, 0);
            if st.wYear = 0 then
            begin
              subStr := NormalizeSubString(subStr);
              st.wYear := StrToIntDef(subStr, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -