PHP Export EXCEL Quick Development Guide
phpexcel has a proprietary development document. Please refer to its development document for detailed operations. This document only optimizes and integrates its use. Facilitates rapid development in new projects.
There are also two ways for phpexcel to generate files, one is to output directly, and the other is to generate static files.
Direct output:
The main file is (the file in the same directory of the class directory):
Copy code The code is as follows:
include("./class/class.php"); // Contains the basic header file of class
include(". /class/phpexcel/PHPExcel.php"); // Generate the basic class definition of excel (note the case of the file name)
// If you directly output the excel file, you must include this file
include(". /class/phpexcel/PHPExcel/IOFactory.php");
// Create a phpexcel object, which contains the output content and format
$m_objPHPExcel = new PHPExcel();
// Template file, in order To achieve separation of format and content, the specific content of the output file is implemented in the template file
// The template file operates the object $m_objPHPExcel
include("./include/excel.php");
// Type of output file, excel or pdf
$m_exportType = "excel";
$m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // Output EXCEL file name
$m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // Output PDF file name
// PHPExcel_IOFactory, output excel
//require_once dirname(__FILE__).'/Classes/PHPExcel/IOFactory.php' ;
// If you need to output EXCEL format
if($m_exportType=="excel"){
$objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'Excel5');
// From The browser directly outputs $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 to 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 content (with common operations attached)
Copy code The code is as follows:
global $m_objPHPExcel; // Defined by 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();
// Pass Operate the index to operate the corresponding workbook
// Just set the workbook index to be operated as the current active workbook, such as
// $m_objPHPExcel->setActiveSheetIndex(0);
// Set the first workbook as the active workbook
$m_objPHPExcel->setActiveSheetIndex(0);
// Set the name of the active workbook
// If it is Chinese, be sure to use the iconv function to convert the encoding
$m_objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'Test Workbook'));
//Set the default font and size
$m_objPHPExcel- >getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体'));
$m_objPHPExcel->getDefaultStyle()->getFont( )->setSize(10);
//Set the width of a column
$m_objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
/ / Set the height of a row
$m_objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
// Merge cells
$m_objPHPExcel->getActiveSheet ()->mergeCells('A1:P1');
// Define a style, bold, centered
$styleArray1 = array(
'font' => array(
' bold' => true,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
//Apply style to cell A1
$m_objPHPExcel->getActiveSheet()- >getStyle('A1')->applyFromArray($styleArray1);
//Set cell style (black font)
$m_objPHPExcel->getActiveSheet()->getStyle('H5') ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // Black
// Set cell format (background)
$m_objPHPExcel->getActiveSheet()- >getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // Set the background to light pink
// Set cell format (number format )
$m_objPHPExcel->getActiveSheet()->getStyle('F1')->getNumberFormat()->setFormatCode('0.000');
// Write content to a specific cell
$m_objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
// Set cell style (centered)
$m_objPHPExcel->getActiveSheet()- >getStyle('H5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//Put the picture into the cell and put the data picture in the J1 cell
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath(". ./logo.jpg"); // Image path can only be a relative path
$objDrawing->setWidth(400); // Image width
$objDrawing->setHeight(123); // Picture height
$objDrawing->setCoordinates('J1');//Cell
$objDrawing->setWorksheet($m_objPHPExcel->getActiveSheet());
//Set A5 cell Content and add hyperlink
$m_objPHPExcel->getActiveSheet()->setCellValue('A5', iconv('gbk', 'utf-8', 'Hyperlinkkeiyi.com'));
$m_objPHPExcel->getActiveSheet()->getCell('A5')->getHyperlink()->setUrl('http://www.keiyi.com/');
?>
Generate static files on the server side
Compared with direct generation, the main difference between these two methods is the generation format. The template files are exactly the same. The following is a modified version based on the above example. Look, pay attention to the difference from the above example.
Copy code The code is as follows:
// Include the basic header file of class
include("./class/class.php");
// Generate the basic class definition of excel (note The case of the file name)
include("./class/phpexcel/PHPExcel.php");
// Contains files in Excel5 format. If you need to generate excel2007 files, just include the corresponding Writer
include("./class/phpexcel/PHPExcel/Writer/Excel5.php");
// Contains writing PDF format files
include("./class/phpexcel/PHPExcel/Writer/PDF.php ");
// Create a phpexcel object, which contains the output content and format
$m_objPHPExcel = new PHPExcel();
// Template file, in order to separate the format and content, the output file is specific The content is implemented in the template file
//The template file operates the object $m_objPHPExcel
include("./include/excel.php");
//The type of output file, excel or pdf
$m_exportType = "pdf";
$m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // Output EXCEL file name
$m_strOutputPdfFileName = date('Y-m-j_H_i_s')." .pdf"; // Output PDF file name
// Output file saving path, this path must be writable
$m_strOutputPath = "./output/";
// If you need to output EXCEL format
if($m_exportType=="excel"){
$objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputExcelFileName);
}
/ / If you need to output PDF format
if($m_exportType=="pdf"){
$objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputPdfFileName);
}
?>
http://www.bkjia.com/PHPjc/327391.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327391.htmlTechArticlePHP Export EXCEL Quick Development Guide phpexcel has a proprietary development document. For detailed operations, please refer to its development document, this document It is just optimized and integrated in its use to facilitate its use in new projects...