今天在用POI生成Excel,写了一个从数字到excel列号的转换方法,记录下来
[code]
public static String int2Column(final int column)
{
int col = column + 1;
int system = 26;
char[] digArr = new char[100];
int ind = 0;
while (col > 0)
{
int mod = col % system;
if (mod == 0) mod = system;
digArr[ind++] = dig2Char(mod);
col = (col - 1) / 26;
}
StringBuffer bf = new StringBuffer(ind);
for (int i = ind - 1; i >= 0; i--)
{
bf.append(digArr[i]);
}
return bf.toString();
}
private static char dig2Char(final int dig)
{
int acs = dig - 1 + 'A';
return (char)acs;
}
[/code]
[code]
public static String int2Column(final int column)
{
int col = column + 1;
int system = 26;
char[] digArr = new char[100];
int ind = 0;
while (col > 0)
{
int mod = col % system;
if (mod == 0) mod = system;
digArr[ind++] = dig2Char(mod);
col = (col - 1) / 26;
}
StringBuffer bf = new StringBuffer(ind);
for (int i = ind - 1; i >= 0; i--)
{
bf.append(digArr[i]);
}
return bf.toString();
}
private static char dig2Char(final int dig)
{
int acs = dig - 1 + 'A';
return (char)acs;
}
[/code]