将样式从一行复制并粘贴到另一行 [英] Copy and paste styles from one row to other
问题描述
我正在使用 PHP Excel 使用模板Excel文件创建Excel.问题是我有一个数据网格,并在模板中设置了标题和第一行的样式.这里是这样的:
I'm using PHP Excel to create an Excel using a template Excel file. The problem is I have a datagrid and which I styled the header and first row in template. Here how it looks like:
最左上角的坐标是C49.
The top leftmost coordinate is C49.
如果我有100行,则需要复制第一行的样式并将其粘贴100次. 这是我的代码
If I have 100 rows, I need to copy style of first row and paste it 100 times. Here is my code
$cstart = 2;
$rstart = 49;
$count = 1;
$input = $worksheet->getStyle(num2char($cstart) . $rstart);
foreach ($b_data['rawData'] as $value) {
$worksheet->setCellValueByColumnAndRow($cstart, $rstart, $count++)
->setCellValueByColumnAndRow($cstart + 1, $rstart, $value['key'])
->setCellValueByColumnAndRow($cstart + 5, $rstart, $value['value']);
$interval = num2char($cstart) . $rstart . ':' . num2char($cstart+5) . $rstart;
$worksheet->duplicateStyle($input, $interval);
$rstart++;
}
function num2char($num) {
$numeric = $num % 26;
$letter = chr(65 + $numeric);
$num2 = intval($num / 26);
if ($num2 > 0) {
return num2char($num2 - 1) . $letter;
} else {
return $letter;
}
}
但是,我有以下内容:
但是我期望的是:
是错误还是我做错了什么?
Is it bug or am I doing something wrong?
推荐答案
正如Mark在评论中指出的那样;合并的单元格是结构性的,而不是样式性的.因此,复制样式不会自动复制合并的单元格.
As Mark pointed out in the comments; a merged cell is structural, not style. So copying the style will not automatically copy the merged cells.
有一个功能请求,以便能够复制包括合并单元格在内的整个行
There is a feature request to be able to duplicate entire rows including merged cells
处理此问题的一种方法是使用像这样的isInMergeRange()
函数检查单元格是否是合并的一部分:
One way to deal with this is to check if the cells are part of a merge using the isInMergeRange()
function like this:
$workbook = new PHPExcel; // prepare the workbook
$sheet = $workbook->getActiveSheet(); // get the sheet
$sheet->mergeCells('A1:E1'); // merge some cells for tesing
$cell = $sheet->getCell('A1'); // get a cell to check if it is merged
$cell->isInMergeRange() // check if cell is merged
^这将返回一个布尔值,指示它是否为合并单元格的一部分.
^This returns a Boolean value indicating if it is part of a merged cell.
您可能感兴趣的另一个功能是isMergeRangeValueCell()
功能:
Another function that may interest you is the isMergeRangeValueCell()
function:
$cell->isMergeRangeValueCell()
^这将返回一个布尔值,指示它是合并单元格 的一部分;该单元格包含合并范围的值;在其他所有情况下都会返回false.
^This returns a Boolean value indicating it is part of a merged cell and the cell contains the value for the merged range; it returns false in all other situations.
这篇关于将样式从一行复制并粘贴到另一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!