一般,有2種方法可以導(dǎo)出doc文檔:
創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)瓦房店,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108
第一種是使用com,并且作為php的一個(gè)擴(kuò)展庫(kù)安裝到服務(wù)器上,然后創(chuàng)建一個(gè)com,調(diào)用它的方法。安裝過(guò)office的服務(wù)器可以調(diào)用一個(gè)叫word.application的com,可以生成word文檔,不過(guò)這種方式我不推薦,因?yàn)閳?zhí)行效率比較低(我測(cè)試了一下,在執(zhí)行代碼的時(shí)候,服務(wù)器會(huì)真的去打開一個(gè)word客戶端)。理想的com應(yīng)該是沒(méi)有界面的,在后臺(tái)進(jìn)行數(shù)據(jù)轉(zhuǎn)換,這樣效果會(huì)比較好,但是這些擴(kuò)展一般需要收費(fèi)。
第2種方法,就是用PHP將我們的doc文檔內(nèi)容直接寫入一個(gè)后綴為doc的文件中即可。使用這種方法不需要依賴第三方擴(kuò)展,而且執(zhí)行效率較高。
function add($name, $type, $size, $defaultvalue = '', $options = '', $title = '', $note = '', $formtype = '', $inputtool = '', $inputlimit = '', $enablehtml = 1, $enablelist = 1, $enablesearch = 0)
{
if(!in_array($type, $this-fieldtypes) || $this-exists($name)) return FALSE;
$size = intval($size);
$fieldsize = $type == 'varchar' ? min($size, 255) : ($type == 'int' ? min($size, 10) : 0);
$fieldtype = strtoupper($type);
if($fieldsize) $fieldtype .= "( $fieldsize )";
$this-db-query("ALTER TABLE $this-table ADD $name $fieldtype NOT NULL");
$this-db-query("INSERT INTO ".TABLE_FIELD."(tablename,name,type,size,defaultvalue,options,title,note,formtype,inputtool,inputlimit,enablehtml,enablelist,enablesearch) VALUES('$this-table','$name','$type','$size','$defaultvalue','$options','$title','$note','$formtype','$inputtool','$inputlimit','$enablehtml','$enablelist','$enablesearch')");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
function edit($fieldid, $type, $size, $defaultvalue = '', $options = '', $title = '', $note = '', $formtype = '', $inputtool = '', $inputlimit = '', $enablehtml = 1, $enablelist = 1, $enablesearch = 0)
{
if(!in_array($type, $this-fieldtypes)) return FALSE;
$fieldid = intval($fieldid);
$field = $this-get_info($fieldid);
$name = $field['name'];
$size = intval($size);
$fieldsize = $type == 'varchar' ? min($size, 255) : ($type == 'int' ? min($size, 10) : 0);
$fieldtype = strtoupper($type);
if($fieldsize) $fieldtype .= "( $fieldsize )";
$this-db-query("ALTER TABLE `$this-table` CHANGE `$name` `$name` $fieldtype NOT NULL");
$this-db-query("UPDATE ".TABLE_FIELD." SET title='$title',note='$note',type='$type',size='$size',defaultvalue='$defaultvalue',options='$options',formtype='$formtype',inputtool='$inputtool',inputlimit='$inputlimit',enablehtml='$enablehtml',enablelist='$enablelist',enablesearch='$enablesearch' WHERE fieldid=$fieldid");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
function delete($fieldid)
{
$fieldid = intval($fieldid);
$r = $this-db-get_one("SELECT name FROM ".TABLE_FIELD." WHERE fieldid=$fieldid");
if(!$r) return FALSE;
$name = $r['name'];
$this-db-query("ALTER TABLE $this-table DROP $name");
$this-db-query("DELETE FROM ".TABLE_FIELD." WHERE fieldid=$fieldid");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
db數(shù)據(jù)庫(kù)類去下個(gè)phpcms里面的就是上面的是自定義字段的操作函數(shù)
1自己拼,XML編碼
?php
header('Content-type:text/xml');
echo "?xml version='1.0' encoding='utf-8'";
echo "book";
echo "PHP";
echo "namePHP程序開發(fā)范例寶典/name";
echo "price 單位='元/本'89.00/price";
echo "date2007-09-01/date";
echo "/PHP";
echo "/book";
?
拼接的效果
2從數(shù)據(jù)庫(kù)中查詢?cè)倨碭ML編碼
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','passwowd'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫(kù)連接對(duì)象$pdo
$query="select * from book"; ? //定義SQL語(yǔ)句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //準(zhǔn)備查詢語(yǔ)句
$result-execute(); ? ? ? //執(zhí)行查詢語(yǔ)句,并返回結(jié)果集
$arr='';
while($res=$result-fetch()){
$arr.='PHPid'.$res[0].'/idname'.$res[1].'/namedate'.$res[2].'/date'.'price'.$res[3].'/price/PHP';
}
echo "?xml version='1.0' encoding='utf-8'?book".$arr.'/book';
} catch (PDOException $e) {
die ("Error!: ".$e-getMessage()."br");
}
?
拼接的效果
3使用ajax獲取,DOM解析
!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
titlexml/title
/head
body
script
function check(){
var xhr=new XMLHttpRequest();
xhr.open('GET','xml.php');
xhr.onreadystatechange=function(){
if(xhr.readyState==4 xhr.status==200){
console.log(xhr.responseText);
//初始化 DOM解析對(duì)象
var domParser = new DOMParser();
//字符串解碼為對(duì)象
var xmlDoc = domParser.parseFromString(xhr.responseText,'text/xml');
//按標(biāo)簽名獲取元素 返回?cái)?shù)組
var elements = xmlDoc.getElementsByTagName('PHP');
//拼接html格式字符串
var str ='trthid/ththname/ththdate/ththprice/th/tr';
for (var i=0;ielements.length;i++){
var id=elements[i].getElementsByTagName('id')[0].firstChild.nodeValue;
var name=elements[i].getElementsByTagName('name')[0].firstChild.nodeValue;
var date=elements[i].getElementsByTagName('date')[0].firstChild.nodeValue;
var price=elements[i].getElementsByTagName('price')[0].firstChild.nodeValue;
str+= 'trtd'+id+'/tdtd'+name+'/tdtd'+date+'/tdtd'+price+'/td/tr';
}
document.getElementById('table2').innerHTML=str;
}
};
xhr.send(null);
}
/script
button onclick="check();"點(diǎn)我/button
table id="table2" border="2" cellspacing="0"
/table
/body
/html
效果
1
2
補(bǔ)充:
使用JSON
1數(shù)據(jù)庫(kù)查詢,自己拼?JSON 編碼
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫(kù)連接對(duì)象$pdo
$query="select * from book"; ? //定義SQL語(yǔ)句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //準(zhǔn)備查詢語(yǔ)句
$result-execute(); ? ? ? //執(zhí)行查詢語(yǔ)句,并返回結(jié)果集
$a=$arr='';
while($res=$result-fetch()){
$arr.='{"id":'.'"'.$res[0].'",'.'"name":'.'"'.$res[1].'",'.'"time":'.'"'.$res[2].'",'.'"jia":'.'"'.$res[3].'",'.'"zhe":'.'"'.$res[4].'",'.'"chu":'.'"'.$res[5].'"},';
}
echo $a="[".substr($arr,0,strlen($arr)-1)."]";
} catch (PDOException $e) {
die ("Error!: ".$e-getMessage()."br");
}
?
2數(shù)據(jù)庫(kù)查詢,函數(shù)?JSON 編碼
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫(kù)連接對(duì)象$pdo
$query="select * from book"; ? //定義SQL語(yǔ)句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //準(zhǔn)備查詢語(yǔ)句
$result-execute(); ? ? ? //執(zhí)行查詢語(yǔ)句,并返回結(jié)果集
$res=$result-fetchAll();
//JSON 編碼
echo json_encode($res);
} catch (PDOException $e) {
die ("Error!: ".$e-getMessage()."br/");
}
?
效果
3ajax獲取,JSON解析
!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
titleJSON/title
/head
body
script
function check() {
var XHR = new XMLHttpRequest();
XHR.open('GET','JSON.php');
XHR.onreadystatechange = function (){
if(XHR.readyState == 4 XHR.status ==200){
var books =JSON .parse(XHR.responseText);
var books2='trthid/ththbookname/ththtime/ththprice/ththmarker/ththpublisher/th/tr'
for (var i=0;ibooks.length;i++){
books2+= 'trtd'+(books[i ].id)+'/tdtd'+(books[i ].name)+'/tdtd'+(books[i ].time)+'/tdtd'+(books[i ].jia)+'/tdtd'+(books[i ].zhe)+'/tdtd'+(books[i ].chu)+'/td/tr';
}
document.getElementById('table2').innerHTML=books2;
}
};
XHR.send(null);
}
/script
input type="button" value="點(diǎn)我" onclick="check();"
table id="table2" border="2" cellspacing="0"/table
/body
/html
你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫(kù)
2、讀取這個(gè)sql文件里的sql語(yǔ)句,并執(zhí)行
3、生成一個(gè)數(shù)據(jù)庫(kù)連接參數(shù)的php文件
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
if?(mysql_query("CREATE?DATABASE?my_db",$con))
{
echo?"Database?created";
}
else
{
echo?"Error?creating?database:?"?.?mysql_error();
}
mysql_close($con);
?
?php
class?ReadSql?{
//數(shù)據(jù)庫(kù)連接
protected?$connect?=?null;
//數(shù)據(jù)庫(kù)對(duì)象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語(yǔ)句集
public?$sqlArr?=?array();
public?function?__construct($host,?$user,?$pw,?$db_name)?{
$host?=?empty($host)???C("DB_HOST")?:?$host;
$user?=?empty($user)???C("DB_USER")?:?$user;
$pw?=?empty($pw)???C("DB_PWD")?:?$pw;
$db_name?=?empty($db_name)???C("DB_NAME")?:?$db_name;
//連接數(shù)據(jù)庫(kù)
$this-connect?=?mysql_connect($host,?$user,?$pw)?or?die("Could?not?connect:?"?.?mysql_error());
$this-db?=?mysql_select_db($db_name,?$this-connect)?or?die("Yon?can?not?select?the?table:"?.?mysql_error());
}
//導(dǎo)入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打開文件錯(cuò)誤");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
}
}
}
//獲取sql語(yǔ)句數(shù)組
public?function?GetSqlArr()?{
//去除注釋
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?創(chuàng)建數(shù)組
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
}?else?{
$this-sqlArr[]?=?$v;
}
}
}
//執(zhí)行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql語(yǔ)句錯(cuò)誤:第"?.?$k?.?"行"?.?mysql_error());
}
}
return?true;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");
$rst?=?$sql-Import("./log_db.sql");
if?($rst)?{
echo?"Success!";
}
?
可以實(shí)現(xiàn),你說(shuō)的這個(gè)功能只是javascript來(lái)操作html元素的內(nèi)容,你搜索php自然不會(huì)找到解決方法。