PHPExcel-在输出上呈现HTML标记 - javascript

在我的网站上,我有几种使用CKEditor的表格来存储其数据。因此,当我导出所有内容时,在excel中,我可以在整个文档中看到标记。是否有一种简单的方法可以将它们转换为\ n或以某种方式使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的字符串,而不是被视为包含ABC和。如果要将其视为两行,则需要将DEF更改为<br />。您还需要设置要对齐的单元格对齐方式。

粗糙的Unicode->没有CLDR的语言代码? - javascript

我在写字典应用。如果用户键入Unicode字符,我想检查该字符是哪种语言。例如字 - returns ['zh', 'ja', 'ko'] العربية - returns ['ar'] a - returns ['en', 'fr', …

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…

如何在HTML选项中添加onClick? - javascript

我要尝试的是单击<option>时,该值将显示在<input type='text'>中现在是我的代码:<select id='months'> <option value='9' id='months9'>9 Months<…

提交初始化后删除某些帖子数据 - javascript

在初始化提交之后但在将数据发送到处理页面之前,是否可以过滤$ _POST表单数据?我想象过程的方式:提交->收集$ _POST数据->发送数据我想做的事:提交->收集$ _POST数据->删除某些元素->发送数据这样就不必更改处理页面以过滤掉不希望接收的元素了吗? javascript大神给出的解决方案 当然可以,您可以在JS …

如果我得到url(''),我该如何使用另一个URL - javascript

我是新手,正在写这篇文章,但是如果源上没有图像,那么我只有空白。有人可以告诉我,如果我正在获取背景图像,如何获取/images/no-image.jpg:url();这是我的代码:<div class="uk-clearfix uk-position-relative"> <div class="recipeb…