返回列表 發帖

[Delphi] 文字檔案開啟及存檔

視窗中建立兩個按鍵, 按第一個 "扛開按鍵 " 便啟動檔案對話匣, 讓你點選指定 extension 的檔案 (例如 csv), 然後把檔案內容讀入到strCSV.
按第二個 "存檔按鍵 " 便啟動檔案對話匣, 讓你點選指定 extension 的檔案存檔 (例如 txt), 程式中會把開啟檔案的檔案名自動放入存檔檔案名

public
      strCSV : String;
      CSVFile, TEXTFile : TextFile;
      Openfilename :String;


procedure TfrmConverter.butImportClick(Sender: TObject);
var
  openDialog : TOpenDialog;    // Open dialog variable
  strTmp : string;
begin
    openDialog := TOpenDialog.Create(self);
    openDialog.InitialDir := GetCurrentDir;
    openDialog.Options := [ofFileMustExist];   // Only allow existing files to be selected
    openDialog.Filter :=  'CSV files|*.csv';

    // Display the open file dialog
    if openDialog.Execute
    then
      begin
        Openfilename :=  openDialog.FileName;
        openDialog.Free;   // Free up the dialog
      End
    else
      begin
         ShowMessage('Open file was cancelled');
         exit;
      end;

    Try
      AssignFile(CSVFile, Openfilename);
      strCSV :='';  //every new import needs to clear strCSV
      Reset(CSVFile);
      while not Eof(CSVFile) do
          begin
             ReadLn(CSVFile, strTmp);
             strCSV := strCSV + strTmp;
          end;
      CloseFile(CSVFile);
    Except
      ShowMessage('File Operation Fails');
    End;



procedure TfrmConverter.butSaveClick(Sender: TObject);
var
  saveDialog : TSaveDialog;    // Save dialog variable
  strTmp2 :String;
  OpenfileLength, Openfilepointer: integer;
begin
  // Create the save dialog object - assign to our save dialog variable
  saveDialog := TSaveDialog.Create(self);
  saveDialog.Title := 'Save your Text File';
  saveDialog.InitialDir := GetCurrentDir;
  saveDialog.Filter := 'Text file|*.txt';
  saveDialog.DefaultExt := 'txt';
  // remove the path of the filename
  Repeat
     OpenfileLength := length(Openfilename);
     Openfilepointer :=Pos('\',Openfilename);
     Openfilename := Copy(Openfilename,Openfilepointer+1,OpenfileLength-Openfilepointer);
  Until (Openfilepointer=0);
  // remove the extension of the filename
  strTmp2 := lowercase(Openfilename);
  Openfilepointer :=Pos('.csv',strTmp2);
  Openfilename := Copy(Openfilename,1,Openfilepointer-1);

  saveDialog.FileName := Openfilename;

  // Display the open file dialog
  if saveDialog.Execute
  then
      begin
        strTmp2 :=  saveDialog.FileName;
        saveDialog.Free;     // Free up the dialog
        Try         
          AssignFile(TEXTFile,strTmp2);
          Rewrite(TEXTFile );
          Writeln(TEXTFile ,strCSV );
          closeFile(TEXTFile );
        Except
          ShowMessage('File Operation Fails');
        End;
      End
  else ShowMessage('Save file was cancelled');
End;
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

以上方法不能存Unicode, 要把 Unicode 存入 CSV 或  TXT

把下面的原來四句

          AssignFile(TEXTFile,strTmp2);
          Rewrite(TEXTFile );
          Writeln(TEXTFile ,strCSV);
          closeFile(TEXTFile );

改為

          { Create a new stream writer directly. }
           Writer := TStreamWriter.Create(strTmp2,false, TEncoding.UTF8);
          { Store the title and then the text. }
           //Writer.WriteLine(''); //add 0D 0A after write the data
           Writer.Write(
strCSV);
          { Close and Free the writer. }
           Writer.Free();

之前要加入以下一句在 var 內
  Writer: TStreamWriter;


Header 要加入SysUtils
及 Classes
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

TOP

Appending a line of text to an existing file

var
  myFile : TextFile;
  text   : string;

begin

// Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');
  ReWrite(myFile);


// Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');


// Close the file
  CloseFile(myFile);


// Reopen to append a final line to the file

Append(myFile);


// Write this final line
  WriteLn(myFile, 'Final line added');


// Close the file
  CloseFile(myFile);


// Reopen the file for reading
  Reset(myFile);


// Display the file contents
  while not Eof(myFile) do
  begin
    ReadLn(myFile, text);
    ShowMessage(text);
  end;


// Close the file for the last time
  CloseFile(myFile);
end;





Result
   Hello
   World
   Final line added
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

TOP

返回列表