1

我有 2 个 xls,我想将此信息绘制到一个 HTML 页面中。

注意:对于假设我提到的 xls。实际的 xls 位置已经在数据库表中。我只会渲染这些位置并绘制到 HTML 页面中。

include_once("Classes/PHPExcel/IOFactory.php");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

//first excel file


$objPHPExcel->getActiveSheet()
           ->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
           ->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
           ->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
           ->setCellValue('c8','40');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');

//second excel file

$objPHPExcel->getActiveSheet()
           ->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
           ->setCellValue('c6','60');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');        

$objWriter->save('combinedexcelpage.html');
4

1 回答 1

1

我不会详细解释为什么这不起作用,因为解释起来需要很长时间;但是您可以采取几种解决方案来实现您想要的:

选项1

Excel 工作簿包含一个或多个工作表,因此您可以将每个“文件”创建为单独的工作表,而不是单独的文件。

//  Create new PHPExcel object
$objPHPExcel = new PHPExcel();

//  First excel worksheet, (created when you instantiate a new PHPExcel object)
$objPHPExcel->getActiveSheet()
    ->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
    ->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
    ->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
    ->setCellValue('c8','40');

//  Second excel worksheet
//  Add new sheet, which should also set it as the new "active" sheet
$objPHPExcel->createSheet()

$objPHPExcel->getActiveSheet()
    ->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
    ->setCellValue('c6','60');

默认情况下,HTML Writer 只会编写一个工作表(第一个),但您可以将其设置为编写所有工作表:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML'); 
$objWriter->writeAllSheets();
$objWriter->save('combinedexcelpage.html');

选项 #2

HTML Writer save() 方法将始终生成文件流(无论是文件系统文件还是 php://output),但您可以调用类中的各个方法来生成包含格式化工作表的字符串,而不是使用 save()数据,并从这些“块”构建您自己的输出。

$data = '';

//  First excel file
$objPHPExcel1 = new PHPExcel();

$objPHPExcel1->getActiveSheet()
    ->setCellValue('c5','10');
$objPHPExcel1->getActiveSheet()
    ->setCellValue('c6','20');
$objPHPExcel1->getActiveSheet()
    ->setCellValue('c7','30');
$objPHPExcel1->getActiveSheet()
    ->setCellValue('c8','40');

$objWriter1 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter1->generateStyles(false);
$data .= $objWriter1->generateHTMLHeader();
$data .= $objWriter1->generateSheetData();

//  Second excel file
$objPHPExcel2 = new PHPExcel();

$objPHPExcel2->getActiveSheet()
    ->setCellValue('c5','50');
$objPHPExcel2->getActiveSheet()
    ->setCellValue('c6','60');

$objWriter2 = PHPExcel_IOFactory::createWriter($objPHPExcel2, 'HTML');        
$objWriter2->generateStyles(false);
$data .= $objWriter2->generateSheetData();
$data .= $objWriter2->generateHTMLFooter();

file_put_contents('combinedexcelpage.html', $data);

这两个选项都在开发人员文档的第 6.8 节中进行了描述

于 2013-07-13T09:39:47.987 回答