Although I am a great fan of stackoverflow and I found several good solutions, this is my first question. I am writing a Delphitool (D6... old but still working!) for making it easier to administrate our students at school.
Delphi reads some (even older...) Visual Foxpro Dbase tables and collects the needed data in a query. This data should be exported to Excel. "Theoretically" everything is working, but I have several problems:
Any ideas? Thanks in advance!
PS: I also looked for a simple "replace all spaces but not within words" way, thought of regexp, but found no way to get this done from within my Delphi code.
As there is not very much text in the excel sheet (100 lines, 4 columns), I could walk through every single cell... but that seems to be not very elegant. How does Trim work in Excel VBA? How to trim text in a column with VBA - Excel
So there's the code for simple replacement of all spaces - unfortunately it also replaces necesserary space between 2 words in a cell:
ovExcelApp := CreateOleObject('Excel.Application');
try
ovExcelApp.displayAlerts := false;
ovExcelWorkbook := ovExcelApp.WorkBooks.Add;
ovWS := ovExcelWorkbook.Worksheets.Item[1];
ovWS.Activate;
ovWS.Select;
ovWS.Cells.NumberFormat := AnsiChar('@');
ovRange := ovWS.Range['A1', 'A1']; //go to first cell
ovRange.Resize[Data.RecordCount, Data.Fields.Count];
ovRange.CopyFromRecordset(Data, Data.RecordCount, Data.Fields.Count);
ovExcelApp.Cells.Replace(
What := ' ',
Replacement := '',
LookAt := 2,
SearchOrder := 1,
MatchCase := False,
SearchFormat := False,
ReplaceFormat := False
);
end;
// How could I do a regexp (only replace leading spaces) or do a trim over all cells withing ovWS?
ovRange.EntireColumn.AutoFit;
ovWS.SaveAs(DestName, 1, '', '', False, False);
Trying to trim field values brings an error:
procedure Tf_dbftools.qADO_einzelnAfterOpen(DataSet: TDataSet);
begin
with qADO_einzeln do begin
first;
while not eof do begin
edit;
Fields[0].AsString := trim(fields[0].asstring);
Fields[1].AsString := trim(fields[1].asstring);
Fields[2].AsString := trim(fields[2].asstring);
Fields[3].AsString := trim(fields[3].asstring);
next;
end;
//post -> error
end;
end;
I just need to edit the data (from different tables) in the qADO Query resp. dataset to get things correctly exported to Excel. Do I really need to copy the data into a clientDataset to edit them?
Finally I chose this (simple...) solution by copying every cell value manually so I can use Delphi's trim function:
.. some code as above, but now "data" is a TADOQuery:
data.First;
for RowIndex := 0 to Data.RecordCount - 1 do begin
for ColumnIndex := 0 to data.Fields.Count - 1 do begin
ovWS.Cells[RowIndex+1, ColumnIndex+1].Value := trim(data.Fields[columnIndex].AsString);
next ;
end;
data.Next;
next;
(... but still wondering if there is a simpler and "cleaner" way... besides the fact that padding in Foxpro is horrible!)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.