php PHPExcel获取相对于给定列的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15147110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel get column name relative to given column
提问by raidzero
Using PHPExcel, is it possible to get the name of a column located X number of columns to the left or right?
使用 PHPExcel,是否可以获取位于 X 列左侧或右侧的列的名称?
Example, given column BZ, I'd like to return column name CB or BX. (2 to the right or left)
例如,给定列 BZ,我想返回列名 CB 或 BX。(向右或向左 2 个)
Thanks
谢谢
回答by Mark Baker
There are functions already built into PHPExcel to help you do this
PHPExcel 中已经内置了一些函数来帮助你做到这一点
$adjustment = -2;
$currentColumn = 'BZ';
$columnIndex = PHPExcel_Cell::columnIndexFromString($currentColumn);
$adjustedColumnIndex = $columnIndex + $adjustment;
$adjustedColumn = PHPExcel_Cell::stringFromColumnIndex($adjustedColumnIndex - 1);
Note the (historic) discrepancy that columnIndexFromString() will return a 1 for column A, but that stringFromColumnIndex expects a 0 to correspond to column A
请注意 columnIndexFromString() 将为 A 列返回 1 的(历史性)差异,但 stringFromColumnIndex 期望 0 对应于 A 列

