本文主要是介绍phpoffice PhpSpreadsheet 制作多sheet表格,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
phpoffice PhpSpreadsheet 制作多sheet表格
public function quotationExcel($id){
set_time_limit(0);
log_message('error', '进入quotationExcel');
// $unit = $this->unitchen();
$this->CI->load->model('Quotation_Model', 'quotation');
$data = $this->CI->quotation->getQuotationById($id);
$product = $this->CI->quotation->getQuotationInfoById($id);
$customer = $this->CI->quotation->getCustomerById($id);
$user = $this->CI->quotation->getUserById($id);
$library = $this->CI->quotation->getLibrary();
$path = '/uploads/excel/'. date('Ymd', time());
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$path)) {
mkdir($_SERVER['DOCUMENT_ROOT'].$path, 0777, true);
}
$filename = $path .'/'. $data['quotationNo']. '-'. date('Y-m-d').'.xlsx';
$template_path = './uploads/quotation.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template_path);
$clonedWorksheet = clone $spreadsheet->getActiveSheet();
for ($i=1;$i < $product['count'];$i++){
$clonedWorksheet->setTitle('Copys'.$i);
$spreadsheet->addSheet($clonedWorksheet);
}
foreach ($product['info'] as $k=>$val){
$spreadsheet->setActiveSheetIndex($k);
$worksheet = $spreadsheet->getActiveSheet()->setTitle($val['sortName']);
$length = 0;
//设置采购抬头
$worksheet->getCell('I1')->setValue('Contact:'.$user['email'].utf8_encode(chr(10))."P:".$user['tel']);
$worksheet->getCell('D7')->setValue(date('Ymd',strtotime($data['createtime'])));
$worksheet->getCell('J7')->setValue($data['quoteValidDate']);
$worksheet->getCell('D8')->setValue($customer['customername']);
$worksheet->getCell('J8')->setValue($customer['contactsname']);
$worksheet->getCell('D9')->setValue($data['exchangeRate']);
/*$worksheet->getCell('J8')->setValue('Telephone:+86-'.$this->user['tel']);
$worksheet->getCell('J9')->setValue(' Email:'.$this->user['email']);
$worksheet->getCell('A14')->setValue($data['address_country'].utf8_encode(chr(10)).$data['address_region'].utf8_encode(chr(10)).$data['address_detailed']);
$worksheet->getCell('L13')->setValue($data['order_no']);
$worksheet->getCell('L14')->setValue(date('Y/m/d'));
$worksheet->getCell('L15')->setValue($data['inquiry_no']);
$worksheet->getCell('L16')->setValue($data['order_no']);*/
if($val['info']){
$length = count($val['info']) ?? 0;
if($length > 1){
$worksheet->insertNewRowBefore(10,($length - 1) * 8);
}else if($length = 0){
$worksheet->removeRow(10,8);
}
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startColor' => [
'argb' => '335593',
],
'endColor' => [
'argb' => '335593',
],
],
];
$BstyleArray = [
'borders' => [//边框
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '00000000'],
],
],
];
foreach ($val['info'] as $key => $v){
$worksheet->getStyle('A'.(10 + 8 * $key).':L'.(17 + 8 * $key))->applyFromArray($BstyleArray);
$worksheet->getStyle('A'.(10 + 8 * $key).':L'.(10 + 8 * $key))->applyFromArray($styleArray);
$worksheet->getStyle('A'.(10 + 8 * $key).':L'.(10 + 8 * $key))->getFont()->setBold(true)->setSize(14);
$worksheet->getStyle('A'.(10 + 8 * $key).':L'.(10 + 8 * $key))->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);
$worksheet->mergeCells('A'.(10 + 8 * $key).':L'.(10 + 8 * $key));
$worksheet->getCell('A'.(10 + 8 * $key))->setValue($v['englishname']);
$ZstyleArray = [
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startColor' => [
'argb' => 'fbe4d5',
],
'endColor' => [
'argb' => 'fbe4d5',
],
],
];
$ZstyleArray2 = [
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startColor' => [
'argb' => 'ffff00',
],
'endColor' => [
'argb' => 'ffff00',
],
],
];
$worksheet->getStyle('A'.(11 + 8 * $key).':F'.(11 + 8 * $key))->applyFromArray($ZstyleArray);
$worksheet->getStyle('J'.(11 + 8 * $key).':L'.(11 + 8 * $key))->applyFromArray($ZstyleArray2);
$worksheet->getStyle('A'.(12 + 8 * $key).':L'.(12 + 8 * $key))->applyFromArray($ZstyleArray);
$worksheet->mergeCells('A'.(11 + 8 * $key).':C'.(11 + 8 * $key));
$worksheet->getCell('A'.(11 + 8 * $key))->setValue('Product Type');
$worksheet->mergeCells('D'.(11 + 8 * $key).':F'.(11 + 8 * $key));
$worksheet->getCell('D'.(11 + 8 * $key))->setValue($library[$v['ProductType']]);
$worksheet->mergeCells('G'.(11 + 8 * $key).':I'.(11 + 8 * $key));
$worksheet->getCell('G'.(11 + 8 * $key))->setValue('Unit Price');
$worksheet->mergeCells('J'.(11 + 8 * $key).':L'.(11 + 8 * $key));
$worksheet->getCell('J'.(11 + 8 * $key))->setValue($library[$data['currency']].$v['productUnitPrice'].'/'.$library[$v['productunit']]);
if ($v['attribute']){
$attributes = $v['attribute'];
$attributes = explode('&',$attributes);
$attribute = [];
foreach ($attributes as $ak=>$av){
$attributeArr[$ak] = explode(':',$av);
if (count($attributeArr[$ak]) > 1){
$attribute[current($attributeArr[$ak])] = next($attributeArr[$ak]);
}
}
unset($attributes);
if (array_key_exists('Size Available',$attribute)){
if (array_key_exists('Lenght available',$attribute)){
$worksheet->mergeCells('A'.(12 + 8 * $key).':B'.(12 + 8 * $key));
$worksheet->getCell('A'.(12 + 8 * $key))->setValue('Size Available');
$worksheet->mergeCells('C'.(12 + 8 * $key).':F'.(12 + 8 * $key));
$worksheet->getCell('C'.(12 + 8 * $key))->setValue($attribute['Size Available']);
unset($attribute['Size Available']);
$worksheet->mergeCells('G'.(12 + 8 * $key).':H'.(12 + 8 * $key));
$worksheet->getCell('G'.(12 + 8 * $key))->setValue('Lenght available');
$worksheet->mergeCells('I'.(12 + 8 * $key).':L'.(12 + 8 * $key));
$worksheet->getCell('I'.(12 + 8 * $key))->setValue($attribute['Lenght available']);
unset($attribute['Lenght available']);
} else {
$worksheet->mergeCells('A'.(12 + 8 * $key).':B'.(12 + 8 * $key));
$worksheet->getCell('A'.(12 + 8 * $key))->setValue('Size Available');
$worksheet->mergeCells('C'.(12 + 8 * $key).':L'.(12 + 8 * $key));
$worksheet->getCell('C'.(12 + 8 * $key))->setValue($attribute['Size Available']);
unset($attribute['Size Available']);
}
}
$atks = 0;
foreach ($attribute as $atk=>$atv){
$worksheet->mergeCells('G'.((13 + $atks) + 8 * $key).':I'.((13 + $atks) + 8 * $key));
$worksheet->getCell('G'.((13 + $atks) + 8 * $key))->setValue($atk);
$worksheet->mergeCells('J'.((13 + $atks) + 8 * $key).':L'.((13 + $atks) + 8 * $key));
$worksheet->getCell('J'.((13 + $atks) + 8 * $key))->setValue($atv);
$atks++;
}
// 产品图片
$worksheet->mergeCells('A'.(13 + 8 * $key).':F'.(17 + 8 * $key));
// $v['files'] = '/uploads/quotation_ceshi.png,/uploads/quotation_ceshi.png';
if ($v['files']){
$files = explode(',',$v['files']);
$fileCount = count($files);
if ($fileCount == 3){
$fileKey = ['A', 'C', 'E'];
}elseif ($fileCount == 2){
$fileKey = ['A','D'];
}elseif ($fileCount == 1){
$fileKey = ['A'];
}
foreach ($files as $f=>$fe){
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo'.$f);
$drawing->setDescription('Logo'.$f);
// $drawing->setPath($this->webHttpName.$v['goods_thumbnail']);
$drawing->setPath('.'.$fe);
// $drawing->setPath('./uploads/files/20201021/b16e0457f1a1dad4aec02c926f6827ded.jpg');
$drawing->setHeight(80);
$drawing->setCoordinates($fileKey[$f].(13 + 8 * $key));
$drawing->setOffsetX(10);
$drawing->setOffsetY(10);
$drawing->setWorksheet($worksheet);
}
}
}
}
if ($length == 0){
$length = 1;
}
$worksheet->getCell('A'.(21 + 8 * $length))->setValue($data['remark']);
}
/*if ($k < ($product['count'] - 1)){
$spreadsheet->createSheet();
}*/
}
$this->clearout($filename,$spreadsheet);
return $filename;
}
这篇关于phpoffice PhpSpreadsheet 制作多sheet表格的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!