PHP Export EXCEL Rapid Development Guide A detailed guide to using PHPEXCEL

  • 2020-06-07 04:03:22
  • OfStack

PHP exports the EXCEL Rapid development guide
phpexcel has a proprietary development document, please refer to its development document for detailed operation. This document only optimizes the use of phpexcel for rapid development in new projects.
There are also two ways to generate phpexcel files, one for direct output and one for generating static files.
Direct output:
The main file is (the same directory file as the class directory) :

<?php 
include("./class/class.php"); //  contains class The basic header file 
include("./class/phpexcel/PHPExcel.php"); //  generate excel The basic class definition of ( Note the case of file names )
//  If you output it directly excel File, to include this file 
include("./class/phpexcel/PHPExcel/IOFactory.php");
//  create phpexcel Object that contains the content and format of the output 
$m_objPHPExcel = new PHPExcel();
//  Template file, in order to achieve the separation of format and content, the specific content of the output file is implemented in the template file 
//  Template files will be objects $m_objPHPExcel operate 
include("./include/excel.php");
//  Type of output file, excel or pdf
$m_exportType = "excel";
$m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; //  The output EXCEL The file name 
$m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; //  The output PDF The file name 
// PHPExcel_IOFactory,  The output excel
//require_once dirname(__FILE__).'/Classes/PHPExcel/IOFactory.php';
//  If you need output EXCEL format 
if($m_exportType=="excel"){
$objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'Excel5');
//  Output directly from the browser $m_strOutputExcelFileName
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type: application/vnd.ms-excel;");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=".$m_strOutputExcelFileName);
header("Content-Transfer-Encoding:binary");
$objWriter->save("php://output"); 
}
//  If you need output PDF format 
if($m_exportType=="pdf"){
$objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'PDF');
$objWriter->setSheetIndex(0);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type: application/pdf");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName);
header("Content-Transfer-Encoding:binary");
$objWriter->save("php://output"); 
}
?>

Template file contents (additional common operations)

<?php 
global $m_objPHPExcel; //  Defined by an external file 
//  Set basic properties 
$m_objPHPExcel->getProperties()->setCreator("Sun Star Data Center")
->setLastModifiedBy("Sun Star Data Center")
->setTitle("Microsoft Office Excel Document")
->setSubject("Test Data Report -- From Sunstar Data Center")
->setDescription("LD Test Data Report, Generate by Sunstar Data Center")
->setKeywords("sunstar ld report")
->setCategory("Test result file");
//  Create multiple workbooks 
$sheet1 = $m_objPHPExcel->createSheet();
$sheet2 = $m_objPHPExcel->createSheet();
//  The corresponding workbook can be manipulated by manipulating the index 
//  Simply set the index of the workbook to be operated on to the current active workbook, for example 
// $m_objPHPExcel->setActiveSheetIndex(0);
//  Set up the first 1 Each workbook is an activity workbook 
$m_objPHPExcel->setActiveSheetIndex(0);
//  Set the active workbook name 
//  If it's In Chinese 1 Need to use iconv Function conversion encoding 
$m_objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', ' Test workbook '));
//  Sets the default font and size 
$m_objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', ' Song typeface '));
$m_objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
//  Set up the 1 The width of the column 
$m_objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
//  Set up the 1 The highly 
$m_objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
//  Merged cell 
$m_objPHPExcel->getActiveSheet()->mergeCells('A1:P1');
//  define 1 Style, bold, center 
$styleArray1 = array(
'font' => array(
'bold' => true,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
//  Apply the style to A1 The cell 
$m_objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);
//  Set the cell style (black font) 
$m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //  black 
//  Set the cell format (background) 
$m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); //  Set the background to light pink 
//  Set the cell format (number format) 
$m_objPHPExcel->getActiveSheet()->getStyle('F1')->getNumberFormat()->setFormatCode('0.000');
//  Writes to a specific cell 
$m_objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
//  Set the cell style (center) 
$m_objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//  Put pictures into cells ,  Put the data picture in J1 Within the cell 
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath("../logo.jpg"); //  Picture path, can only be relative path 
$objDrawing->setWidth(400); //  Image width 
$objDrawing->setHeight(123); //  Picture height 
$objDrawing->setCoordinates('J1');// The cell 
$objDrawing->setWorksheet($m_objPHPExcel->getActiveSheet());
//  Set up the A5 Cell content and add hyperlinks 
$m_objPHPExcel->getActiveSheet()->setCellValue('A5', iconv('gbk', 'utf-8', ' hyperlinks keiyi.com'));
$m_objPHPExcel->getActiveSheet()->getCell('A5')->getHyperlink()->setUrl('http://www.keiyi.com/');
?>

Generate static files on the server side
The main difference between these two methods is the different format of the build. The template file is exactly the same.

<?php 
//  contains class The basic header file 
include("./class/class.php");
//  generate excel The basic class definition of ( Note the case of file names )
include("./class/phpexcel/PHPExcel.php");
//  Contains the writing Excel5 Format the file if needed excel2007 Contains the corresponding file Writer Can be 
include("./class/phpexcel/PHPExcel/Writer/Excel5.php");
//  Contains the writing PDF Format file 
include("./class/phpexcel/PHPExcel/Writer/PDF.php");
//  create phpexcel Object that contains the content and format of the output 
$m_objPHPExcel = new PHPExcel();
//  Template file, in order to achieve the separation of format and content, the specific content of the output file is implemented in the template file 
//  Template files will be objects $m_objPHPExcel operate 
include("./include/excel.php");
//  Type of output file, excel or pdf
$m_exportType = "pdf";
$m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; //  The output EXCEL The file name 
$m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; //  The output PDF The file name 
//  The output file saves the path, which must be writable 
$m_strOutputPath = "./output/";
//  If you need output EXCEL format 
if($m_exportType=="excel"){
$objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputExcelFileName); 
}
//  If you need output PDF format 
if($m_exportType=="pdf"){
$objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputPdfFileName); 
}
?>


Related articles: