Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.11.27;
Скачать: CL | DM;

Вниз

Люди добрые помогите....   Найти похожие ветки 

 
Vista   (2005-10-17 08:10) [0]

Люди помогите кто может....
Такая задача....
Есть файл excel с данными людей, 10 полей....
Как написать код что бы программа конвертировалла эти данные из excel в текстовый файл в определенный формат?
Я новичек в програмировании и пока такая задача для меня непреодалима...


 
dreamse   (2005-10-17 08:13) [1]

импортировать данные из Excel в StringGrid


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.Button1Click(Sender: TObject);
begin
 if Xls_To_StringGrid(StringGrid1, "C:\Table1.xls") then
   ShowMessage("Table has been exported!");
end;



 
dreamse   (2005-10-17 08:15) [2]

Получение данных из Excel


procedure TForm1.BitBtnFromExcelOnClick(Sender: TObject);
var
 WorkBk: _WorkBook;
 WorkSheet: _WorkSheet;
 K, R, X, Y: Integer;
 IIndex: OleVariant;
 RangeMatrix: Variant;
 NomFich: WideString;
begin
 NomFich := "C:\MyDirectory\NameOfFile.xls";
 IIndex := 1;
 XLApp.Connect;
 // Открываем файл Excel
 XLApp.WorkBooks.Open(NomFich, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
 EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,EmptyParam,0);
 WorkBk := XLApp.WorkBooks.Item[IIndex];
 WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
 // Чтобы знать размер листа (WorkSheet), т.е. количество строк и количество
 // столбцов, мы активируем его последнюю непустую ячейку
 WorkSheet.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
 // Получаем значение последней строки
 X := XLApp.ActiveCell.Row;
 // Получаем значение последней колонки
 Y := XLApp.ActiveCell.Column;
 // Определяем количество колонок в TStringGrid
 GenericStringGrid.ColCount := Y;
 // Сопоставляем матрицу WorkSheet с нашей Delphi матрицей
 RangeMatrix := XLApp.Range["A1",XLApp.Cells.Item[X,Y]].Value;
 // Выходим из Excel и отсоединяемся от сервера
 XLApp.Quit;
 XLApp.Disconnect;
 //  Определяем цикл для заполнения TStringGrid
 K := 1;
 repeat
   for R := 1 to Y do
     GenericStringGrid.Cells[(R - 1),(K - 1)] := RangeMatrix[K,R];
   Inc(K,1);
   GenericStringGrid.RowCount := K + 1;
 until
   K > X;
 // Unassign the Delphi Variant Matrix
 RangeMatrix := Unassigned;
end;




Страницы: 1 вся ветка

Текущий архив: 2005.11.27;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.063 c
2-1131276773
Tek Noise
2005-11-06 14:32
2005.11.27
Поиск файлов.


14-1131316468
Mirror
2005-11-07 01:34
2005.11.27
Подскажите компонент


9-1121162697
D-Man
2005-07-12 14:04
2005.11.27
Радиус света (OGL)


2-1131538684
Al_Ba
2005-11-09 15:18
2005.11.27
Помогите с TreeView


14-1131446476
Аноним
2005-11-08 13:41
2005.11.27
Аномальное явление?