PHP操作:将数据库中的数据保存到Word、Excel中。
2024-10-19 11:40:40
1.首先要把word、excel表放到文件的根目录下
2.定义了一个word类
<?php
class word
{ function start()
{
ob_start(); ob_start — 打开输出控制缓冲
} function save($path)
{
$data = ob_get_contents(); ob_get_contents — 返回输出缓冲区的内容
ob_end_clean(); ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲
$this->wirtetoword($path,$data);
}
function wirtetoword ($fn,$data)
{
$fp=fopen($fn,"w"); fopen — 打开文件或者 URL,第二个参数是表示以什么方式打开
fwrite($fp,$data); fwrite — 写入文件(可安全用于二进制文件)
fclose($fp); fclose — 关闭一个已打开的文件指针
}
}
?>
2.引用该类,并从数据库中调用nation表的数据
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<?php
if(!isset($_GET["id"]))
{
?>
<input type="button" name="submit" value="将表格内容保存到Word" onclick="window.location.href='main.php?id=print'">
<?php
}
?>
<?php
if($_GET["id"]!="")
{
include("word.class.php"); 将word类引用进来
$word=new word(); 对该类进行实例化
$word->start(); 调用start()方法,定义要保存表格的开始 } ?> <table border="1" cellspacing="0" cellpadding="0">
<tr><th>代号</th><th>名称</th></tr>
<?php
include("../gongju/DBDA.class.php");
$db=new DBDA();
$sql="select * from nation";
$attr=$db->Query($sql);
foreach($attr as $v)
{
echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td></tr>";
} ?>
</table>
<?php
if($_GET["id"]!=""){
$word->save("data.doc"); 保存表格结束
} ?> </body>
</html>
最新文章
- Yii2.X 多语言-类图
- ansible-copy
- 9.9,新iPhone要来了,是欢呼,还是墙角画圈,一会儿见分晓
- 基础学习day10--异常、包
- iOS开发多线程篇—单例模式(ARC)
- android startActivityForResult(Intent intent, int requestCode) 整理与总结! .
- html-----018----HTML Web Server/HTML URL 字符编码
- Global build settings
- hibernate初体验
- linux下FFmpeg编译生成ffplay
- 新概念英语(1-45)The boss&#39;s letter
- Python__flask初识
- BZOJ_5118_Fib数列2_矩阵乘法+欧拉定理
- ReactNative之从“拉皮条”来看RN中的Spring动画
- Django Linux环境下部署CentOS7+Python3+Django+uWSGI+Nginx(含Nginx返回400问题处理、防火墙管理)
- MySQL ERROR 1130 (HY000): Host &#39;192.168.1.8&#39; is not allowed to connect to this MySQL server
- 用selenium获取cookies
- mac maven lombok报错
- apache atlas资料收集
- hping安装过程