php做excel导入word,php 将excel文件写到word中

利用php写word的方法,网上主导的方法有三种:

1.利用微软提供com组件.我个人是不推荐的,首先是com组件的文档繁杂,用起来也不是很顺手.

2.利用第三方的wordphp的库,这个库的github网址如下

功能非常强大的一个库,而且不仅支持word格式,还支持pdf等多种格式.如果你需要的功能很多,可以考虑利用这个库.

3.利用html为中介生成mht格式(和HTML很相似)写入word

纯HTML格式写入word

我个人利用的方法就是这个.

在这里给出一个php函数,利用的就是html页面为中介function cword($data, $fileName = '')

{

if (empty($data)) {

return '';

}

$data = '' . $data . '';

$dir = "./docfile/";

if (!file_exists($dir)) {

mkdir($dir, 777, true);

}

if (empty($fileName)) {

$fileName = $dir . date('His') . '.doc';

} else {

$fileName = $dir . $fileName . '.doc';

}

$writefile = fopen($fileName, 'wb') or die("创建文件失败"); //wb以二进制写入

fwrite($writefile, $data);

fclose($writefile);

return $fileName;

}

既然以及使用html为中介,那么从读取excel的方法,也可以利用html为中介,先把excel转化为html,再把对应的html页面存为word文件.

在这里利用的是github上面的zekus/php-excel-reader项目

由于源文件过大,我就不放代码了,大家自己去链接下载就可以了

下面各一个具体的例子

在这里调用抵用excel_rader2.php里面的方法,将对应的excel调用为对应的方法将其转化为对应的html格式的字符串,然后利用cword函数,保存为word文件<?php

function cword($data, $fileName = '')

{

if (empty($data)) {

return '';

}

$data = '' . $data . '';

$dir = "./docfile/";

if (!file_exists($dir)) {

mkdir($dir, 777, true);

}

if (empty($fileName)) {

$fileName = $dir . date('His') . '.doc';

} else {

$fileName = $dir . $fileName . '.doc';

}

$writefile = fopen($fileName, 'wb') or die("创建文件失败"); //wb以二进制写入

fwrite($writefile, $data);

fclose($writefile);

return $fileName;

}

?>

error_reporting(E_ALL ^ E_NOTICE);

include_once 'excel_reader2.php';

$data = new Spreadsheet_Excel_Reader("example.xls");

$html = "

table.excel {

border-style:ridge;

border-width:1;

border-collapse:collapse;

font-family:sans-serif;

font-size:12px;

}

table.excel thead th, table.excel tbody th {

background:#CCCCCC;

border-style:ridge;

border-width:1;

text-align: center;

vertical-align:bottom;

}

table.excel tbody th {

text-align:center;

width:20px;

}

table.excel tbody td {

vertical-align:bottom;

}

table.excel tbody td {

padding: 0 3px;

border: 1px solid #EEEEEE;

}

$html .= $data->dump(true, true);

$html .= "

";

cword($html, "test1");

?>

table.excel {

border-style:ridge;

border-width:1;

border-collapse:collapse;

font-family:sans-serif;

font-size:12px;

}

table.excel thead th, table.excel tbody th {

background:#CCCCCC;

border-style:ridge;

border-width:1;

text-align: center;

vertical-align:bottom;

}

table.excel tbody th {

text-align:center;

width:20px;

}

table.excel tbody td {

vertical-align:bottom;

}

table.excel tbody td {

padding: 0 3px;

border: 1px solid #EEEEEE;

}

<?php echo $data->dump(true, true); ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值