2014-10-06 104 views
0

在我的网站中,我有几种使用CKEditor存储数据的表单。因此,当我导出所有内容时,在Excel中,我可以在我的文档中看到
标记。有没有简单的方法将它们转换为\ n或者让PHPExcel将它们识别为中断?PHPExcel - 在输出上渲染HTML标记

我也尝试将导出类型更改为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; 
?> 
+0

MS Excel在单元格中没有HTML标记的概念,它只是一个字符串值 – 2014-10-06 14:44:38

+0

'str_replace('
',“\ n”,$ str)'? – 2014-10-06 14:48:57

+0

@MarcB或'preg_replace('/ \ /i',“\ n”,$ str)' – sjagr 2014-10-06 14:50:35

回答

1

MS Excel有一个细胞内没有HTML标记的概念,它是一个简单的字符串值....存储作为含有ABC两行像ABC<br />DEF的值将简单地作为含有ABC<br />DEF而不是字符串被处理和分别为DEF。如果您希望将它看作两行,则需要将<br />更改为"\n"。您还需要设置单元格对齐以进行换行。

+0

+1我建议使用['strip_tags' ](http://php.net/manual/en/function.strip-tags.php)后,您的建议,摆脱任何HTML,Excel不能有意义地渲染 – sjagr 2014-10-06 14:49:14

+0

我试过以下,它取代
与\ n正确,但Excel不会将其识别为断线。它只是添加文本\ n $ data = str_replace(“
”,'\ n',$ data); – 2014-10-06 15:00:46

+0

PHP 101 - ''\ n''!=='“\ n”'....一个是第一个是字母'\'后跟字母'n';第二个是换行符。单引号和双引号会产生很大的差异:[PHP文档](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single) – 2014-10-06 15:02:55