php - 在PHPExcel中按数字设置列值
问题描述
我有一个带有规格的数组。我希望每个规范都成为一列。不过,我在解决这个问题时遇到了麻烦。
$specifications = new Specifications();
$columnCounter = 1;
foreach ($specifications as $specificationId => $specification) {
$column = PHPExcel_Cell::getColumnByNumber($columnCounter);
$objPHPExcel
->getActiveSheet()
->getColumnDimension($column)
->setAutoSize(true)
;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($column.'1', $specification['value'])
;
$columnCounter++;
}
当然是PHPExcel::getColumnByNumber()
一个虚函数。虽然我想知道其他人如何做到这一点以及如何最好地解决这个问题。
解决方案
$book = new PHPExcel();
$book->setActiveSheetIndex(0);
$sheet = $book->getActiveSheet();
$sheet->setTitle('Sets');
$xls_row = 5;
$xls_col = 3;
foreach($specifications as $specificationId => &$specification)
{
$adr = coord($xls_row, $xls_col);
$sheet->setCellValueExplicit($adr, $specification->title, PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getColumnDimension(coord_x($xls_col))->setAutoSize(true);
$xls_col++;
}
// convert a 0-based coordinate value into EXCEL B1-format
function coord_x($x)
{
if($x<26) $x = chr(ord('A')+$x);
else
{
$x -= 26;
$c1 = $x % 26;
$c2 = intval(($x - $c1)/26);
$x = chr(ord('A')+$c2).chr(ord('A')+$c1);
}
return $x;
}
// convert X,Y 0-based cell address into EXCEL B1-format pair
function coord($y,$x)
{
return coord_x($x).($y+1);
}
推荐阅读
- python-3.x - 有没有办法在 OOP 中的另一种方法中使用一种方法?
- javascript - Extjs 6 - 如何在网格中为嵌套数据使用 DataIndex
- javascript - 使用 npm 命令安装 Nodemon 的问题
- r - R- 从公式中获取输出值列表
- ios - 访问嵌入在 UIViewController 中的 UITabBarController
- c# - 无法检查用户角色。User.IsInRole 返回 false
- python - Python多处理队列重复
- python - 如何将输入字符串附加到字典键 int 中?
- android - Visual Studio 2015 中的 Xamarin 设计师仅查看黑色
- node.js - 将 JSON 数据从 NodeJS/Express 发送到 EJS 模板时出现问题