PHPExcel - 在输出时呈现 HTML 标记


PHPExcel - Render HTML markups on output

在我的网站上,我有几个使用CKEditor的表单来存储它的数据。因此,当我导出所有内容时,在 excel 中,我可以在整个文档中看到
标记。有没有一种简单的方法将它们转换为'或以某种方式让PHPExcel将它们识别为中断?

我还尝试将导出类型更改为 Excel2007,认为也许 Excel 可以简单地呈现 HTML 标记,但是当我切换类型时,我的报表就会崩溃。所以我坚持使用Excel5。

任何帮助将不胜感激。

<?php
/** PHPExcel */  
require_once '/Excelphp/Classes/PHPExcel.php';  
// Create new PHPExcel object  
$objPHPExcel = new PHPExcel();  
$rows=2;
$sheet=$objPHPExcel->getActiveSheet(); 
// Set 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");  
//This is the hard coded *non dynamic* cell formatting
    $sheet->getColumnDimension('A')->setWidth(5);  
    $sheet->getColumnDimension('B')->setWidth(15);  
    $sheet->getColumnDimension('C')->setWidth(50);  
    $sheet->getColumnDimension('D')->setWidth(50);  
    $sheet->getSheetView()->setZoomScale(90);
    $sheet->getStyle('A:D') ->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//Font Setting for the Support group title.
