Показать сообщение отдельно
  #1  
Старый 06.09.2018, 09:12
Vansha Vansha вне форума
Прохожий
 
Регистрация: 18.01.2018
Сообщения: 30
Версия Delphi: 7, XE5
Репутация: 10
По умолчанию Подгрузить данные из книг Excel в ComboBox

Здравствуйте.
Подскажите, как можно подгрузить в ComboBox1 данные из книги +'\3021.xlsx, в ComboBox2 из книги +'\3022.xlsx
Как разделить процедуры?

код программы:

Код:
var
  Form1: TForm1;
  sg: TStringGrid;
implementation
 
{$R *.dfm}
 // drkb
uses
ComObj;
function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
// Create Excel-OLE Object
XLApp := CreateOleObject('Excel.Application');
try
   // Hide Excel
   XLApp.Visible := False;
   // Open the Workbook
   XLApp.Workbooks.Open(AXLSFile);
   // Sheet := XLApp.Workbooks[1].WorkSheets[1];
   Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
   // In order to know the dimension of the WorkSheet, i.e the number of rows
   // and the number of columns, we activate the last non-empty cell of it
   Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
   // Get the value of the last row
   x := XLApp.ActiveCell.Row;
   // Get the value of the last column
   y := XLApp.ActiveCell.Column;
   // Set Stringgrid's row &col dimensions.
   AGrid.RowCount := x;
   AGrid.ColCount := y;
   // Assign the Variant associated with the WorkSheet to the Delphi Variant
   RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
   //  Define the loop for filling in the TStringGrid
   k := 1;
   repeat
     for r := 1 to y do
       AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
     Inc(k, 1);
     AGrid.RowCount := k + 1;
   until k > x;
   // Unassign the Delphi Variant Matrix
   RangeMatrix := Unassigned;
finally
   // Quit Excel
   if not VarIsEmpty(XLApp) then
   begin
     // XLApp.DisplayAlerts := False;
     XLApp.Quit;
     XLAPP := Unassigned;
     Sheet := Unassigned;
     Result := True;
   end;
end;
end;
 
procedure TForm1.zgr(ds: integer);
begin
 Edit1.Text:= sg.Rows[ds][0];
 Edit2.Text:= sg.Rows[ds][2];
 Edit3.Text:= sg.Rows[ds][3];
 Edit4.Text:= sg.Rows[ds][4];
 Edit5.Text:= sg.Rows[ds][5];
 Edit6.Text:= sg.Rows[ds][0];
 Edit7.Text:= sg.Rows[ds][2];
 Edit8.Text:= sg.Rows[ds][3];
 Edit9.Text:= sg.Rows[ds][4];
 Edit10.Text:= sg.Rows[ds][5];
 end;
 
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 zgr(ComboBox1.ItemIndex+1);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
 sg:= TStringGrid.Create(self);
 sg.Visible:= false;
 Xls_To_StringGrid(sg, extractFileDir(ParamSTR(0)) +'\3021.xlsx');
 sg.RowCount:= sg.RowCount-2;
 Combobox1.Clear;
 Combobox1.Items.AddStrings(sg.Cols[1]);
 Combobox1.Items.Delete(0);
 Combobox1.ItemIndex:= 0;
zgr(1);
 
  end;
 
end.
Ответить с цитированием