问题:
我要实现的目标是从一个Excel电子表格中复制选定范围的单元格,并使用laravel-excel
和phpspreadsheet
库将其插入到新生成的单元格中。到目前为止,我所拥有的代码可以做到这一点,但并不理想。
Excel::load($file, function($reader)
{
$activeSheet = $reader->getActiveSheet();
$this->data = $activeSheet->rangeToArray(
'A1:A27', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
true, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
true, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
true // Should the array be indexed by cell row and cell column
);
});
// Create new file.
$newExport = Excel::create('Filename', function($excel) {
$excel->sheet('Sheetname', function($sheet) {
$sheet->fromArray($this->data, null, 'B1', true);
});
});
// Export newly created file.
$newExport->export('xlsx');
问题在于它还将列名插入第一个单元格(在屏幕快照上为 0
,因为我关闭了索引编制功能,打开了索引编制功能,还会插入A
),如下面的截图所示。
我尝试过的东西:
rangeToArray
方法中的索引编制。fromArray()
中设置了第二个参数),但这并不实用,因为我最终将每个列名都添加到忽略列表中,此外,它将空白值插入第一列并开始来自B2
单元格。如果有人可以给我一些解决方案的想法,那就太好了。
谢谢!
答案 0 :(得分:1)
此案的答案如下...
阅读您实际使用的库的文档,而不是它所基于的库的文档。
之所以添加列标题,是由于laravel-excel库fromArray
方法的默认属性值,该属性默认情况下启用了heading generation
。
$sheet->fromArray($this->data, null, 'B1', true);
更改为
$sheet->fromArray($this->data, null, 'B1', true, false);
可接受的参数:
fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration)
。