![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
|
|
#1
|
|||
|
|||
|
Подскажите, пожалуйста, как правильно импортировать данные из таблицы Excel, экспорт работает отлично, необходимо обратный процесс создать...
Вот код экспорта: Код:
procedure TBasePPL.ToExcelExecute(Sender: TObject);
var ws,xl:variant;
i,maxrow:integer;
col,fst:integer;
begin
fst:=2;
xl:=createoleobject('Excel.Application');
try
maxrow:=0;
xl.visible:=true;
XL.WorkBooks.Add;
ws := XL.ActiveWorkBook.ActiveSheet;
ws.Range['A1'].NumberFormat:='0';
ws.Range['A'+inttostr(fst)].Value := '№';
ws.Range['B'+inttostr(fst)].Value := 'Nombres';
col:=1;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(fst)].Value := ' ';
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(fst)].Value := ' ';
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(fst)].Value := ' ';
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(fst)].Value := ' ';
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(fst)].Value := ' ';
end;
for i:=fst+1 to Visitas.Items.Count+fst do begin
col:=1;
ws.Range['a'+inttostr(i)].Value := i-2;//# работы
ws.Range['b'+inttostr(i)].Value := Visitas.Items[i-fst-1].Caption;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(i)].Value:=Visitas.Items[i-fst-1].SubItems[0];
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(i)].Value:=Visitas.Items[i-fst-1].SubItems[1];
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(i)].Value:=Visitas.Items[i-fst-1].SubItems[2];
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(i)].Value:=Visitas.Items[i-fst-1].SubItems[3];
end;
begin
inc(col);
ws.Range[Chr(Ord('A')+col)+inttostr(i)].Value:=Visitas.Items[i-fst-1].SubItems[4];
end;
maxrow:=i;
end;
//установим сетку для таблицы
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeLeft].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeLeft].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeLeft].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeTop].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeTop].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeTop].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeBottom].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeBottom].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeBottom].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeRight].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeRight].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlEdgeRight].ColorIndex:=xlAutomatic;
//внутреннее пересечение
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideVertical].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideVertical].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideVertical].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideHorizontal].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideHorizontal].Weight:=xlThin;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(maxrow)].Borders[xlInsideHorizontal].ColorIndex:=xlAutomatic;
//наружние границы
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeLeft].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeLeft].Weight:=xlMedium;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeLeft].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeTop].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeTop].Weight:=xlMedium;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeTop].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeBottom].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeBottom].Weight:=xlMedium;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeBottom].ColorIndex:=xlAutomatic;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeRight].LineStyle:=xlContinuous;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeRight].Weight:=xlMedium;
ws.Range['A'+inttostr(fst)+':'+Chr(Ord('A')+col)+inttostr(fst)].Borders[xlEdgeRight].ColorIndex:=xlAutomatic;
for i:=ord('A')to ORD('A')+col do begin
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeLeft].LineStyle:=xlContinuous;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeLeft].Weight:=xlMedium;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeLeft].ColorIndex:=xlAutomatic;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeTop].LineStyle:=xlContinuous;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeTop].Weight:=xlMedium;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeTop].ColorIndex:=xlAutomatic;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeBottom].LineStyle:=xlContinuous;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeBottom].Weight:=xlMedium;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeBottom].ColorIndex:=xlAutomatic;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeRight].LineStyle:=xlContinuous;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeRight].Weight:=xlMedium;
ws.Range[Chr(i)+inttostr(fst)+':'+Chr(i)+inttostr(maxrow)].Borders[xlEdgeRight].ColorIndex:=xlAutomatic;
end;
ws.range['A1:'+chr(Ord('A')+col)+'1'].merge;
if (Groups.ItemIndex>=0)and(Groups.ItemIndex<groups.Items.Count)then
ws.range['A1:'+chr(Ord('A')+col)+'1'].value:='Lista "'+Groups.Items[Groups.ItemIndex].Caption+'"'
else
ws.range['A1:'+chr(Ord('A')+col)+'1'].value:='Lista';
ws.range['A1:'+chr(Ord('A')+col)+'1'].font.Bold:=true;
ws.range['A'+inttostr(fst)+':'+chr(Ord('A')+col)+inttostr(fst)].font.bold:=true;
//подгоним размеры
ws.Columns['A:'+chr(Ord('A')+col)].EntireColumn.AutoFit;
xl.ActiveWindow.SplitRow:=fst;
xl.ActiveWindow.FreezePanes:=True;
xl.ActiveSheet.PageSetup.Orientation:=xlLandscape;
xl.ActiveSheet.PageSetup.PrintArea:='$A:$G';
finally
//выход из экселя
// xl.quit;
end;Последний раз редактировалось doctordradd, 01.11.2010 в 20:13. |
|
#2
|
||||
|
||||
|
Ну как-бы все тоже самое, только не запись в ячейки листа, а чтение из них:
Код:
Value1 := ws.Range['A'+inttostr(fst)].Value; Value2 := ws.Range['B'+inttostr(fst)].Value; |
|
#3
|
|||
|
|||
|
Ммм...
Т.е. во всем коде только изменить запись на чтение??? Больше ничего? настолько просто? |
|
#4
|
||||
|
||||
|
А вам обязательно метровая портянка с кодом нужна?
![]() |
|
#5
|
|||
|
|||
|
Открывается файл экселя, записывается временно 1 колонка, при закрытии программы записанные данные исчезают.
Записываются только в готовую строку, т.е. в программу введены вручную данные, в эти строки вписываются данные с экселя, в пустые же - нет... Подскажите, пожалуйста, где ошибка. Код:
var ws,xl:variant;
i,maxrow:integer;
col,fst:integer;
begin
fst:=7;
xl:=createoleobject('Excel.Application');
try
maxrow:=0;
xl.visible:=true;
XL.WorkBooks.open('PRESENTES');
ws := XL.ActiveWorkBook.ActiveSheet;
ws.Range['A1'].NumberFormat:='0';
for i:=fst+1 to Visitas.Items.Count+fst do begin
col:=1;
Visitas.Items[i-fst-1].SubItems[0]:= ws.Range['A'+inttostr(fst)].Value;
Visitas.Items[i-fst-1].SubItems[1] := ws.Range['B'+inttostr(fst)].Value;
maxrow:=i;Последний раз редактировалось doctordradd, 02.11.2010 в 03:58. |