
正文
phpxlsx数据库 php的数据库在哪个目录下
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP 如何利用phpexcel导入数据库
?php
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '/');//设置环境变量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //这个是读 xlsphpxlsx数据库的
$inputFileType = 'Excel2007';//这个是计xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'br /';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader-load($inputFileName);
/*
$sheet = $objPHPExcel-getSheet(0);
$highestRow = $sheet-getHighestRow(); //取得总行数
$highestColumn = $sheet-getHighestColumn(); //取得总列
*/
$objWorksheet = $objPHPExcel-getActiveSheet();//取得总行数
$highestRow = $objWorksheet-getHighestRow();//取得总列数
echo 'highestRow='.$highestRow;
echo "br";
$highestColumn = $objWorksheet-getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo 'highestColumnIndex='.$highestColumnIndex;
echo "br /";
$headtitle=array();
for ($row = 1;$row = $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col $highestColumnIndex;$col++)
{
$strs[$col] =$objWorksheet-getCellByColumnAndRow($col, $row)-getValue();
}
$info = array(
'word1'="$strs[0]",
'word2'="$strs[1]",
'word3'="$strs[2]",
'word4'="$strs[3]",
);
//在这儿,phpxlsx数据库你可以连接,你的数据库,写入数据库了
print_r($info);
echo 'br /';
}
?
相关问答
Q1: PHP如何将查询出来的数据导出成excel表格(最好做一个按钮)?
讲的复杂了啊!\x0d\x0a你先在一个完整版的PHPExcel之后解压,在“Examples”目录下会找到一大堆例子,根据你的要求这个“01simple-download-xlsx.php”文件就可以了!\x0d\x0a注:你先保持“01simple-download-xlsx.php”文件所在的目录位置不要变,测试好了,再改变名,移到别的地方,地方变了的话,文件里的 “require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';”的所在位置也要变!\x0d\x0a我们要改动代码很少,如下:\x0d\x0a// Add some data\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)\x0d\x0a-setCellValue('A1', 'Hello')\x0d\x0a-setCellValue('B2', 'world!')\x0d\x0a-setCellValue('C1', 'Hello')\x0d\x0a-setCellValue('D2', 'world!');\x0d\x0a\x0d\x0a// Miscellaneous glyphs, UTF-8\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)\x0d\x0a-setCellValue('A4', 'Miscellaneous glyphs')\x0d\x0a-setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');\x0d\x0a、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、\x0d\x0a直接用的我的替换\x0d\x0a$objPHPExcel-setActiveSheetIndex(0)//这个就是现实导出的表第一行,有几列是根据你的那张表有几列!\x0d\x0a-setCellValue('A1', '单号')\x0d\x0a-setCellValue('B1', '标题')\x0d\x0a-setCellValue('C1', '内容')\x0d\x0a-setCellValue('D1', '序列')\x0d\x0a-setCellValue('E1', '数字');\x0d\x0a//下面实现的就是建立数据库连接,直接到表,你的连接数据库、表、字段应该与我的不一样,你可以参考\x0d\x0a$conn=@mysql_connect("localhost","root","root") or die("数据库服务器连接错误".mysql_error());//连接mysql数据库\x0d\x0amysql_select_db("temp",$conn) or die("数据库访问错误".mysql_error());//数据库\x0d\x0amysql_query("set character set gb2312");\x0d\x0amysql_query("set names gb2312");\x0d\x0a\x0d\x0a$sqlgroups="select * from test ";//查询这一张表的条件\x0d\x0a$resultgroups=mysql_query($sqlgroups);\x0d\x0a$numrows=mysql_num_rows($resultgroups);\x0d\x0aif ($numrows0)\x0d\x0a{\x0d\x0a$count=1;\x0d\x0awhile($data=mysql_fetch_array($resultgroups))\x0d\x0a{\x0d\x0a$count+=1;\x0d\x0a$l1="A"."$count";\x0d\x0a$l2="B"."$count";\x0d\x0a$l3="C"."$count";\x0d\x0a$l4="D"."$count";\x0d\x0a$l5="E"."$count";\x0d\x0a$objPHPExcel-setActiveSheetIndex(0) \x0d\x0a-setCellValue($l1, $data['id'])//这就是你要导出表的字段、与对应的名称\x0d\x0a-setCellValue($l2, $data['title'])\x0d\x0a-setCellValue($l3, $data['content'])\x0d\x0a-setCellValue($l4, $data['sn'])\x0d\x0a-setCellValue($l5, $data['num']);\x0d\x0a}\x0d\x0a}
Q2: PHP 用PHPExcel往数据库导入大量数据
1、首先我们准备一个含有数据的Excel表格,表头和数据表中的表字段相对应。
2、在ThinkPHP中引入PHPExcel类库。
3、然后我们编写导入的PHP代码。
4、然后我们编写导出的PHP代码。
5、然后我们进行导出测试发现可以导出即可。
Q3: PHP将Excel导入数据库中
如果小的话,可以直接导入excel文件,如果大的话,请先存成csv文件,然后通过mysql
的load
data导入数据库
关于phpxlsx数据库和php的数据库在哪个目录下的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







