请问如何将EXCEL中的单元格地址(如D15)转换成[ROW,COL]的模式.(100分)

  • 主题发起人 主题发起人 Co1230
  • 开始时间 开始时间
C

Co1230

Unregistered / Unconfirmed
GUEST, unregistred user!
例如:将D15转换为[15,4];AA2转换为[2,27],求助。
 
我有一个,不过是将数字15,4转换为D15,你可以参考一下,再改过来.
我的使用例子.
TurnExcelCell(1,1)='A1';

function TurnExcelCell(a, k: integer): string;
var x,y,z:string;
b,c,i,j:integer;
begin
b:=a div 26;
c:=a mod 26;
if (b>=27) and (c>0) then begin
Result:='0'; //若返回值為0則無效
abort;
end
else if (b>0) and (b=27) then
i:=64+b-1
else if (b>0) and (b<27) then
i:=64+b
else if b=0 then
i:=64+26;
if c>0 then
j:=64+c
else if c=0 then
j:=64+26;
x:=Chr(i);
y:=Chr(j);
if (b>0) and (c>0) then
z:=x+y
else if (c=0) and (b=27) then
z:=x+y
else if ((c=0) and (b>0)) or ((c>0) and (b=0)) then
z:=y
else
z:=x;
Result:=z+IntToStr(k);
end;
 
还是让Excel帮你算得了。
sheet1.range("AA2").Row
2
sheet1.Range("AA2").Column
27

 
agree bluerain
 
大富豪怎么了
 
主要是计算列的序列号:Col2Num是将a-iv列转换成序列号的函数.我一直在项目中使用它
function Col2Num(ColName:string):integer;
var
Len:Integer;
D1,D2:Integer;
begin
Result:=-1;
Len:=Length(ColName);
if Len>2 then raise Exception.Create('列名长过3个字符('+ColName+')');
if Len<=0 then raise Exception.Create('列名为空');
if Len=2 then begin
if Ord(UpperCase(ColName[1])[1])>Ord('I') then begin
raise Exception.Create('非法列名'+ColName);
end else begin
if Ord(UpperCase(ColName[1])[1])=Ord('I') then begin
if Ord(UpperCase(ColName[2])[1])>Ord('V') then raise Exception.Create('列名过长('+ColName+')');
end;
D1:=26*(Ord(UpperCase(ColName[1])[1])-Ord('A')+1);
D2:=Ord(UpperCase(ColName[2])[1])-Ord('A')+1;
Result:=D1+D2;
end;
end;
if Len=1 then begin
Result:= Ord(UpperCase(ColName[1])[1])-Ord('A')+1;
end;
end;
 
看一下Excel的帮助,多了解一下Rang和Item的区别.

Application 对象:返回 Range 对象,该对象代表活动工作表中所有的单元格。如果当前活动文档不是工作表,本属性将失败。只读。
Range 对象:返回 Range 对象,该对象代表指定区域内的所有单元格(换句话说,本属性未做任何事情)。只读。
Worksheet 对象:返回 Range 对象,该对象代表工作表中所有的单元格(不仅是当前正在使用的单元格)。只读。
说明
因为 Item 属性是 Range 对象的默认属性,所以可在 Cells 关键字后直接指定行号和列标。详细内容请参阅 Item 属性及本帮助主题的示例。
使用本属性时,如果不指定对象识别符,本属性将返回代表活动工作表中所有单元格的 Range 对象。
 
