Import documents

Use this demo code to load a document into a TPageControl container with grids (TStringGrid).

procedure TForm1.DoImport(const aPageControl: TPageControl);

  procedure _ShowInGrid(const bWorkSheet: TExportWorkSheet; const bGrid: TStringGrid);
  var
    I, J: Integer;
    xRow: TExportRow;
  begin
    bGrid.Options := bGrid.Options + [goColSizing];
    bGrid.RowCount := Max(1, bWorkSheet.Rows.Count);
    bGrid.ColCount := 1;

    for I := 0 to bWorkSheet.Rows.Count-1 do begin
      xRow := bWorkSheet.Rows[I];
      bGrid.ColCount := Max(bGrid.ColCount, xRow.Cells.Count);
      for J := 0 to xRow.Cells.Count-1 do begin
        //UNCOMMENT TO TEST EMPTY CELLS
        {if xRow.Cells[J] is TExportCellEmpty then
          bGrid.Cells[J, I] := '[empty]'//EMPTY (null) cell
        else}
          bGrid.Cells[J, I] := xRow.Cells[J].SqlText;
      end;
    end;
  end;

var
  xExport: TOExport;
  I: Integer;
  xPage: TTabSheet;
  xGrid: TStringGrid;
begin
  xExport := TOExport.Create;
  try
    if xExport.LoadFromFileWithDialog then begin
      //DELETE OLD PAGES
      for I := aPageControl.PageCount-1 downto 0 do
        aPageControl.Pages[I].Free;

      for I := 0 to xExport.WorkSheets.Count-1 do begin
        xPage := TTabSheet.Create(aPageControl);
        xPage.PageControl := aPageControl;
        xPage.Caption := xExport.WorkSheets[I].Title;
        xGrid := TStringGrid.Create(aPageControl);
        xGrid.Parent := xPage;
        xGrid.Align := alClient;

        _ShowInGrid(xExport.WorkSheets[I], xGrid);
      end;
    end;
  finally
    xExport.Free;
  end;
end;

procedure TForm1.BtnImportXLSXClick(Sender: TObject);
begin
  DoImport(PCImport);
end;
Categories