$Support_team = array('font'=> array('bold'=> true,'color' => array('rgb' => '4D4D4D'),'size'  => 22,'name'  => 'Arial'),'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER),);
//Font settings for the header cells only.
$headers = array('font'=> array('bold'=> true,'color' => array('rgb' => '4D4D4D'),'size'  => 12,'name'  => 'Arial'),'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER),);
//Border settings
$borders = array('borders' => array('inside'=> array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('argb' => '717171')),'outline' => array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('argb' => '717171'))));
//  SQl database connections
    $db = mysql_connect("localhost", "IMC_COE2", "IMC123");  
    mysql_select_db("IMC_COE2",$db);  
    $sql="select client, team_name,support_team_prime,prime_comments,support_team_backup,backup_comments,escalation1,escalation1_comments,escalation2,escalation2_comments,escalation3,escalation3_comments,escalation4,escalation4_comments,note from tbl_address ORDER BY team_name";  
    $result=mysql_query($sql);  
        $numrows=mysql_num_rows($result);  
        if ($numrows>0)  
        {  
            while($data=mysql_fetch_array($result))  
            {  
//Cell Merging 
$sheet
    ->mergeCells('B'.$rows.':D'.$rows)  
    ->mergeCells('B'.($rows+2).':D'.($rows+2))  
    ->mergeCells('B'.($rows+5).':D'.($rows+5))  
    ->mergeCells('B'.($rows+10).':D'.($rows+10))  
    ->mergeCells('C'.($rows+1).':D'.($rows+1))
    ->mergeCells('B'.($rows+11).':D'.($rows+11)); 
// Add some data  
$objPHPExcel->setActiveSheetIndex(0)  
    ->setCellValue('B'.($rows+1), 'Client:')
    ->setCellValue('B'.($rows+2), 'Support group contacts')     
    ->setCellValue('B'.($rows+3), 'Prime:')
    ->setCellValue('B'.($rows+4), 'Backup:')
    ->setCellValue('B'.($rows+5), 'Escalations') 
    ->setCellValue('B'.($rows+6), 'Escalation 1:') 
    ->setCellValue('B'.($rows+7), 'Escalation 2:') 
    ->setCellValue('B'.($rows+8), 'Escalation 3:') 
    ->setCellValue('B'.($rows+9), 'Escalation 4:') 
    ->setCellValue('B'.($rows+10), 'Notes'); 
//Format the hardcoded text 
$sheet->getStyle('B'.$rows)->applyFromArray($Support_team);
$sheet->getStyle('B'.($rows+2))->applyFromArray($headers);      
$sheet->getStyle('B'.($rows+5))->applyFromArray($headers);  
$sheet->getStyle('B'.($rows+10))->applyFromArray($headers); 
//Row height adjustments
$sheet->getRowDimension($rows+3)->setRowHeight(60);
$sheet->getRowDimension($rows+4)->setRowHeight(60);
$sheet->getRowDimension($rows+6)->setRowHeight(60);
$sheet->getRowDimension($rows+7)->setRowHeight(60);
$sheet->getRowDimension($rows+8)->setRowHeight(60);
$sheet->getRowDimension($rows+9)->setRowHeight(60);
$sheet->getRowDimension($rows+11)->setRowHeight(100);   

//Cell Wraptext
$sheet->getStyle('C'.($rows+1).':D'.($rows+1))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+3).':D'.($rows+3))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+4).':D'.($rows+4))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+6).':D'.($rows+6))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+7).':D'.($rows+7))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+8).':D'.($rows+8))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+9).':D'.($rows+9))->getAlignment()->setWrapText(true);
$sheet->getStyle('B'.($rows+11).':D'.($rows+11))->getAlignment()->setWrapText(true);
//Background color on cells 
$sheet->getStyle('B'.$rows.':D'.$rows)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+2).':D'.($rows+2))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+5).':D'.($rows+5))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+10).':D'.($rows+10))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+1))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+3))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+4))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+6))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+7))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+8))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+9))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');         
//Border range  
$sheet->getStyle('B'.$rows.':D'.($rows+11))->applyFromArray($borders);          
//This section is the actual data imported from the SQL database *don't touch*
$objPHPExcel->setActiveSheetIndex(0)              
    ->setCellValue('C'.($rows+1), utf8_encode($data['client'])) //this will give cell C2. 
    ->setCellValue('B'.$rows, utf8_encode($data['team_name'])) // this will give cell B2 
    ->setCellValue('C'.($rows+3), utf8_encode($data['support_team_prime'])) //this will give C5 
    ->setCellValue('D'.($rows+3), utf8_encode($data['prime_comments']))  // This will give D5
    ->setCellValue('C'.($rows+4), utf8_encode($data['support_team_backup']))  //This will give C6
    ->setCellValue('D'.($rows+4), utf8_encode($data['backup_comments'])) //This will give D6  
    ->setCellValue('C'.($rows+6), utf8_encode($data['escalation1']))//THis will give you C8
    ->setCellValue('D'.($rows+6), utf8_encode($data['escalation1_comments']))//This will give you D8
    ->setCellValue('C'.($rows+7), utf8_encode($data['escalation2']))//This will give you C9
    ->setCellValue('D'.($rows+7), utf8_encode($data['escalation2_comments']))//This will give you D9
    ->setCellValue('C'.($rows+8), utf8_encode($data['escalation3']))//This will give you C10
    ->setCellValue('D'.($rows+8), utf8_encode($data['escalation3_comments']))//This will give you D10
    ->setCellValue('C'.($rows+9), utf8_encode($data['escalation4']))//This will give you C11
    ->setCellValue('D'.($rows+9), utf8_encode($data['escalation4_comments']))//This will give you D11
    ->setCellValue('B'.($rows+11), utf8_encode($data['note'])); //This will give you B13
$rows+=14; 
            }  
        }          
// Rename sheet  
$sheet->setTitle('Directory Tool Full dump');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5) 
ob_end_clean(); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
header('Content-Type: application/vnd.ms-excel');
$today=date("F.d.Y");
$filename = "Directory_Export-$today.xls";
header("Content-Disposition: attachment; filename=$filename");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$objWriter->save('php://output');  
exit;  
?>

MS Excel在单元格中没有HTML标记的概念,它只是一个字符串值。存储像 ABC<br />DEF 这样的值将简单地被视为包含 ABC<br />DEF 的字符串,而不是分别包含 ABCDEF 的两行。如果您希望将其视为两行,则需要将<br />更改为 "'n" 。您还需要将单元格对齐方式设置为换行。