XLSBOF: array[0..4] of Word = ($409, 6, 0, $10, 0);
XLSGUTS: array[0..5] of Word = ($80, 8, 0, 0, 0, 0);
XLSPalette: array[0..2] of Word = ($92, 0, 0);
XLSDimension: array [0..6] of Word = ($200, $0A, 0, $FFFF, 0, $FF, 0);
XLSEOF: array[0..1] of Word = ($0A, 0);
XLSFONTH: array[0..9] of Byte = ($31, 2, 0, 0, 0, 0, 0, 0, 8, 0);
XLSFONTG: array[0..9] of Byte = ($31, 2, 0, 0, 0, 0, 0, 0, 10, 0);
XLSFONT: array[0..9] of Byte = ($31, 2, 0, 0, 0, 0, 0, 0, 0, 0);
XLSXF1: array[0..15] of Byte = ($43, 4, $0C, 0, 0, 0, $F5, $FF, $20, 0, 0, $CE, 0, 0, 0, 0);
XLSXF2: array[0..15] of Byte = ($43, 4, $0C, 0, 1, 0, $F5, $FF, $20, $F4, 0, $CE, 0, 0, 0, 0);
XLSXF3: array[0..15] of Byte = ($43, 4, $0C, 0, 2, 0, $F5, $FF, $20, $F4, 0, $CE, 0, 0, 0, 0);
XLSXF4: array[0..15] of Byte = ($43, 4, $0C, 0, 0, 0, $F5, $FF, $20, $F4, 0, $CE, 0, 0, 0, 0);
XLSXF5: array[0..15] of Byte = ($43, 4, $0C, 0, 0, 0, 1, 0, $20, 0, 0, $CE, 0, 0, 0, 0);
XLSXF6: array[0..15] of Byte = ($43, 4, $0C, 0, 1, $21, $F5, $FF, $20, $F8, 0, $CE, 0, 0, 0, 0);
XLSXF7: array[0..15] of Byte = ($43, 4, $0C, 0, 1, $1F, $F5, $FF, $20, $F8, 0, $CE, 0, 0, 0, 0);
XLSXF8: array[0..15] of Byte = ($43, 4, $0C, 0, 1, $20, $F5, $FF, $20, $F8, 0, $CE, 0, 0, 0, 0);
XLSXF9: array[0..15] of Byte = ($43, 4, $0C, 0, 1, $1E, $F5, $FF, $20, $F8, 0, $CE, 0, 0, 0, 0);
XLSXF10: array[0..15] of Byte = ($43, 4, $0C, 0, 1, $0D, $F5, $FF, $20, $F8, 0, $CE, 0, 0, 0, 0);
XLSXF: array[0..15] of Byte = ($43, 4, $0C, 0, 5, 0, 1, 0, $21, $78, $41, 3, 0, 0, 0, 0);
XLSXFB: array[0..15] of Byte = ($43, 4, $0C, 0, 8, 0, 1, 0, $22, $78, $41, 2, $71, $71, $71, 0);
XLSXFH: array[0..15] of Byte = ($43, 4, $0C, 0, 6, 0, 1, 0, $22, $78, $41, 2, $71, $71, $71, $71);
XLSXFG: array[0..15] of Byte = ($43, 4, $0C, 0, 7, 0, 1, 0, $21, $78, $C1, 2, 0, 0, 0, 0);
XLSXFF: array[0..15] of Byte = ($43, 4, $0C, 0, 5, 0, 1, 0, $22, $78, $C1, 2, $B9, $B9, $B9, $B9);
XLSXFF1: array[0..15] of Byte = ($43, 4, $0C, 0, 5, 0, 1, 0, $22, $78, $C1, 2, $B9, 0, $B9, 0);
XLSXFRF: array[0..15] of Byte = ($43, 4, $0C, 0, 9, 0, 1, 0, $21, $78, $C1, 2, $B9, $B9, $B9, $B9);
XLSXFRF1: array[0..15] of Byte = ($43, 4, $0C, 0, 9, 0, 1, 0, $21, $78, $C1, 2, $B9, 0, $B9, 0);
XLSCOL: array[0..7] of Word = ($7D, $0C, 0, 0, 0, $F, 0, 0);
XLSSFONT: array[0..15] of Byte = ($31, 2, $0C, 0, $C8, 0, 0, 0, $FF, $7F, 5, $41, $72, $69, $61, $6C);
XLSLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
XLSBlank: array[0..4] of Word = ($201, 6, 0, 0, $17);
XLSBlankF: array[0..4] of Word = ($201, 6, 0, 0, $19);
XLSBlankRF: array[0..4] of Word = ($201, 6, 0, 0, $1C);
// XLSBlankH: array[0..4] of Word = ($201, 6, 0, 0, $16);
// XLSBlankB: array[0..4] of Word = ($201, 6, 0, 0, $1A);
XLSNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
XLSRK: array[0..4] of Word = ($27E, 10, 0, 0, 0);
XLSFormula: array[0..15] of Word =($406, $1D, 0, 0, $18, 0, 0, 0, 0, 3, $0B, $25, 0, 0, 0, 0);
 
function getNumFromCol(pvs_Column:string):integer;
begin
result:=0;
case length(pvs_Column) of
1: result:=ord(pvs_Column[1])-ord('A')+1;
2: result:=26+(ord(pvs_Column[1])-ord('A'))*26+(ord(pvs_Column[2])-ord('A')+1);
end;
end;

function getColFromNum(pvi_ColumnNo:integer):string;
begin
result:='';
if pvi_ColumnNo<=26 then
result:=chr(ord('A')+pvi_ColumnNo-1)
else begin
if pvi_ColumnNo mod 26<>0 then
result:=chr(ord('A')+(pvi_ColumnNo div 26)-1)+chr(ord('A')+((pvi_ColumnNo-26) mod 26)-1)
else
result:=chr(ord('A')+(pvi_ColumnNo div 26)-2)+chr(ord('A')+25)
end;
end;
 
多人接受答案了。
 
后退
顶部