Summary of common methods based on PHPExcel

  • 2020-06-15 07:51:37
  • OfStack


//  usually PHPExcel Objects can be instantiated in two ways 
// 1.  through new Keywords create blank documents 
$phpexcel = newPHPExcel();
// 2.  Create by reading from an existing template 
$phpexcel =PHPExcel_IOFactory::createReader("Excel5")->load("template.xls");


<?
// Set up the PHPExcel The class library include path
set_include_path('.'. PATH_SEPARATOR .
                 'D:\Zeal\PHP_LIBS' . PATH_SEPARATOR .
                 get_include_path());
/**
 *  Here is an example of how to use it  ////  The opening line is optional in different ways, please according to the actual needs 
 *  Open the comment for the corresponding line. 
 *  If you are using  Excel5  , the output should be GBK Encoding. 
 */
require_once 'PHPExcel.php';
// uncomment
////require_once 'PHPExcel/Writer/Excel5.php';    //  For other lower versions xls
// or
////require_once 'PHPExcel/Writer/Excel2007.php'; //  Used for  excel-2007  format 
//  create 1 Number of processing object instances 
$objExcel = new PHPExcel();
//  Create file format write object instances , uncomment
////$objWriter = new PHPExcel_Writer_Excel5($objExcel);    //  For other version formats 
// or
////$objWriter = new PHPExcel_Writer_Excel2007($objExcel); //  Used for  2007  format 
//$objWriter->setOffice2003Compatibility(true);
//*************************************
// Set the document base properties 
$objProps = $objExcel->getProperties();
$objProps->setCreator("Zeal Li");
$objProps->setLastModifiedBy("Zeal Li");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");
//*************************************
// Set the current sheet Index for subsequent content operations. 
//1 Generally only in use of multiple sheet When you need to display the call. 
// By default, PHPExcel The control is automatically created 1 a sheet Is set SheetIndex=0
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
// Set current activity sheet The name of the 
$objActSheet->setTitle(' test Sheet');
//*************************************
// Sets the cell content 
//
// by PHPExcel Automatically determines the cell content type based on incoming content 
$objActSheet->setCellValue('A1', ' String content ');  //  String content 
$objActSheet->setCellValue('A2', 26);            //  The numerical 
$objActSheet->setCellValue('A3', true);          //  Boolean value 
$objActSheet->setCellValue('A4', '=SUM(A2:A2)'); //  The formula 
// Explicitly specify the content type 
$objActSheet->setCellValueExplicit('A5', '847475847857487584', 
                                   PHPExcel_Cell_DataType::TYPE_STRING);
// Merged cell 
$objActSheet->mergeCells('B1:C22');
// Detached cell 
$objActSheet->unmergeCells('B1:C22');
//*************************************
// Sets the cell style 
//
// Set the width 
$objActSheet->getColumnDimension('B')->setAutoSize(true);
$objActSheet->getColumnDimension('A')->setWidth(30);
$objStyleA5 = $objActSheet->getStyle('A5');
// Sets the numeric format for the cell content. 
//
// If you use  PHPExcel_Writer_Excel5  To generate content, 
// Here's the thing to notice  PHPExcel_Style_NumberFormat  Of the class  const  Variable defined 
// In various custom formatting modes, other types can be used normally, but when setFormatCode
// for  FORMAT_NUMBER  The actual effect is not formatted as "0" . Need to be 
// Modify the  PHPExcel_Writer_Excel5_Format  Class source code  getXf($style)  Method, 
// in  if ($this->_BIFF_version == 0x0500) {  (the first 363 Row near) front increases 1
// Lines of code : 
//if($ifmt === '0') $ifmt = 1;
//
// Set the format to PHPExcel_Style_NumberFormat::FORMAT_NUMBER Avoid some big Numbers 
// Shown in scientific notation, in conjunction with the following  setAutoSize  The method can make every 1 lines 
// It all appears as the original content. 
$objStyleA5
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
// Set the font 
$objFontA5 = $objStyleA5->getFont();
$objFontA5->setName('Courier New');
$objFontA5->setSize(10);
$objFontA5->setBold(true);
$objFontA5->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objFontA5->getColor()->setARGB('FF999999');
// Set alignment 
$objAlignA5 = $objStyleA5->getAlignment();
$objAlignA5->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objAlignA5->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// Set the border 
$objBorderA5 = $objStyleA5->getBorders();
$objBorderA5->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getTop()->getColor()->setARGB('FFFF0000'); // color
$objBorderA5->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
// Set fill color 
$objFillA5 = $objStyleA5->getFill();
$objFillA5->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objFillA5->getStartColor()->setARGB('FFEEEEEE');
// Copies style information from the specified cell .
$objActSheet->duplicateStyle($objStyleA5, 'B1:C22');
//*************************************
// Add images 
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('ZealImg');
$objDrawing->setDescription('Image inserted by Zeal');
$objDrawing->setPath('./zeali.net.logo.gif');
$objDrawing->setHeight(36);
$objDrawing->setCoordinates('C23');
$objDrawing->setOffsetX(10);
$objDrawing->setRotation(15);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(36);
$objDrawing->setWorksheet($objActSheet);
// add 1 A new one worksheet
$objExcel->createSheet();
$objExcel->getSheet(1)->setTitle(' test 2');
// Guard cell 
$objExcel->getSheet(1)->getProtection()->setSheet(true);
$objExcel->getSheet(1)->protectCells('A1:C22', 'PHPExcel');
//*************************************
// output 
//
$outputFileName = "output.xls";
// To the file 
////$objWriter->save($outputFileName);
//or
// To the browser 
////header("Content-Type: application/force-download");
////header("Content-Type: application/octet-stream");
////header("Content-Type: application/download");
////header('Content-Disposition:inline;filename="'.$outputFileName.'"');
////header("Content-Transfer-Encoding: binary");
////header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
////header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
////header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
////header("Pragma: no-cache");
////$objWriter->save('php://output');
?>


Related articles: