乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > php 如何把word文档中的内容 导入到数据库中-word插入数据库,word怎么建立数据库

php 如何把word文档中的内容 导入到数据库中-word插入数据库,word怎么建立数据库

作者:乔山办公网日期:

返回目录:word文档


给你写了一个代码,里面假定了你的数据库地址、用户名、密码、数据库、表格名称
<?php
//读取文件
$txtFile="someTxt.txt";
$content=file($txtfile);
$db=mysql_connect("localhost","root","","mydatabase"); //连接数据库,自己填好参数吧
foreach($content as $ff)
{
if(preg_match("/(.+?)| *?([0-9]+) *?| *?([0-9]+\-[0-9]+\-[0-9]+ [0-9]+:[0-9]+:[0-9]+) *?id:([0-9])+/i",$ff,$vals)>0)
{
mysql_query($db,"INSERT INTO `my_table` VALUES (\"{$vals[1]}\",{$vals[2]},\"{$vals[3]}\",{$vals[4]})");
if(mysql_errno()!=0)
{echo"Error!";break;}
}
}
mysql_close($db);
?>

代码如下:

$content = file($haoma); //$temp=file("seo.csv");//连接EXCEL文件,格式为了.csvfor ($i=0;$i <count($content);$i++){$string=explode(",",$content[$i]);//通过循环得到EXCEL文件中每行记录的值$sql = "insert into {$tablepre}object (area,objectname,objectaddress,state,commend) values ('$string[0]','$string[1]','$string[2]','$state','$commend')";$sqls = mb_convert_encoding($sql,"UTF-8","GBK"); //这里用的是urf8编码 $rs = $db->query($sqls);
给你提供一个读取Word内容的代码,自己把读取出来的内容插入数据库:<?
/**
* @desc PHP读取Word文档
*
*/
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("asd.doc"));
$content = (string) $word->ActiveDocument->Content; echo $content; //读取Word内容
$word->ActiveDocument->Close(false); $word->Quit();
$word = null;
unset($word);
?>

方法有两个:
1. 直接用php读取文件,然后以BLOB流的形式存入数据库,这种方法是不能查询文件内容的,只能把数zd据库当成一个容器来用
2. 先将word文档转换成xml文档,word菜单里另存为xml就行,然后用php分析这个xml文件,把其中的内容存进数据库。好处word文档的内容可以通过数据库来查询,但是还原成原word文档不能保证100%的word格式无损失(word 2010之后的版本对这个问题有改进)。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网