|
|
Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
|
Опции темы | Поиск в этой теме | Опции просмотра |
#1
|
|||
|
|||
Подгрузить данные из книг 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. Последний раз редактировалось Vansha, 06.09.2018 в 11:14. |
#2
|
||||
|
||||
Больно расплывчато, в данном куске загрузка комбо данными из 3021.xlsx идет посредством гридовой прокладки, из её ячеек - тогда нужен ещё один симбиоз StringGrid2 и ComboBox2 вызовом Xls_To_StringGrid() для другой файлы
Я не понял Вашего вопроса, но всё же Вам на него отвечу! |
Этот пользователь сказал Спасибо Alegun за это полезное сообщение: | ||
Vansha (13.09.2018)
|
#3
|
|||
|
|||
Спасибо.
Прописал, все заработало. Код:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Grids, Controls, Forms, Dialogs, StdCtrls, Buttons, ExcelXP, OleServer; type TForm1 = class(TForm) ComboBox1: TComboBox; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Edit7: TEdit; Edit8: TEdit; Edit9: TEdit; Edit10: TEdit; Edit11: TEdit; Edit12: TEdit; Edit13: TEdit; ComboBox2: TComboBox; BitBtn1: TBitBtn; BitBtn2: TBitBtn; BitBtn3: TBitBtn; BitBtn4: TBitBtn; ExcelQueryTable1: TExcelQueryTable; ExcelApplication1: TExcelApplication; ExcelWorkbook1: TExcelWorkbook; ExcelOLEObject1: TExcelOLEObject; procedure zgr(ds: integer); procedure zgr2(ds: integer); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure BitBtn3Click(Sender: TObject); procedure BitBtn4Click(Sender: TObject); procedure ComboBox1Change(Sender: TObject); procedure ComboBox2Change(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; sg: TStringGrid; sg2: TStringGrid; implementation uses ComObj; {$R *.dfm} // drkb 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('C:\test\baza.xlsx'); // Sheet := XLApp.Workbooks[1].WorkSheets[1]; Sheet := XLApp.Workbooks[ExtractFileName('C:\test\baza.xlsx')].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; function Xls_To_StringGrid2(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('C:\test\3021.xlsx'); // Sheet := XLApp.Workbooks[1].WorkSheets[1]; Sheet := XLApp.Workbooks[ExtractFileName('C:\test\3021.xlsx')].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]; Edit11.Text:= sg.Rows[ds][2]; Edit2.Text:= sg.Rows[ds][3]; Edit4.Text:= sg.Rows[ds][5]; Edit5.Text:= sg.Rows[ds][6]; end; procedure TForm1.zgr2(ds: integer); begin Edit12.Text:= sg2.Rows[ds][0]; Edit13.Text:= sg2.Rows[ds][2]; end; procedure TForm1.BitBtn3Click(Sender: TObject); var XL: Variant; begin XL := CreateOLEObject('Excel.Application'); XL.WorkBooks.Open('C:\test\O210.xls'); XL.visible := true; end; procedure TForm1.BitBtn4Click(Sender: TObject); begin Form1.Close; 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)) +'\baza.xlsx'); sg.RowCount:= sg.RowCount-2; Combobox1.Clear; Combobox1.Items.AddStrings(sg.Cols[1]); Combobox1.Items.Delete(0); Combobox1.ItemIndex:= 0; sg2:= TStringGrid.Create(self); sg2.Visible:= false; Xls_To_StringGrid2(sg2, extractFileDir(ParamSTR(0)) +'\3021.xlsx'); sg2.RowCount:= sg2.RowCount-2; Combobox2.Clear; Combobox2.Items.AddStrings(sg2.Cols[1]); Combobox2.Items.Delete(0); Combobox2.ItemIndex:= 0; end; procedure TForm1.FormDestroy(Sender: TObject); begin sg.Free; end; procedure TForm1.ComboBox2Change(Sender: TObject); begin zgr2(ComboBox2.ItemIndex+1); end; end. |