|
| 1 | +unit RTL.Excel; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + RTL.DB, |
| 7 | + Spring.Collections, |
| 8 | + System.Classes, |
| 9 | + Data.DB, |
| 10 | + Data.Win.ADODB; |
| 11 | + |
| 12 | +type |
| 13 | + IExcelFile = interface |
| 14 | + ['{0CAB1799-A7A1-41E1-8BC9-DFA8D256CE39}'] |
| 15 | + function GetOnFileOpen: TNotifyEvent; |
| 16 | + function GetSheets: IEnumerable<string>; |
| 17 | + function GetFileName: string; |
| 18 | + |
| 19 | + procedure SetFileName(const Value: string); |
| 20 | + procedure SetOnFileOpen(const Value: TNotifyEvent); |
| 21 | + |
| 22 | + function SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet; |
| 23 | + |
| 24 | + property FileName: string read GetFileName write SetFileName; |
| 25 | + property Sheets: IEnumerable<string> read GetSheets; |
| 26 | + |
| 27 | + property OnFileOpen: TNotifyEvent read GetOnFileOpen write SetOnFileOpen; |
| 28 | + end; |
| 29 | + |
| 30 | + TExcelFile = class(TInterfacedObject, IExcelFile) |
| 31 | + strict private |
| 32 | + FConnection: TADOConnection; |
| 33 | + FFileName: string; |
| 34 | + FOnFileOpen: TNotifyEvent; |
| 35 | + |
| 36 | + function CreateQuery(AOwner: TComponent): TADOQuery; |
| 37 | + |
| 38 | + procedure OpenFile(const AFileName: string); |
| 39 | + procedure CloseFile; |
| 40 | + |
| 41 | + function GetOnFileOpen: TNotifyEvent; |
| 42 | + function GetSheets: IEnumerable<string>; |
| 43 | + function GetFileName: string; |
| 44 | + |
| 45 | + procedure SetFileName(const Value: string); |
| 46 | + procedure SetOnFileOpen(const Value: TNotifyEvent); |
| 47 | + strict protected |
| 48 | + function GetConnectionString: string; virtual; |
| 49 | + public |
| 50 | + constructor Create; overload; |
| 51 | + constructor Create(const AFileName: string); overload; |
| 52 | + destructor Destroy; override; |
| 53 | + |
| 54 | + function SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet; |
| 55 | + |
| 56 | + property FileName: string read GetFileName write SetFileName; |
| 57 | + property Sheets: IEnumerable<string> read GetSheets; |
| 58 | + |
| 59 | + property OnFileOpen: TNotifyEvent read GetOnFileOpen write SetOnFileOpen; |
| 60 | + end; |
| 61 | + |
| 62 | +implementation |
| 63 | + |
| 64 | +uses |
| 65 | + System.SysUtils; |
| 66 | + |
| 67 | +constructor TExcelFile.Create; |
| 68 | +begin |
| 69 | + inherited Create; |
| 70 | + FFileName := EmptyStr; |
| 71 | + |
| 72 | + FConnection := TADOConnection.Create(NIL); |
| 73 | + FConnection.Provider := 'Microsoft.Jet.OLEDB.4.0'; |
| 74 | + FConnection.LoginPrompt := False; |
| 75 | +end; |
| 76 | + |
| 77 | +constructor TExcelFile.Create(const AFileName: string); |
| 78 | +begin |
| 79 | + Create; |
| 80 | + Filename := AFileName; |
| 81 | +end; |
| 82 | + |
| 83 | +destructor TExcelFile.Destroy; |
| 84 | +begin |
| 85 | + FConnection.Free; |
| 86 | + inherited Destroy; |
| 87 | +end; |
| 88 | + |
| 89 | +function TExcelFile.CreateQuery(AOwner: TComponent): TADOQuery; |
| 90 | +begin |
| 91 | + Result := TADOQuery.Create(AOwner); |
| 92 | + Result.Connection := FConnection; |
| 93 | +end; |
| 94 | + |
| 95 | +procedure TExcelFile.CloseFile; |
| 96 | +begin |
| 97 | + if FFileName.IsEmpty then |
| 98 | + Exit; |
| 99 | + |
| 100 | + FFileName := EmptyStr; |
| 101 | + FConnection.Close; |
| 102 | +end; |
| 103 | + |
| 104 | +function TExcelFile.GetSheets: IEnumerable<string>; |
| 105 | +var |
| 106 | + LSheets: IList<string>; |
| 107 | + LTables: TStrings; |
| 108 | + s: string; |
| 109 | +begin |
| 110 | + LSheets := TCollections.CreateList<string>; |
| 111 | + |
| 112 | + if not FConnection.Connected then |
| 113 | + Exit(LSheets); |
| 114 | + |
| 115 | + LTables := TStringList.Create; |
| 116 | + try |
| 117 | + FConnection.GetTableNames(LTables); |
| 118 | + |
| 119 | + for s in LTables do |
| 120 | + LSheets.Add(s); |
| 121 | + finally |
| 122 | + LTables.Free; |
| 123 | + end; |
| 124 | + |
| 125 | + Result := LSheets; |
| 126 | +end; |
| 127 | + |
| 128 | +function TExcelFile.GetConnectionString: string; |
| 129 | +begin |
| 130 | + Result := 'Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s;Extended Properties="Excel 8.0;HDR=No";'; |
| 131 | +end; |
| 132 | + |
| 133 | +function TExcelFile.GetFileName: string; |
| 134 | +begin |
| 135 | + Result := FFileName; |
| 136 | +end; |
| 137 | + |
| 138 | +function TExcelFile.GetOnFileOpen: TNotifyEvent; |
| 139 | +begin |
| 140 | + Result := FOnFileOpen; |
| 141 | +end; |
| 142 | + |
| 143 | +procedure TExcelFile.OpenFile(const AFileName: string); |
| 144 | +begin |
| 145 | + FFileName := EmptyStr; |
| 146 | + FConnection.Close; |
| 147 | + FConnection.ConnectionString := Format(GetConnectionString, [AFileName]); |
| 148 | + FConnection.Open; |
| 149 | + FFileName := AFileName; |
| 150 | + |
| 151 | + if Assigned(FOnFileOpen) then |
| 152 | + FOnFileOpen(Self); |
| 153 | +end; |
| 154 | + |
| 155 | +procedure TExcelFile.SetFileName(const Value: string); |
| 156 | +begin |
| 157 | + OpenFile(Value); |
| 158 | +end; |
| 159 | + |
| 160 | +procedure TExcelFile.SetOnFileOpen(const Value: TNotifyEvent); |
| 161 | +begin |
| 162 | + FOnFileOpen := Value; |
| 163 | +end; |
| 164 | + |
| 165 | +function TExcelFile.SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet; |
| 166 | +var |
| 167 | + LQuery: TADOQuery; |
| 168 | +begin |
| 169 | + if not Assigned(ACloner) then |
| 170 | + raise Exception.CreateFmt('%s.SheetData :: ACloner is not assigned', [ClassName]); |
| 171 | + |
| 172 | + if SheetName.IsEmpty then |
| 173 | + raise Exception.CreateFmt('%s.SheetData :: SheetName is Empty', [ClassName]); |
| 174 | + |
| 175 | + LQuery := CreateQuery(NIL); |
| 176 | + try |
| 177 | + LQuery.SQL.Text := Format(' SELECT * FROM [%s] ', [SheetName]); |
| 178 | + LQuery.Open; |
| 179 | + Result := ACloner.Copy(LQuery); |
| 180 | + finally |
| 181 | + LQuery.Free; |
| 182 | + end; |
| 183 | +end; |
| 184 | + |
| 185 | +end. |
0 commit comments