官方文档Spout
execl导入功能
public static function importFile($filePath)
{
try{
ini_set('memory_limit', '100M');
set_time_limit(1000);
$reader=ReaderEntityFactory::createXLSXReader();
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$rowArr[] = $row->toArray();
}
}
unset($rowArr[0]);
$reader->close();
}catch (\Exception $e){
throw new \Exception($e->getMessage());
}
return $rowArr;
}
execl导出功能
public static function exportFile($headerCell=[],$name='test.xls',$data=[])
{
try{
ini_set('memory_limit', '100M');
set_time_limit(1000);
$backStyle = (new StyleBuilder())
->setBackgroundColor(Color::LIGHT_BLUE)
->setFontColor(Color::BLACK)
->setFontBold()
->build();
$border=(new BorderBuilder())->setBorderTop(Color::BLACK,Border::WIDTH_THIN)
->setBorderLeft(Color::BLACK,Border::WIDTH_THIN)
->setBorderRight(Color::BLACK,Border::WIDTH_THIN)
->setBorderBottom(Color::BLACK,Border::WIDTH_THIN)
->build();
$borderStyle=(new StyleBuilder())->setBorder($border)->build();
$writer = WriterEntityFactory::createXLSXWriter();
foreach ($headerCell as $val){
$cells[] = WriterEntityFactory::createCell($val,$backStyle);
}
$singleRow = WriterEntityFactory::createRow($cells)->setStyle($borderStyle);
$writer->openToBrowser("php://output/" . $name)
->addRow($singleRow);
foreach ($data as $value) {
$rowFromValues = WriterEntityFactory::createRowFromArray($value);
$writer->setDefaultRowStyle($borderStyle)->addRow($rowFromValues);
}
$writer->close();
return true;
}catch (\Exception $e){
throw new \Exception($e->getMessage());
}
}
|