用Delphi 將 Excel 列名轉 0 起 數值

function GetColNum(aColName: string): Integer;
//  Excel 單元格 行列值 如: AI -> 34
var
  i, tmpLen: Integer;
begin
  tmpLen := Length(aColName);
  Result := 0;
  for I := 1 to tmpLen do
  begin
    if tmpLen - I > 0 then
      Result := Result + Trunc((Ord(aColName[I]) - 64) * Power(26, tmpLen - I))
    else
      Result := Result + Trunc(Ord(aColName[I]) - 64);
  end;
  Dec(Result); // xlsreadwrite  列是從 0 開始 
end;

function GetColAndRow(ColAndRowStr: string): TPoint;
var
  i, x, y: Integer;
  str: string;
begin
  Result := Point(0, 0);
  for i := 1 to Length(ColAndRowStr) do
  begin
    if ColAndRowStr[i] in ['0'..'9'] then
    begin
      str := UpperCase(Copy(ColAndRowStr, 1, i-1));
      x := GetColNum(str);
      y := StrToInt(Copy(ColAndRowStr,i, 255));
      Result := POint(x, y);
      Exit;
    end;
  end;
end;
相關文章
相關標籤/搜索