if (!function_exists (' Exportexcel ')) {
Export Excel File
function Exportexcel ($datas, $save _method= ' http ', $save _path= ') {
Set_time_limit (0);
Error_reporting (E_all);
Ini_set (' display_errors ', TRUE);
Ini_set (' display_startup_errors ', TRUE);
$excel _title = ' box Details list '. Date (' Ymdhis ', Time ());
$code = $datas [' Code '];
$totalBox = $datas [' Totalbox '];
$totalPackages = $datas [' totalpackages '];
$totalWeight = $datas [' Totalweight '];
$data = $datas [' Data '];
Require_once Root_path. ' /classes/phpexcel.php ';
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ()
->setcreator ("Maarten Balliauw")
->setlastmodifiedby ("Maarten Balliauw")
->settitle ("Office" XLSX Test Document ")
->setsubject ("Office" XLSX Test Document ")
->setdescription ("Test document for Office" XLSX, generated using PHP classes. ")
->setkeywords ("Office openxml PHP")
->setcategory ("Test result file");
$row = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ');
Set column widths
$objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (12);
$objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (23);
$objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (10);
$objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (18);
$objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setwidth (22);
$objPHPExcel->getactivesheet ()->getcolumndimension (' F ')->setwidth (23);
$objPHPExcel->getactivesheet ()->getcolumndimension (' G ')->setwidth (12);
$objPHPExcel->getactivesheet ()->getcolumndimension (' H ')->setwidth (15);
$styleThinBlackBorderOutline = Array (
' Borders ' = Array (
' Outline ' = Array (
' Style ' = Phpexcel_style_border::border_thin,
' Color ' = = Array (' ARGB ' = ' FF000000 '),
),
);
for ($i =0; $i <count ($row); $i + +) {
Set border
for ($a = 1; $a < 4; $a + +) {
$objPHPExcel->getactivesheet ()->getstyle ($row [$i]. $a)->applyfromarray ($styleThinBlackBorderOutline);
}
Set Center
for ($j =1; $j <500; $j + +) {
$objPHPExcel->getactivesheet ()->getstyle ($row [$i]. $j)->getalignment ()->sethorizontal (phpexcel_ Style_alignment::vertical_center);
$objPHPExcel->getactivesheet ()->getstyle ($row [$i]. $j)->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center);
}
Set Background color
$objPHPExcel->getactivesheet (0)->getstyle ($row [$i]. ' 1 ')->getfill ()->setfilltype (\phpexcel_style_fill::fill_solid);
$objPHPExcel->getactivesheet (0)->getstyle ($row [$i]. ' 1 ')->getfill ()->getstartcolor ()->setargb (' 00ffff00 ');
$objPHPExcel->getactivesheet (0)->getstyle ($row [$i]. ' 2: '. $row [$i]. ' 3 ')->getfill ()->setfilltype (\phpexcel_style_fill::fill_solid);
$objPHPExcel->getactivesheet (0)->getstyle ($row [$i]. ' 2: '. $row [$i]. ' 3 ')->getfill ()->getstartcolor ()->setargb (' 00dbe5f1 ');
Set line breaks
$objPHPExcel->getactivesheet ()->getstyle ($row [$i]. ' 2 ')->getalignment ()->setwraptext (true);
}
Set row height
$objPHPExcel->getactivesheet ()->getdefaultrowdimension ()->setrowheight (28);
$objPHPExcel->getactivesheet ()->getrowdimension (' 2 ')->setrowheight (66);
Defining the first three rows of a table
$objPHPExcel->getactivesheet ()
->mergecells (' a1:b1 ')
->mergecells (' c1:d1 ')
->mergecells (' E1:f1 ')
->mergecells (' g1:h1 ')
->mergecells (' b2:b3 ')
->mergecells (' c2:c3 ')
->mergecells (' H2:h3 ')
->mergecells (' g2:g3 ');
$objPHPExcel->setactivesheetindex (0)
->setcellvalue (' A1 ', ' Customer Code: ')
->setcellvalue (' C1 ', $code)
->setcellvalue (' E1 ', ' dates date: ')
->setcellvalue (' G1 ', date (' y-m-d h:i:s ', Time ()))
->setcellvalue (' A2 ', ' Total number of boxes: '. $totalBox. " \ n "." Total Box ')
->setcellvalue (' A3 ', ' box Number ')
->setcellvalue (' B2 ', ' transshipment number '). \ n "." Tracking number ')
->setcellvalue (' C2 ', ' Order number ')
->setcellvalue (' D2 ', ' Total number of packages: '. $totalPackages. " \ n "." Total Packages ')
->setcellvalue (' D3 ', ' parcel weight (g) ')
->setcellvalue (' E2 ', ' Total box weight (kg): '. $totalWeight. ' \ n "." Total Weight ')
->setcellvalue (' E3 ', ' box weight (kg) ')
->setcellvalue (' F2 ', ' total volume weight '). " \ n "." (Length * Width * height cm/5000)
->setcellvalue (' F3 ', ' box volume (l * w * h cm) ')
->setcellvalue (' G2 ', ' head-on '. ") \ n "." Delivery method ')
->setcellvalue (' H2 ', ' tracking number '. \ n "." Tracking number ');
Handling the logic behind
Number of rows currently in
$num = 4;
foreach ($data as $k = = $v) {
Number of specific orders
$len = count ($v);
$mergeLen = $num + $len-4;
$objPHPExcel->getactivesheet ()
->mergecells (' e '. $num. ': E '. $mergeLen)
->mergecells (' F '. $num. ': F '. $mergeLen)
->mergecells (' G '. $num. ': G '. $mergeLen)
->mergecells (' h '. $num. ': H '. $mergeLen);
$objPHPExcel->setactivesheetindex ()
->setcellvalue (' A '. $num, $v [' Boxid '])
->setcellvalue (' E '. $num, $v [' WEIGHTSS '])
->setcellvalue (' G '. $num, $v [' Carrier_company ']);
for ($i =0; $i < $len-3; $i + +) {
$objPHPExcel->setactivesheetindex ()
->setcellvalue (' A '. $num, $v [' Boxid '])
->setcellvalueexplicit (' B '. $num, $v [$i] [' Tracknumber '],phpexcel_cell_datatype::type_string]
->setcellvalue (' C '. $num, $v [$i] [' ebay_id '])
->setcellvalue (' D '. $num, $v [$i] [' weight ']);
$num + +;
}
}
$objPHPExcel->getactivesheet ()->settitle ($excel _title);
$objPHPExcel->setactivesheetindex (0);
$filename = $excel _title. XLS ';
if ($save _method = = = ' file ') {
Save to File
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$full _path = $save _path. $filename;
Local use, on-line annotations
$full _path = iconv (' utf-8 ', ' Gbk//ignore ', $full _path);
if (Is_file ($full _path)) unlink ($full _path);
$objWriter->save ($full _path);
return $full _path;
}else{
Ob_end_clean ();
Directly in the browser output
Header (' Content-type:application/vnd.ms-excel ');
Header ("content-disposition:attachment;filename={$filename}");
Header (' cache-control:max-age=0 ');
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
}
}
}
Phpexcel Exporting Excel tables