当前位置:网站首页>Phpexcel export with picture Excel

Phpexcel export with picture Excel

2022-06-23 02:13:00 It workers

<?php
//  Used here PHPExcel The version number is 1.8.0
//  Download address https://github.com/PHPOffice/PHPExcel  download ZIP Compressed package 
//  After downloading Classes Folders are extracted for subsequent use 


/*
 The directory structure of the original plug-in 
 The root directory Classes
            --PHPExcel Folder 
            --PHPExcel.php file 
*/
/** Include PHPExcel */
require_once '/Classes/PHPExcel.php';
/*
 stay thinkPHP in   take Classes Rename the folder to PHPExcel, Put it in the catalog  ThinkPHP/Library/Vendor Next 
 Store in thinkPHP Middle and rear directory structure 
 The root directory ThinkPHP
            --Library
                    --Vendor
                            --PHPExcel
                                    --PHPExcel Folder 
                                    --PHPExcel.php file 
*/

//  stay thinkPHP Contained in the PHPExcel.php File mode 
// vendor('PHPExcel.PHPExcel');
//  And in thinkPHP Add a backslash before all the class names of the calling plug-ins in  \


// Create new PHPExcel object
$objPHPExcel = new \PHPExcel ();

// Set document properties
$objPHPExcel->getProperties ()->setCreator ( "zend" )-> //  author 
setLastModifiedBy ( "zend" )-> //  Last saved by 
setTitle ( "Office 2007 XLSX Document" )-> //  title 
setSubject ( "Office 2007 XLSX Document" )-> //  The theme 
setDescription ( "document for Office 2007 XLSX, generated using PHP classes." )-> //  remarks 
setKeywords ( "office 2007 openxml php" )-> //  Mark 
setCategory ( "result file" ); //  Category 

$objPHPExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( '15' ); //  Set column width 
$objPHPExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( '30' );
$objPHPExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( '30' );
$objPHPExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( '20' );

// Add some data
$objPHPExcel->setActiveSheetIndex ( 0 )
->setCellValue ( 'A1', ' Phone number ' ) //  first line A The column value 
->setCellValue ( 'B1', ' full name ' ) //  first line B The column value 
->setCellValue ( 'C1', ' picture ' ) //  first line C The column value 
->setCellValue ( 'D1', ' Submission time ' ); //  first line D The column value 







//  From this point on, the number of rows can be accumulated in the loop 
$objPHPExcel->setActiveSheetIndex ( 0 )
->setCellValue ( 'A' . '2', ' ' . '12345678910' ) //  The second line A The column value 
->setCellValue ( 'B' . '2', ' ' . ' user name ' ); //  The second line B The column value 

/*  Instantiate the insert picture class  */
$objDrawing = new \PHPExcel_Worksheet_Drawing ();
/*  Set the image path   Bear in mind : Only local pictures  */
$objDrawing->setPath ( './img/test.jpg' );
/*  Set the image height  */
$objDrawing->setHeight ( 100 );
/*  Set the position of the cell where the picture is to be inserted  */
$objDrawing->setCoordinates ( 'C' . '2' ); //  The second line here C Column 
//  Write the... Of the picture in the specified grid X Coordinate value 
$objDrawing->setOffsetX ( 20 );
//  Write the... Of the picture in the specified grid Y Coordinate value 
$objDrawing->setOffsetY ( 20 );
//  Set the rotation angle 
// $objDrawing->setRotation(20);
$objDrawing->getShadow ()->setVisible ( true );
$objDrawing->getShadow ()->setDirection ( 50 );
$objDrawing->setWorksheet ( $objPHPExcel->getActiveSheet () );

//  Set cell height 
$objPHPExcel->getActiveSheet ()->getRowDimension ( $key + 2 )->setRowHeight ( 100 );

$objPHPExcel->setActiveSheetIndex ( 0 )->setCellValue ( 'D' . '2', ' ' . '2017-08-24 16:01' ); //  The second line D The column value 
//  If adding rows circularly , Then the loop ends here 








// Rename worksheet
$objPHPExcel->getActiveSheet ()->setTitle ( 'Simple' );

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex ( 0 );

//  Redirect output to the client Web browser  (Excel2007)
header ( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
header ( 'Content-Disposition: attachment;filename=" Test filename .xlsx"' );
header ( 'Cache-Control: max-age=0' );
//  If it is IE9 browser , You need the following 
header ( 'Cache-Control: max-age=1' );

//  If it's a belt SSL Of IE, You may use the following 
header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); //  Past date 
header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' ); //  Real time 
header ( 'Cache-Control: cache, must-revalidate' ); // HTTP/1.1
header ( 'Pragma: public' ); // HTTP/1.0

$objWriter = \PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
$objWriter->save ( 'php://output' );
原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202091754070002.html