Home > Backend Development > PHP Tutorial > phpExcel Chinese Help Manual Common Function Guide, _PHP Tutorial

phpExcel Chinese Help Manual Common Function Guide, _PHP Tutorial

WBOY
Release: 2016-07-13 10:20:44
Original
822 people have browsed it

phpExcel Chinese Help Manual Common Function Guide,

Basic operations of PHPExcel:
Define EXCEL entities
That is, define a PHPEXCEL object and set the display content in the EXCEL object

// Excel开始
// 准备EXCEL的包括文件
// Error reporting 
error_reporting(0);
// PHPExcel 
require_once dirname(__FILE__) . 'PHPExcel.php';
// 生成新的excel对象
$objPHPExcel = new PHPExcel();
// 设置excel文档的属性
$objPHPExcel->getProperties()->setCreator("Sam.c")
             ->setLastModifiedBy("Sam.c Test")
             ->setTitle("Microsoft Office Excel Document")
             ->setSubject("Test")
             ->setDescription("Test")
             ->setKeywords("Test")
             ->setCategory("Test result file");
// 开始操作excel表
// 操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
// 设置工作薄名称
$objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'phpexcel测试'));
// 设置默认字体和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体'));
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);

Copy after login

3. Output file

// 如果需要输出EXCEL格式
if($m_exportType=="excel"){   
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    // 从浏览器直接输出$filename
    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=".$filename);
    header("Content-Transfer-Encoding:binary");
    $objWriter->save("php://output"); 
}
// 如果需要输出PDF格式
if($m_exportType=="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"); 
}

Copy after login

Set the width of a column:

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
Copy after login

Set the height of a row:

$objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
Copy after login

Merge cells:

$objPHPExcel->getActiveSheet()->mergeCells('A1:P1');
Copy after login

Set cell A1 to be bold and centered:

$styleArray1 = array(
  'font' => array(
    'bold' => true,
    'size'=>12,
    'color'=>array(
      'argb' => '00000000',
    ),
  ),
  'alignment' => array(
    'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  ),
);
// 将A1单元格设置为加粗,居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);

$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);

Copy after login

Write content into a specific cell:

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
Copy after login

Set cell style (centered horizontally/vertically):

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

Set cell style (black font):

$objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
Copy after login

Set cell format (background):

$objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 将背景设置为浅粉色
Copy after login

Set cell format (number format):

$objPHPExcel->getActiveSheet()->getStyle('F'.$iLineNumber)->getNumberFormat()->setFormatCode('0.000');
Copy after login

Put a picture into the cell:

// 将数据中心图片放在J1单元格内
$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());
Copy after login


Set a hyperlink in a cell:

$objPHPExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天'));
$objPHPExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('http://www.bkjia.com/');
Copy after login

Set cell borders

$styleThinBlackBorderOutline = array(
    'borders' => array (
       'outline' => array (
          'style' => PHPExcel_Style_Border::BORDER_THIN,  //设置border样式
          //'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式
          'color' => array ('argb' => 'FF000000'),     //设置border颜色
      ),
   ),
);
$objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline);

//添加一个新的worksheet 
          $objExcel->createSheet(); 
          $objActSheet = $objExcel->getSheet($s); 
          $objActSheet->setTitle('表'.$GSheet);
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/865613.htmlTechArticlephpExcel Chinese Help Manual Common Function Guide, PHPExcel Basic Operation: Defining an EXCEL entity means defining a PHPEXCEL object and setting Display content in EXCEL object // Excel starts // Prepare...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template