تصدير بيانات dbgrid الى excel
#2
(19-04-2020, 03:01 PM)اهلا أخي جرب هذا الكود وسينجح معك باذن الله كتب :
procedure TfrmMain.BitBtn1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
  ExportTableToExcel(FDQuery1,SaveDialog1.FileName);
end;
end;
procedure TfrmMain.ExportTableToExcel(Tablex:TFDQuery;sFileConfusedtring);
var
//Declarations
ExcelApplication : variant;
Sheet : variant;
column, row ,rowsno: integer;
FormatFloatY:WideString;
ValXX:WideString;
begin
Cursor:=crHourGlass;
try  //Try to create Excel application
  begin
  ExcelApplication := CreateOleObject('Excel.Application');
  ExcelApplication.Visible := true;  //let's make visible
  end;
except
//If failed then show warning
  Showmessage('Cannot create an Excel file,'
  +'make sure that MS Excel is installed on your system');
  Application.Terminate;
end;
rowsno:=Tablex.RecordCount;  //records number in a table
Tablex.RecNo:=1; //set table to first record
ExcelApplication.WorkBooks.Add(-4167);  //Add excel workbook
ExcelApplication.WorkBooks[1].Worksheets[1].Name := 'قائمة الاساتذة';
Sheet := ExcelApplication.WorkBooks[1].WorkSheets['قائمة الاساتذة'];
//Format cells in excel sheet
Sheet.Range['A1:F'+IntToStr(rowsno+1)].Borders.LineStyle := 7;
Sheet.Range['A1:F'+IntToStr(rowsno+1)].Borders.color := clBlack;
Sheet.Range['A1:A'+IntToStr(rowsno+1)].HorizontalAlignment := xlCenter;
      //colors of cells in first line
Sheet.Cells[1,1].Interior.Color := clMoneyGreen;
Sheet.Cells[1,2].Interior.Color := clMoneyGreen;
Sheet.Cells[1,3].Interior.Color := clMoneyGreen;
Sheet.Cells[1,4].Interior.Color := clMoneyGreen;
Sheet.Cells[1,5].Interior.Color := clMoneyGreen;
Sheet.Cells[1,6].Interior.Color := clMoneyGreen;
                          //widths of columns
Sheet.Columns[1].ColumnWidth := 10;
Sheet.Columns[2].ColumnWidth := 30;
Sheet.Columns[3].ColumnWidth := 12;
Sheet.Columns[4].ColumnWidth := 25;
Sheet.Columns[5].ColumnWidth := 12;
Sheet.Columns[6].ColumnWidth := 12;
            //captions/text of cells
Sheet.Cells[1,1] := 'الرفم';
Sheet.Cells[1,2] := 'الاسم واللقب';
Sheet.Cells[1,3] := 'تاريخ الميلاد';
Sheet.Cells[1,4] := 'أسم المدرسة';
Sheet.Cells[1,5] := 'بداية التربص';
Sheet.Cells[1,6] := 'الحالة';
  //set progressbar max value = teble records count
ProgressBar1.max:=Tablex.RecordCount;
ProgressBar1.position:=0;  //set position to 0=start
//now copy from table to excel cells
for row := 1 to Tablex.RecordCount do
begin
Sheet.Cells[row+1,1] := Tablex.Fields[0].AsString; //row+1 = move to the next line
//in column one
Sheet.Cells[row+1,2] :=Tablex.Fields[1].AsString;
//row+1 = move to the next line
//in column two
Sheet.Cells[row+1,3] := Tablex.Fields[2].AsString;
//row+1 = move to the next line
//in column three
Sheet.Cells[row+1,4] := Tablex.Fields[3].AsString;
//row+1 = move to the next line
//in column three
Sheet.Cells[row+1,5] := Tablex.Fields[4].AsString;
//row+1 = move to the next line
//in column three
Sheet.Cells[row+1,6] := Tablex.Fields[5].AsString;
//row+1 = move to the next line
//in column three
ProgressBar1.position:=row;//set progress bar position
Tablex.Next;  //go to the next record in a table
end;
Screen.Cursor:=crDefault;  //set default cursor
ProgressBar1.position:=0; //it is done , set PB to the begining
DeleteFile(sFile);  //
Sheet.SaveAs(sFile);  //Save excel file
Screen.Cursor:=crDefault;  //set default cursor
{ ExcelApplication.Quit; //Quit excel
  ExcelApplication := Unassigned;  //to be sure that no hidden excel in memory
    Sheet := Unassigned; }
  //let's close manually
//let's test
//a bit slow while screen capturing
end;
الرد


الردود في هذا الموضوع
تصدير بيانات dbgrid الى excel - بواسطة appakar - 19-04-2020, 03:01 PM
RE: تصدير بيانات dbgrid الى excel - بواسطة nawfel201 - 19-04-2020, 04:45 PM
RE: تصدير بيانات dbgrid الى excel - بواسطة bouh25 - 19-04-2020, 07:43 PM
RE: تصدير بيانات dbgrid الى excel - بواسطة nawfel201 - 19-04-2020, 09:38 PM
RE: تصدير بيانات dbgrid الى excel - بواسطة appakar - 19-04-2020, 10:39 PM
RE: تصدير بيانات dbgrid الى excel - بواسطة ghostdz - 22-04-2020, 10:32 AM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم