procedure Tfrmpag.Button9Click(Sender: TObject); var ss : TStrings; i, j : integer; s, sFields, sTitle : string; begin if sdSave.Execute then begin sFields := ';'; sTitle := ''; For i := 0 to Self.ComponentCount - 1 do if (Self.Components[i] is TDBGrid) then begin if (Self.Components[i] as TDBGrid).DataSource = dsMX then begin For j := 0 to (Self.Components[i] as TDBGrid).Columns.Count - 1 do begin sTitle := sTitle + (Self.Components[i] as TDBGrid).Columns[j].Title.Caption + #9; sFields := sFields + (Self.Components[i] as TDBGrid).Columns[j].FieldName + ';'; end; end; end; j := qryMX.FieldCount - 1; qryMX.First; ss := TStringList.Create; ss.Add(sTitle); while not qryMX.Eof do begin s := ''; if sFields <> ';' then begin for i := 0 to j do if (Pos(';'+qryMX.Fields[i].FieldName+';', sFields) > 0) or (qryMX.Fields[i].DataType = ftMemo) then s := s + qryMX.Fields[i].asstring + #9; end else begin for i := 0 to j do s := s + qryMX.Fields[i].asstring + #9; end; ss.Add(s); qryMX.Next; end; ss.SaveToFile(sdSave.FileName); ss.free; end; end;
请问dsMX,qryMX指的是什么?另外能说一下要用到些什么控件吗?谢谢!
将dataset导出EXCEL的function procedure TFrmMain.WriteExcel(AdsData TADODataSet; sName, Title string); var ExcelApplication1 TExcelApplication; ExcelWorksheet1 TExcelWorksheet; ExcelWorkbook1 TExcelWorkbook; i, j integer; filename string; begin filename = concat(extractfilepath(application.exename), sName, ’.xls’); try ExcelApplication1 = TExcelApplication.Create(Application); ExcelWorksheet1 = TExcelWorksheet.Create(Application); ExcelWorkbook1 = TExcelWorkbook.Create(Application); ExcelApplication1.Connect; except Application.Messagebox(’Excel 没有安装!’, ’Hello’, MB_ICONERROR + mb_Ok); Abort; end; try ExcelApplication1.Workbooks.Add(EmptyParam, 0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet); AdsData.First; for j = 0 to AdsData.Fields.Count - 1 do begin ExcelWorksheet1.Cells.item[3, j + 1] = AdsData.Fields[j].DisplayLabel; ExcelWorksheet1.Cells.item[3, j + 1].font.size = ’10’; end; for i = 4 to AdsData.RecordCount + 3 do begin for j = 0 to AdsData.Fields.Count - 1 do begin ExcelWorksheet1.Cells.item[i, j + 1] = AdsData.Fields[j].Asstring; ExcelWorksheet1.Cells.item[i, j + 1].font.size = ’10’; end; AdsData.Next; end; ExcelWorksheet1.Columns.AutoFit; ExcelWorksheet1.Cells.item[1, 2] = Title; ExcelWorksheet1.Cells.Item[1, 2].font.size = ’14’; ExcelWorksheet1.SaveAs(filename); Application.Messagebox(pchar(’数据成功导出’ + filename), ’Hello’, mb_Ok); finally ExcelApplication1.Disconnect; ExcelApplication1.Quit; ExcelApplication1.Free; ExcelWorksheet1.Free; ExcelWorkbook1.Free; end; end;
var
ss : TStrings;
i, j : integer;
s, sFields, sTitle : string;
begin
if sdSave.Execute then
begin
sFields := ';';
sTitle := '';
For i := 0 to Self.ComponentCount - 1 do
if (Self.Components[i] is TDBGrid) then
begin
if (Self.Components[i] as TDBGrid).DataSource = dsMX then
begin
For j := 0 to (Self.Components[i] as TDBGrid).Columns.Count - 1 do
begin
sTitle := sTitle + (Self.Components[i] as TDBGrid).Columns[j].Title.Caption + #9;
sFields := sFields + (Self.Components[i] as TDBGrid).Columns[j].FieldName + ';';
end;
end;
end;
j := qryMX.FieldCount - 1;
qryMX.First;
ss := TStringList.Create;
ss.Add(sTitle);
while not qryMX.Eof do
begin
s := '';
if sFields <> ';' then
begin
for i := 0 to j do
if (Pos(';'+qryMX.Fields[i].FieldName+';', sFields) > 0)
or (qryMX.Fields[i].DataType = ftMemo) then
s := s + qryMX.Fields[i].asstring + #9;
end
else
begin
for i := 0 to j do
s := s + qryMX.Fields[i].asstring + #9;
end;
ss.Add(s);
qryMX.Next;
end;
ss.SaveToFile(sdSave.FileName);
ss.free;
end;
end;
procedure TFrmMain.WriteExcel(AdsData TADODataSet; sName, Title string);
var
ExcelApplication1 TExcelApplication;
ExcelWorksheet1 TExcelWorksheet;
ExcelWorkbook1 TExcelWorkbook;
i, j integer;
filename string;
begin
filename = concat(extractfilepath(application.exename), sName, ’.xls’);
try
ExcelApplication1 = TExcelApplication.Create(Application);
ExcelWorksheet1 = TExcelWorksheet.Create(Application);
ExcelWorkbook1 = TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox(’Excel 没有安装!’, ’Hello’, MB_ICONERROR + mb_Ok);
Abort;
end;
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
AdsData.First;
for j = 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[3, j + 1] = AdsData.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[3, j + 1].font.size = ’10’;
end;
for i = 4 to AdsData.RecordCount + 3 do
begin
for j = 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] =
AdsData.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size = ’10’;
end;
AdsData.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.Cells.item[1, 2] = Title;
ExcelWorksheet1.Cells.Item[1, 2].font.size = ’14’;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar(’数据成功导出’ + filename), ’Hello’,
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;
再放一个显示数据的DBGRID
qryMX 是一个Query
sdSave 是一个SaveDialog