Blogger Information
Blog 263
fans 3
comment 2
visits 113338
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
生成几种文件的方法
福哥的博客
Original
1182 people have browsed it

生成 word 文件 :

<?php
header("Content-type:text/html;charset=utf-8");

function MakeWord($data,$fileName=''){
    if(empty($data)) return '';
    $data='<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">'.$data.'</html>';
    if(empty($fileName)) $fileName=date('YmdHis').'.doc';
    $fp=fopen($fileName,'wb');
    fwrite($fp,$data);
    fclose($fp);
}


$str='中国 world !';
//$str = utf8_encode($str);
//$str = iconv("UTF-8","GB2312//IGNORE",$str) ;//iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。
$str = iconv("UTF-8","GBK",$str);////iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。

MakeWord($str);

?>

生成 html 文件 :

<?php
header("Content-type:text/html;charset=utf-8");
$con=array (array('Aaron','15900000000'),array('Ryan','15942806312'));
foreach ($con as $id=>$val){
    $title = $val[0];
    $content = $val[1];
    $path = $id.'.htm';
    $fp = fopen("tmp.htm","r");//只读打开模板
    $str=fread($fp,filesize("tmp.htm"));//读取摸板中内容
    $str=str_replace("{title}",$title,$str);
    $str=str_replace("{content}",$content,$str);//替换内容
    $str = iconv("UTF-8","GB2312//IGNORE",$str);
    fclose($fp);
    
    $handle=fopen($path,'w');//写入方式打开新闻路径
    fwrite($handle,$str);//把刚才替换的内容写入生成的HTML文件
    fclose($handle);
    echo "生成成功<br />";
}
//unlink($path);//删除文件
?>

生成 html 的 demo 文件 :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>标题:{title}</title>
</head>
<body>
此新闻的内容:{content}
</body>
</html>

生成 excel 文件:

  1. ***PHPExcel包,

  2. PHPExcel.php  PHPExcel文件

  3. create.php  实现代码

<?php
include 'PHPExcel.php';
//require_once('pc_fns.php');
header("Content-Type: text/html;charset=utf-8");
//创建对象
$excel = new PHPExcel();
//Excel表格式
$letter = array('A','B','C','D','E');
//表头数组
$tableheader = array('用户','性别','年龄','爱好','工作');
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
session_start();
//表格数组
$data = unserialize($_SESSION['sdata']);//反序列化
//填充表格信息
for ($i = 2;$i <= count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i",str_replace('&nbsp;','',strip_tags("$value")));
$j++;
}
}
//ob_end_clean();
//创建Excel输入对象
$write = new PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="pls_change_name.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post