Skip to content

Instantly share code, notes, and snippets.

@NoNameDev-Git
Created February 8, 2025 08:00
Show Gist options
  • Save NoNameDev-Git/7999104c22c714b1920b7d7bcff9ac04 to your computer and use it in GitHub Desktop.
Save NoNameDev-Git/7999104c22c714b1920b7d7bcff9ac04 to your computer and use it in GitHub Desktop.
Delphi Save Load ListView .csv Excel
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Buttons, sSpeedButton, Vcl.ComCtrls,
sListView;
type
TForm1 = class(TForm)
sListView1: TsListView;
sSpeedButton1: TsSpeedButton;
sSpeedButton2: TsSpeedButton;
procedure sSpeedButton1Click(Sender: TObject);
procedure sSpeedButton2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure SaveFileCSV(ListView: TsListView; FilePath: string);
var
CSVFile: TextFile;
i, j: Integer;
ListItem: TListItem;
CellValue: string;
begin
try
if FileExists(FilePath) then DeleteFile(FilePath);
AssignFile(CSVFile, FilePath);
try
Rewrite(CSVFile);
for j := 0 to ListView.Columns.Count - 1 do
begin
if j > 0 then
Write(CSVFile, ';');
Write(CSVFile, '"' + ListView.Columns[j].Caption + '"');
end;
Writeln(CSVFile);
for i := 0 to ListView.Items.Count - 1 do
begin
ListItem := ListView.Items[i];
for j := 0 to ListView.Columns.Count - 1 do
begin
if j > 0 then
Write(CSVFile, ';');
if j = 0 then
CellValue := ListItem.Caption
else
CellValue := ListItem.SubItems[j - 1];
if (Pos(';', CellValue) > 0) or (Pos('"', CellValue) > 0) then
CellValue := '"' + StringReplace(CellValue, '"', '""', [rfReplaceAll]) + '"';
Write(CSVFile, CellValue);
end;
Writeln(CSVFile);
end;
finally
CloseFile(CSVFile);
end;
except
on E: Exception do
Application.ProcessMessages;
end;
end;
procedure LoadFileCSV(ListView: TsListView; FilePath: string);
var
CSVFile: TextFile;
Line: string;
j, ColCount: Integer;
ListItem: TListItem;
Values: TStringList;
begin
if not FileExists(FilePath) then Exit;
Values := TStringList.Create;
try
AssignFile(CSVFile, FilePath);
Reset(CSVFile);
// Очищаем ListView перед загрузкой
// ListView.Items.Clear;
// ListView.Columns.Clear;
if not Eof(CSVFile) then
begin
ReadLn(CSVFile, Line);
Values.Delimiter := ';';
Values.StrictDelimiter := True;
Values.DelimitedText := Trim(Line);
while (Values.Count > 0) and (Values[Values.Count - 1] = '') do
Values.Delete(Values.Count - 1);
ColCount := Values.Count;
// Создаём заголовки столбцов
// for j := 0 to ColCount - 1 do
// ListView.Columns.Add.Caption := Values[j];
end;
while not Eof(CSVFile) do
begin
ReadLn(CSVFile, Line);
Values.DelimitedText := Trim(Line);
while (Values.Count > 0) and (Values[Values.Count - 1] = '') do
Values.Delete(Values.Count - 1);
if Values.Count > 0 then
begin
ListItem := ListView.Items.Add;
ListItem.Caption := Values[0];
for j := 1 to Values.Count - 1 do
ListItem.SubItems.Add(Values[j]);
end;
end;
finally
Values.Free;
CloseFile(CSVFile);
end;
end;
procedure TForm1.sSpeedButton1Click(Sender: TObject);
begin
SaveFileCSV(sListView1, ExtractFilePath(ParamStr(0)) + 'File.csv');
end;
procedure TForm1.sSpeedButton2Click(Sender: TObject);
begin
LoadFileCSV(sListView1, ExtractFilePath(ParamStr(0)) + 'File.csv');
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment