Home  >  Article  >  Backend Development  >  PHP export to Excel example explanation, export excel example explanation_PHP tutorial

PHP export to Excel example explanation, export excel example explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:07897browse

Explanation of PHP export to Excel examples, explanation of export to excel examples

This time to implement PHP export of Excel files, the PHP open source program PHPExcel is used. Some key codes are shared with you. The specific content is as follows

<&#63;php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
require_once './Classes/PHPExcel.php';
$data=array(
  0=>array(
    'id'=>1001,

    'username'=>'张飞',
    'password'=>'123456',
    'address'=>'三国时高老庄250巷101室'
  ),
  1=>array(
    'id'=>1002,
    'username'=>'关羽',
    'password'=>'123456',
    'address'=>'三国时花果山'
  ),
  2=>array(
    'id'=>1003,
    'username'=>'曹操',
    'password'=>'123456',
    'address'=>'延安西路2055弄3号'
  ),
  3=>array(
    'id'=>1004,
    'username'=>'刘备',
    'password'=>'654321',
    'address'=>'愚园路188号3309室'
  )
);
$objPHPExcel=new PHPExcel();
$objPHPExcel->getProperties()->setCreator('http://www.bkjia.com')
               ->setLastModifiedBy('http://www.bkjia.com')
               ->setTitle('Office 2007 XLSX Document')
               ->setSubject('Office 2007 XLSX Document')
               ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
               ->setKeywords('office 2007 openxml php')

               ->setCategory('Result file');

$objPHPExcel->setActiveSheetIndex(0)

      ->setCellValue('A1','ID')

      ->setCellValue('B1','用户名')

      ->setCellValue('C1','密码')

      ->setCellValue('D1','地址');

$i=2;     

foreach($data as $k=>$v){

  $objPHPExcel->setActiveSheetIndex(0)
      ->setCellValue('A'.$i,$v['id'])
      ->setCellValue('B'.$i,$v['username'])
      ->setCellValue('C'.$i,$v['password'])
      ->setCellValue('D'.$i,$v['address']);

  $i++;

}
$objPHPExcel->getActiveSheet()->setTitle('三年级2班');

$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('学生信息统计表').'_'.date('Y-m-dHis');
 
//生成xlsx文件
/*
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
*/

//生成xls文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

Note: If garbled characters appear when exporting Chinese, you can try to convert the string into gb2312. The code is similar to the following:

View code print

$str=mb_convert_encoding("gb2312","UTF-8",$str);

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Articles you may be interested in:

  • How to export database to excel with php
  • PHP export MySQL data to Excel file (fputcsv)
  • phpExcel Solution to memory overflow error when exporting large amounts of data
  • A little experience in exporting Excel based on PHP to perfectly solve the garbled problem
  • Detailed explanation of exporting to Excel or CSV based on PHP (with utf8, gbk encoding conversion)
  • php import and export excel examples
  • Use phpExcel to import and export Excel data (detailed analysis of all steps)
  • Use phpexcel to import excel into the database and database export excel
  • ThinkPHP uses PHPExcel to implement a complete example of Excel data import and export

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1095277.htmlTechArticleExplanation of PHP export to Excel examples, explanation of export excel examples This time to implement PHP export of Excel files, the PHP open source program PHPExcel is used , share some key codes with everyone, the specific contents are as follows ph...
Statement:
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