典型的思路是先从文本文件中分割、读取信息,然后通过SQL语句将数据保存如数据库,但是这种方法的执行效率比较低,好象以前看到过将文本文件直接倒入数据库的,但是对文件的格式有要求,你的这个,恐怕不行。

解决方案 »

  1.   

    这是增值税开票系统接口导出的数据格式,我需要对这些数据再次使用,但是找不到合适的方法。
    heluqing老师你的执行效率低的方法能否给于阐述一下?
      

  2.   

    简单一些,把文本文件中的空格(或者是TAB制表符)用逗号替换,这样的文件可以看做上CSV文件,可以用EXCEL打开,EXCEL应该可以导入数据库中的
      

  3.   

    1.读取该文本文件
    2.依次保存记录到数据库(就不说了)下面是以前写过的读取CSV类.可以读取换行过的记录,且对引号做过处理.
    type
      TCSVRead = class
      private
        FDataList: TList;
        FCurrent: Integer;
        function unUsualRow(sLine: string): boolean;
        function LoadHead(stCSV: TStringList): boolean;
        function GetData: TStringList;
        function GetCount: Integer;
      public
        Head: TStringList;
        property Data: TStringList read GetData;
        property Count: integer read GetCount;
        Constructor Create;
        Destructor Destroy;override;
        procedure LoadCSV(FileName: string);
        function First: boolean;
        function Next: boolean;
        function Eof: Boolean;
      end;
    //实现
    { TCSVRead }constructor TCSVRead.Create;
    begin
      FDataList := TList.Create;
      Head := TStringList.Create;
    end;destructor TCSVRead.Destroy;
    var
      I: integer;
    begin
      if Assigned(Head) then
        Head.Free;
      for I := 0 to FDataList.Count - 1 do
        TStringList(FDataList[I]).Free;
      FDataList.Free;
      inherited;
    end;function TCSVRead.Eof: Boolean;
    begin
      if FCurrent >= Count  then
        Result := true
      else
        Result := false;
    end;function TCSVRead.First: boolean;
    begin
      if Count > 0 then
        FCurrent := 0;
      Result := true;
    end;function TCSVRead.GetCount: Integer;
    begin
      Result := FDataList.Count;
    end;function TCSVRead.GetData: TStringList;
    begin
      try
        if (FCurrent >= 0) and (FCurrent < Count) then
          Result := FDataList[FCurrent]
        else
          Result := nil;
      except
        Result := nil;
      end;
    end;procedure TCSVRead.LoadCSV(FileName: string);
    var
      stCSV: TStringList;
      I,Index,Count: integer;
      st: TstringList;
      sLine: string;
      LineEOF,bReadNext: boolean;
    begin
      if Trim(FileName) = '' then
        raise SysUtils.Exception.Create('请指定CSV文件');  try
        FDataList.Clear;
        FCurrent := -1;
        try
          stCSV := TStringList.Create;
          stCSV.LoadFromFile(Filename);
        except
          //无法读取文件
        end;
        Count := stCSV.Count;
        if Count > 1 then
          LoadHead(stCSV);    I := 1;
        while I < Count do begin
          st := TstringList.Create;      LineEOF := false;
          bReadNext := true;
          sLine := stCSV[I];
          while not LineEOF do begin
            //判断本行格式是不是不正常
            if unUsualRow(sLine) then
              bReadNext := false;
            if sLine[1] = '"' then begin
              sLine := copy(sLine,2,length(sLine));
              repeat begin
                Index := pos('",',sLine);
                if Index > 0 then begin  // 找到 “, 说明字段为双引号引起来的,且是在中间的字段
                  st.Add(copy(sLine,1,Index - 1));
                  sLine := copy(sLine,Index + 2,Length(sLine));
                  break;
                end
                else begin //否则 直接 查看最后位是不是 "
                  if (sLine[Length(sLine)] = '"') then begin //是 “ 说明该字段是本行最后一个字段
                    st.Add(copy(sLine,1,Length(sLine) - 1));
                    LineEOF := true;
                    break;
                  end
                  else begin
                    if (not bReadNext) then begin  //本行格式有错误,不再读取下面行,因为会影响后面读取数据
                      st.Add(copy(sLine,1,Length(sLine)));
                      LineEOF := true;
                      break;
                    end
                    else begin //存在换行符,要再读取下一行
                        Inc(I);
                        sLine := sLine + ' ' + stCSV[I];
                    end;
                  end;
                end;
              end;
              until (false);
            end
            else begin
              Index := pos(',',sLine);
              if Index = 0 then begin
                st.Add(copy(sLine,1,Length(sLine)));
                LineEOF := true;
              end
              else begin
                st.Add(copy(sLine,1,Index - 1));
                sLine := copy(sLine,Index + 1,Length(sLine));
              end;
            end;
            if Length(sLine) = 0 then begin  //为空表示 最后的字段 值为 空
              LineEOF := true;
              st.Add('');
            end;
          end;
          inc(I);
          FDataList.Add(st);
        end;
        First;
      except
        raise SysUtils.Exception.Create('CSV文件读取失败');
      end;
      stCSV.Free;
    end;function TCSVRead.LoadHead(stCSV: TStringList): boolean;
    var
      Index: integer;
      sLine: string;
      LineEOF: boolean;
    begin
      Head.Clear;
      sLine := stCSV[0];
      LineEOF := false;  while (not LineEOF) or (Length(sLine) = 0) do begin
        Index := pos(',',sLine);
        if Index = 0 then begin
          Head.Add(copy(sLine,1,Length(sLine)));
          LineEOF := true;
        end
        else begin
          Head.Add(copy(sLine,1,Index - 1));
          sLine := copy(sLine,Index + 1,Length(sLine));
        end;
      end;
      Result := true;end;function TCSVRead.Next: boolean;
    begin
      Result := false;
      if Count > 0 then begin
        if FCurrent < Count then
          inc(FCurrent);
        Result := true;
      end;
    end;function TCSVRead.unUsualRow(sLine: string): boolean;
    var
      Index1,Index2: integer;
    begin
      Result := false;
      Index1 := pos('"',sLine);
      Index2 := pos('",',sLine);
      if (Index2 > 0) and (Index2 <= Index1) then
        Result := true;
    end;
      

  4.   

    select * from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
    DefaultDir=c:\temp;','select * from aaaa.txt')
      

  5.   

    可以用dx控件,可以直接将txt文件load到dbgrid里