Jump to content

Recommended Posts

Posted

I found this on StackOverflow:

procedure FillStringgrid;
// Split the line of text into individual entries
  procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
  begin
    Assert(Assigned(Strings));
    Strings.Clear;
    Strings.Delimiter     := Delimiter;
    Strings.DelimitedText := Input;
  end;


var
  strlst: Tstringlist;
  myfile: TextFile;
  search: string;
  i, j  : Integer;
begin
  i := 0;
  AssignFile(myfile, 'filepath'); // specify your file path here
  Reset(myfile);
  while not Eof(myfile) do
  begin
    Readln(myfile, search);
    strlst := Tstringlist.Create;
    Split(' ', search, strlst); // get the no's separated by the delimiter
    // adjust your column count based on no of entries
    if StringGrid1.ColCount < strlst.Count then
      StringGrid1.ColCount := strlst.Count;
    StringGrid1.rows[i]    := strlst; // adjust the row count
    inc(i);
    StringGrid1.RowCount := i;
  end;
  // free stringlist and textfile
  CloseFile(myfile);
  strlst.Free;


  // fill in the blank entries with 0
  for i := 0 to StringGrid1.RowCount - 1 do
  begin
    for j := 0 to StringGrid1.ColCount - 1 do
    begin
      if StringGrid1.Cells[j, i] = '' then
        StringGrid1.Cells[j, i] := '0';
    end;
  end;
end;
Posted

you can easily translate this into unigui

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 imported!');
end;

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...