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='<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>';
$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='<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>';
$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;
}

?>

<?php

error_reporting(E_ALL ^ E_NOTICE);
include_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("example.xls");


$html = "<html>
<head>
<style>
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;
}
</style>
</head>

<body>";

$html .= $data->dump(true,true);
$html .= "</body>
</html>";

cword($html , "test1");

?>

<html>
<head>
<style>
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;
}
</style>
</head>

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


發佈了49 篇原創文章 · 獲贊 42 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章