Solved

URGENT!!! Export to CSV file using Delphi 7

Posted on 2004-10-26
1,184 Views
Last Modified: 2010-04-05
Hi experts!

I need to export my oracle data to a csv file. How do I do that? I can import the data but I don't know how to export it. I know it should be the opposite way to do it but how?

 if OpenDialog.Execute then
    begin
      //read the CSV file into a text string
      if FileExists(OpenDialog.FileName) then
      begin

        AssignFile(CSVFile, OpenDialog.FileName);
        Reset(CSVFile);

        CSVClassFileOutLine := TStringList.Create;
        //read line by line while not end of file
        CSVFileOutLine.Delimiter := ',';
        CSVFileOutLine.QuoteChar := '"';

        while not EOF(CSVFile) do
        begin
          Readln(CSVFile, CSVFileLine);
          CSVFileOutLine.DelimitedText := CSVFileLine;

        end;//end while
      end; //file exists

      CSVFileOutLine.Free;
      CloseFile(CSVFile);
    end;
end;
0
Question by:rodneylim
    8 Comments
     
    LVL 2

    Expert Comment

    by:SaLz
    have a try of this.

    procedure TForm1.Button1Click(Sender: TObject);
    var F:TextFile;
        i : integer;
    begin
     AssignFile(F, 'c:\griddata.csv');
     Rewrite(F);
     for i := 0 to stringgrid1.RowCount-1 do
     begin
      writeln(F,stringgrid1.Rows[i].CommaText);
     end;
     CloseFile(F);
    end;


    this is from http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10300557.html

    Sal.
    0
     
    LVL 17

    Expert Comment

    by:Wim ten Brink
    You need to export your Oracle data and you're showing us how you import the CSV file. But in the while-loop you continuously replace the contents of CSVFileOutLine. By assigning a value to DelimitedText, you just repeatedly change the contents. I fear you're confusing things a little here...
    0
     
    LVL 2

    Expert Comment

    by:SaLz
    he said he already imported the data from the oracle to his program, he wants 2 export it, if am wrong then am sorry, bgz on me. I do apologize.

    Sal.
    0
     
    LVL 2

    Expert Comment

    by:SaLz
    ye, hes showing us his way of importing of his csv, and he wants 2 export data from his oracle database, I was wondering the same thing, it was little confusing, but his question title was sound, export 2 csv in delphi 7.

    u probly will have 2 connect 2 ur db, then list the data into the grid, then export it that way.

    Sal.
    0
     

    Author Comment

    by:rodneylim
    Hi guys!

    What I showed was my import of my csv file to my oracle data. The loop is to write line by line into the oracle database. It is just to show what I have done on the import side. What I do not know is how to export the existing oracle database back into a csv file. It is just the opposite way of what I have done which I think I still have to use the while loop right?

    Example of the CSV file when opened with a notepad.
    133456, "Finance 100", "School of Business"

    I hope I explained more clearly this time! Thanks guys!
    0
     
    LVL 27

    Accepted Solution

    by:
    var
      s : string;
      i : integer;
    begin  
      adoquery.sql.text := 'YourSelectYouHave';
      adoquery.open;
      AssignFile(CSVFile, SaveDialog.FileName);
      Rewrite(CSVFile);
      while not adoquery.eof do
      begin
        s := '';
        for i := 0 to adoquery.fields.count - 1 do
          if adoquery.fields[i].DataType = ftString then
             s := s+'"'+adoquery.fields[i].AsString+'",'
          else
             s := s+adoquery.fields[i].AsString+',';
        delete(s,length(s),1);  //delete last comma
        writeln(CSVFile,s);
        adoquery.next;
      end;
      adoquery.close;
      closefile(CSVFile);
    end;

    just from head

    meikl ;-)
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    hello?
    0
     

    Author Comment

    by:rodneylim
    sorry for the delay...it is been a hectic period for me! thanks for your help kretzschmar!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now