问题
I have to display an excel workbook in html form because some will view the page with Macbooks and for sure not everybody has excel installed. The workbook has five sheets which have different number of columns and the column widths are different, so they cannot be put in one sheet. I'm using wamp server 2.4 with phpexcel 1.8, php 5.4.16, apache 2.4.4 under windows XP. I can get the different sheets active (show up first) by getActiveSheet(). But when I tried to display them in html, only the first sheet is shown. Seems it can't switch to other sheets. How can I get the html to show other sheets? Here is my code. I have modified it so it writes a workbook with 5 sheets with different background color in cell (A2); different column width in (B) and the actual sheet number in cell (A5). Thanks for your help in advance.
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
#require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
require_once 'c:/wamp/www/Classes/PHPExcel.php';
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$fcolor=array("FF0000","00FF00","0000FF","FFF000","000FFF");
$i=0;
while ($i<=4){
echo 'sheet->'.$i, EOL;
$objPHPExcel->createSheet();
// Add some data
$objPHPExcel->setActiveSheetIndex($i)
->setcellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!')
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'Sheet='.$i);
#$objPHPExcel->getActiveSheet()->getStyle('A2')->getFill()->getStartColor()->setRGB('FF0000');
$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray(
array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => $fcolor[$i]))));
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(5*($i+1));
echo 'Index->'.$objPHPExcel->getActiveSheetIndex(),EOL;
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'html');
$objWriter->save('php://output');
echo '<br/>';
$i++;
}
echo 'Done',EOL;
/**
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="simple.xlsx"');
header('Cache-Control: max-age=0');
ob_end_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
**/
?>
回答1:
Perhaps by using the HTML Writer's writeAllSheets()
method
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('php://output');
Note that the writer name, 'HTML'
should be uppercase, some operating systems are case-sensitive
回答2:
Thanks Mark! You gave me a hint to what I want:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->setSheetIndex($i); //To output a specific sheet
$objWriter->save('php://output');
Thanks!
来源:https://stackoverflow.com/questions/31740801/phpexcel-display-sheet2-in-html