<?php
02
/**
03
* phpexcel实例 导入数据库
04
* by www.jbxue.com
05
*/
06
error_reporting(E_ALL); //开启错误
07
set_time_limit(0); //脚本不超时
08
09
date_default_timezone_set('Europe/London'); //设置时间
10
11
/** Include path **/
12
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.jbxue.com/../Classes/');//设置环境变量
13
14
/** PHPExcel_IOFactory */
15
include 'PHPExcel/IOFactory.php';
16
17
//$inputFileType = 'Excel5'; //这个是读 xls的
18
$inputFileType = 'Excel2007';//这个是计xlsx的
19
//$inputFileName = './sampleData/example2.xls';
20
$inputFileName = './sampleData/book.xlsx';
21
22
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
23
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
24
$objPHPExcel = $objReader->load($inputFileName);
25
/*
26
$sheet = $objPHPExcel->getSheet(0);
27
$highestRow = $sheet->getHighestRow(); //取得总行数
28
$highestColumn = $sheet->getHighestColumn(); //取得总列
29
*/
30
$objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
31
$highestRow = $objWorksheet->getHighestRow();//取得总列数
32
33
echo 'highestRow='.$highestRow;
34
echo "<br>";
35
$highestColumn = $objWorksheet->getHighestColumn();
36
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
37
echo 'highestColumnIndex='.$highestColumnIndex;
38
echo "<br />";
39
$headtitle=array();
40
for ($row = 1;$row <= $highestRow;$row++)
41
{
42
$strs=array();
43
//注意highestColumnIndex的列数索引从0开始
44
for ($col = 0;$col < $highestColumnIndex;$col++)
45
{
46
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
47
}
48
$info = array(
49
'word1'=>"$strs[0]",
50
'word2'=>"$strs[1]",
51
'word3'=>"$strs[2]",
52
'word4'=>"$strs[3]",
53
);
54
//引处可放置代码,写入数据库
55
print_r($info);
56
echo '<br />';
57
}
58
?>
phpexcel实现导入内容到数据库中
最新推荐文章于 2024-06-13 18:49:59 发布