把行号(row)和列号(col)转换成Excel的表示形式。 例如:A1,B2

0,0 ⇒ A1


import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;

public class TestPOI {
public static void main(String[] args) {
System.out.println(getCellName(0, 0)); //A1
System.out.println(getCellName(3, 28)); //AC4
System.out.println(getCellName(5, 255)); //IV6

}

public static String getCellName(int row, int col) {
if (row < 0) {
throw new IllegalArgumentException("row: " + row);
}
if (col < 0) {
throw new IllegalArgumentException("col: " + col);
}
//return CellReference.convertNumToColString(col) + (row + 1);
return new CellReference(row, col).formatAsString();
}
}


输出:
A1
AC4
IV6

需要区域转换成

CellRangeAddress address = new CellRangeAddress(0, 4, 0, 3);
System.out.println(address.formatAsString());

输出:
A1:D5

其中用了POI的库,下载。
[url]http://poi.apache.org/download.html[/url]


如果希望反过来。
A1 ⇒ 0,0

CellReference cr = new CellReference("A1");
System.out.println(cr.getRow() + ":" + cr.getCol());


区域的

CellRangeAddress address = CellRangeAddress.valueOf("A1:D5");
int firstRow = address.getFirstRow();
int lastRow = address.getLastRow();
int firstColumn = address.getFirstColumn();
int lastColumn = address.getLastColumn();
System.out.println(firstRow + ":" + lastRow + ":" + firstColumn + ":" + lastColumn);

输出:
0:4:0:3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值