I save the table in xlsx format

$html = $_POST['html']; file_put_contents($path."/table.html", $html); $objReader = new PHPExcel_Reader_HTML; $objPHPExcel = $objReader->load($path."/table.html"); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($path."/table.xlsx"); 

The file is created, but instead of the Russian letters kryakozyabry. The html code from the browser page (utf-8) is transferred to $ html. Help defeat the encoding.

With the encoding issue was resolved.

The solution was as always simple. I tried to convert html to Excel. But this html was only the code table

 <table><tr><td></td></tr></table> 

but you must

 <html><head><meta charset="UTF-8"></head><body><table> ..................... 

How to save table styles now?

 <td style='background-color:#F8F8F8; border:1px solid #000; width: 250px'> 

Does not help

  • Try to select in Ixelle to change the encoding to save the file and reopen again. - Naumov
  • Will not work. The file is generated by the script and immediately sent to the manager by e-mail. - thecoder

3 answers 3

The solution was as always simple. I tried to convert html to Excel. But this html was only the code table

 <table><tr><td></td></tr></table> 

but you must

 <html><head><meta charset="UTF-8"></head><body><table> ..................... 
  • Beautifully turned out probably. The PhpExcel library is actually very helpful. - fonjeekay
  • But not very. Table styles are not saved: (I don’t know how to win. - thecoder
  • styles should be in <head> as I understand it, between <style></style> - fonjeekay
  • or try to enter the style="" attribute style="" in the tags where the styles are needed: <td style="color:grey"></td> pens, even if the version does not work, try another PHPEXcel? - fonjeekay
  • @fonjeekay Meaning another PHPExcel? - thecoder

It seems to me that the matter is in file_put_contents

 $html = $_POST['html']; file_put_contents($path."/table.html", mb_convert_encoding($html, 'UTF-8', 'OLD-ENCODING')); $objReader = new PHPExcel_Reader_HTML; $objPHPExcel = $objReader->load($path."/table.html"); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($path."/table.xlsx"); 

How to write file in UTF-8 format

  • So I think that the problem is in file_put_contents. But your example didn't work - thecoder
  • But most likely this is not the case. I wrote down the file in UTF-8 and windows-1251, and the file opens with cracks. I think that the library itself crookedly translates Html to xlsx. - thecoder
  • I have been working with her for a year and were also funny with the coding, - fonjeekay

Try using the function → iconv() .

 $html = $_POST['html']; $html = iconv("cp1251", "UTF-8", $html); file_put_contents($path."/table.html", $html); 
  • From where kp1251? For this and the minus itself - Naumov
  • iconv doesn't help - thecoder