PHPEXCEL实例用法

<?php
set_include_path('/var/www/myproject');
require_once('PHPExcel.php');
require_once('PHPExcel/Writer/Excel2007.php');
$php_excel->getProperties()->setCreator("Scott");
$php_excel->getProperties()->setLastModifiedBy("Scott");
$php_excel->getProperties()->setTitle("My Project");
$php_excel->getProperties()->setSubject("My Project");
$php_excel->getProperties()->setDescription("Fun Project");

$sheet = $php_excel->setActiveSheetIndex(0);
$sheet = $php_excel->getActiveSheet();

$sheet->setCellValue('A1', 'Name');
$sheet->getStyle('A1')->getFont()->setBold(true);
$sheet->setCellValue('B1', 'Price');
$sheet->getStyle('B1')->getFont()->setBold(true);

$count = 2;

foreach($objects as $object) {
  $sheet->setCellValue('A'.$count, $object->getName());
  $cell->setDataType(PHPExcel_Cell_DataType::TYPE_NUMERIC);
  $style=$sheet->getStyle('B'.$count);
  $style->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  $sheet->setCellValue('B'.$count, $object->getPrice());
  ++$count;
}


$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);

$writer = new PHPExcel_Writer_Excel2007($php_excel);
$writer->save("/tmp/excel_file.xlsx");

header("Cache-Control: private");
header("Content-type: application/xlsx");
header("Content-Disposition: attachment; filename=\"general_registration_list.xlsx\"");
header('Cache-Control: maxage=3600'); //Adjust maxage appropriately

header('Content-Length: '.filesize('/tmp/excel_file.xlsx'));
header('Pragma: public');
readfile("/tmp/excel_file.xlsx");
flush();
?>

你可能感兴趣的:(PHP,Excel,职场,休闲)