Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the phpExcel function

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use phpExcel function". In daily operation, I believe many people have doubts about how to use phpExcel function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use phpExcel function". Next, please follow the editor to study!

Basic operation of PHPExcel:

Define EXCEL entities

That is, define a PHPEXCEL object and set the display content within the EXCEL object

/ / Excel starts / / prepares the EXCEL including files / / Error reporting error_reporting (0); / / PHPExcel require_once dirname (_ _ FILE__). 'PHPExcel.php';// generates a new excel object $objPHPExcel = new PHPExcel () / / set the properties of the excel document $objPHPExcel- > getProperties ()-> setCreator ("Sam.c")-> setLastModifiedBy ("Sam.c Test")-> setTitle ("Microsoft Office Excel Document")-> setSubject ("Test")-> setDescription ("Test")-> setKeywords ("Test")-> setCategory ("Test result file") / / start to operate the excel table / / operate the first worksheet $objPHPExcel- > setActiveSheetIndex (0); / / set the workbook name $objPHPExcel- > getActiveSheet ()-> setTitle (iconv ('gbk',' utf-8', 'phpexcel Test'); / / set the default font and size $objPHPExcel- > getDefaultStyle ()-> getFont ()-> setName (iconv ('gbk',' utf-8', 'Arial)); $objPHPExcel- > getDefaultStyle ()-> getFont ()-> setSize (10)

III. Output file

/ / if you need to output EXCEL format if ($objPHPExcel exportTypeType = "excel") {$objWriter = PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel5'); / / output $filename header directly from the browser ("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=". $filename); header ("Content-Transfer-Encoding:binary"); $objWriter- > save ("php://output") } / / if you need to output PDF format if ($objPHPExcel exportTypeType = "pdf") {$objWriter = PHPExcel_IOFactory::createWriter ($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");}

Set the width of a column:

$objPHPExcel- > getActiveSheet ()-> getColumnDimension ('A')-> setWidth (15)

Set the height of the row:

$objPHPExcel- > getActiveSheet ()-> getRowDimension ('6')-> setRowHeight (30)

Merge cells:

$objPHPExcel- > getActiveSheet ()-> mergeCells ('A1VR P1')

Set A1 cells to be bold and centered:

$styleArray1 = array ('font' = > array (' bold' = > true, 'size'= > 12,' color'= > array ('argb' = >' 000000000,),), 'alignment' = > array (' horizontal' = > PHPExcel_Style_Alignment::HORIZONTAL_CENTER,),); / / set A1 cell to bold, centered $objPHPExcel- > getActiveSheet ()-> getStyle ('A1')-> applyFromArray ($styleArray1) $objPHPExcel- > getActiveSheet ()-> getStyle ('B1')-> getFont ()-> setBold (true)

Write to a specific cell:

$objPHPExcel- > getActiveSheet ()-> setCellValue ('A1Qing,' Hello Baby')

Set the cell style (horizontal / vertical center):

$objPHPExcel- > getActiveSheet ()-> getStyle ('A1')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel- > getActiveSheet ()-> getStyle (' A1')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment::VERTICAL_CENTER)

Set the cell style (black font):

$objPHPExcel- > getActiveSheet ()-> getStyle ('H5')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color::COLOR_BLACK); / / Black

Format the cell (background):

$objPHPExcel- > getActiveSheet ()-> getStyle ('H5')-> getFill ()-> getStartColor ()-> setARGB (' 00ff99cc'); / / set the background to light pink

Format the cell (number format):

$objPHPExcel- > getActiveSheet ()-> getStyle ('F'.$iLineNumber)-> getNumberFormat ()-> setFormatCode (' 0.000')

Put a picture in the cell:

/ put the data center picture in the J1 cell $objDrawing = new PHPExcel_Worksheet_Drawing (); $objDrawing- > setName ('Logo'); $objDrawing- > setDescription (' Logo'); $objDrawing- > setPath ('test.jpg'); $objDrawing- > setWidth (400); $objDrawing- > setHeight (123); $objDrawing- > setCoordinates (' J1'); $objDrawing- > setWorksheet ($objPHPExcel- > getActiveSheet ())

Set the hyperlink in the cell:

$objPHPExcel- > getActiveSheet ()-> setCellValue ('H8', iconv ('gbk',' utf-8', 'Yan Nantian'); $objPHPExcel- > getActiveSheet ()-> getCell ('H8')-> getHyperlink ()-> setUrl (' https://www.jb51.net/');')

Set the cell border

$styleThinBlackBorderOutline = array ('borders' = > array (' outline' = > array ('style' = > PHPExcel_Style_Border::BORDER_THIN, / / set border style / /' style' = > PHPExcel_Style_Border::BORDER_THICK, another style 'color' = > array (' argb' = > 'FF000000'), / / set border color),) $objPHPExcel- > getActiveSheet ()-> getStyle ('A4getStyle E10')-> applyFromArray ($styleThinBlackBorderOutline); / / add a new worksheet $objExcel- > createSheet (); $objActSheet = $objExcel- > getSheet ($s); $objActSheet- > setTitle (Table. $GSheet). At this point, the study on "how to use the phpExcel function" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report