简体   繁体   English

将原始html打印到php中的xls文件

[英]Print raw html to xls file in php

I am trying to create a xls file in php. 我正在尝试在php中创建一个xls文件。 And I am making a string in html format lets say <table><tr><td>Cell 1</td><td>Cell 2</td></tr></table> and want to print in xls file as it is. 我正在以html格式制作一个字符串,假设<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>并希望在xls文件中打印照原样。 but My problem is that when I try to print in xls file its getting render. 但是我的问题是,当我尝试在xls文件中打印时,它会呈现出来。 but I want raw html string without rendered in xls file. 但我想要未在xls文件中呈现的原始html字符串。

Here is my code:- 这是我的代码:

<?php
$file="demo.xls";
$test="<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header('Content-type: application/excel');
header("Content-Disposition: attachment; filename=$file");
echo $test;

?>php

You can also use a service called DocRaptor which does html to excel conversion. 您还可以使用名为DocRaptor的服务,该服务可以将html转换为excel。 http://docraptor.com/ , Multiple language supported http://docraptor.com/ ,支持多种语言

Or You can use the below code to download the file in Excel but first you have to download the PHPExcel class files and include like below . 或者,您可以使用下面的代码在Excel中下载文件,但首先您必须下载PHPExcel类文件,并包括以下内容。

require_once "excel/Classes/PHPExcel.php";

$objTpl = new PHPExcel();

$l=0;
$row_num = 1;
$objTpl->getActiveSheet()->getDefaultStyle()->getFont()->setName('Calibri');
$objTpl->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);

$objTpl->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objTpl->getActiveSheet()->duplicateStyle($objTpl->getActiveSheet()->getStyle('A1'), 'B1:Z1');
$objTpl->getActiveSheet()->getStyle('A1:Z1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);



// you can use loop over here remember to set $row_num = 2 if using loop and increment it inside $row_num++

$objTpl->getActiveSheet()->setCellValueByColumnAndRow($l++,$row_num, 'Cell 1');

$objTpl->getActiveSheet()->setCellValueByColumnAndRow($l++,$row_num, 'Cell 2');

$filename='some_file.xls'; //just some random filename
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); 

$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');      //downloadable file is in Excel 2003 format (.xls)

$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!

exit; //done.. exiting! 

There is a way by either writing html in file or comma separated text 有一种通过在文件中编写html或用逗号分隔的文本的方法

With HTML 使用HTML

$xlsx_file = $pathinfo['dirname']."/".$pathinfo['filename'].".xlsx" ;

$fh = fopen($xlsx_file, 'w+');<br>
fwrite($fh, $table); // your table <br>
fclose($fh);<br>

with array or string of csv 与数组或csv字符串

 $data = '"Name"t"City"t"Country"' . "n";<br>
 $data .= '"Ashok"t"New Delhi"t"India"' . "n";<br>
 $data .= '"Krishna"t"Bangalore"t"India"' . "n";<br>

 $f = fopen('data.xls' , 'wb');<br>
 fwrite($f , $data );<br>
 fclose($f);<br>

this is temporary solution but if you want full set of options you go for PHP Excel as suggested by others 这是暂时的解决方案,但是如果您要全套选项,可以按照他人的建议使用PHP Excel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM