我必须以html格式显示excel工作簿,因为有些人会使用Macbook查看页面,并且肯定不是每个人都安装了excel。工作簿有五个页面,列数不同,列宽不同,因此不能放在一个工作表中。我在Windows XP下使用wamp服务器2.4和phpexcel 1.8,php 5.4.16,apache 2.4.4。我可以通过getActiveSheet()获取不同的工作表(首先显示)。但是当我尝试在html中显示它们时,只显示第一张纸。似乎它无法切换到其他工作表。如何让html显示其他表格?这是我的代码。我已对其进行了修改,因此它会在单元格(A2)中编写一张带有5张不同背景颜色的工作簿; (B)中的不同列宽和单元(A5)中的实际页数。感谢您的帮助。
<?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;
**/
?>
答案 0 :(得分:1)
也许使用HTML Writer的writeAllSheets()
方法
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('php://output');
请注意,编写者名称'HTML'
应为大写,某些操作系统区分大小写
答案 1 :(得分:0)
谢谢马克!你给了我一个暗示我想要的东西:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->setSheetIndex($i); //To output a specific sheet
$objWriter->save('php://output');
谢谢!