PHP导出word文件,简单拓展可导出其他文本类文件

PHP导出word文件,简单拓展可导出其他文本类文件

/**
 * PHP 导出简单文本内容(word txt等)
 * @param $content mixed 导出内容 (文本string  /  html代码)
 * @param $filename string 需保存文件名
 * @param string $extension 文件类型 (doc docx txt xml)
 */
function export_html_to_word($content, $filename, $extension = 'doc')
{
  ob_start();

  $_export_content = '';
  if ($extension == 'doc' || $extension == 'docx') {
    $_export_content .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">';
  }

  $_export_content .= $content;

  if ($extension == 'doc' || $extension == 'docx') $_export_content .= '</html>';

  echo $_export_content;

  ob_get_contents();

  if ($extension == 'doc' || $extension == 'docx') header('Content-type:application/word');
  header('Content-Disposition: attachment; filename=' . $filename . '.' . $extension);

  ob_flush();
  flush();
}

$html = '<b style="color: red">你看我哪里像好人</b>';

$wordname = 'test-file';

export_html_to_word($html, $wordname);

猜你喜欢

转载自blog.csdn.net/weixin_43144260/article/details/83185365
今日推荐