PHP-Excel读取导入Excel数据到数据库(2003,2007通用)使用方法
来源:互联网 发布:python可视化 编辑:程序博客网 时间:2024/04/30 18:20
<?php
header("Content-type: text/html; charset=utf-8");
require_once 'phpexcel.php';
require_once 'PHPExcel\IOFactory.php';
require_once 'PHPExcel\Reader\Excel2007.php';
$uploadfile='data.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/
$objPHPExcel = $objReader->load($uploadfile); //Excel 路径
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
/*方法一*/
$strs=array();
for ($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
/*注销上一行读取数据*/
unset($str);
unset($strs);
for($k='A';$k<=$highestColumn;$k++){//从A列读取数据
//实测在excel中,如果某单元格的值包含了||||||导入的数据会为空
$str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'||||||';//读取单元格
}
//explode:函数把字符串分割为数组。
$strs = explode("||||||",$str);
$sql = "INSERT INTO te() VALUES ( '{$strs[0]}','{$strs[1]}', '{$strs[2]}','{$strs[3]}','{$strs[4]}')";
echo $sql.'<br>';
}
/*方法二【推荐】*/
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // 取得总行数
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
for ($row = 1;$row <= $highestRow;$row++) {
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++) {
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
print_r($strs);
}
?>
header("Content-type: text/html; charset=utf-8");
require_once 'phpexcel.php';
require_once 'PHPExcel\IOFactory.php';
require_once 'PHPExcel\Reader\Excel2007.php';
$uploadfile='data.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/
$objPHPExcel = $objReader->load($uploadfile); //Excel 路径
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
/*方法一*/
$strs=array();
for ($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
/*注销上一行读取数据*/
unset($str);
unset($strs);
for($k='A';$k<=$highestColumn;$k++){//从A列读取数据
//实测在excel中,如果某单元格的值包含了||||||导入的数据会为空
$str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'||||||';//读取单元格
}
//explode:函数把字符串分割为数组。
$strs = explode("||||||",$str);
$sql = "INSERT INTO te() VALUES ( '{$strs[0]}','{$strs[1]}', '{$strs[2]}','{$strs[3]}','{$strs[4]}')";
echo $sql.'<br>';
}
/*方法二【推荐】*/
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // 取得总行数
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
for ($row = 1;$row <= $highestRow;$row++) {
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++) {
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
print_r($strs);
}
?>
0 0
- PHP-Excel读取导入Excel数据到数据库(2003,2007通用)使用方法
- PHP-Excel读取导入Excel数据到数据库(2003,2007通用)使用方法
- php导入excel 到数据库
- PHP导入Excel到数据库
- 从数据库读取数据导入到Excel中
- 数据库中读取数据导入到excel中
- 读取EXCEL中数据并导入到数据库
- 从Excel文件读取数据批量导入到Mysql数据库
- 从Mysql数据库读取数据批量导入到Excel文件
- 读取Excel数据、批量导入到Oracle数据库
- POI 读取excel导入导出数据到数据库
- 从Excel读取数据导入到数据库中
- 导入Excel数据到数据库
- 导入excel数据到数据库
- 导入excel数据到数据库
- excel-从excel导入数据到数据库
- php把excel表格中的数据导入到mysql数据库
- PHP上传Excel文件导入数据到MySQL数据库示例
- Oracle数据库导入导出
- 如何在eclipse下查看android源码?
- fatal error LNK1104: 无法打开文件“lua51.lib”
- 默染江山——oc之ARC、self
- IIS程序池应用于不同net framework版本时引发的问题(Server Application Unavailable)
- PHP-Excel读取导入Excel数据到数据库(2003,2007通用)使用方法
- 新的一周,新的旅程…
- 这世上本来没有路,走的人多了,也便成了路
- ubuntu14.10折腾tomcat8,是的,折腾
- 用diff比较目录文件差异和patch备份源代码
- linux内核的生成过程, vmlinux调试分析
- android 中毛玻璃效果(背景虚化)的实现
- 用弱引用堵住内存泄漏
- 软件项目将死的27个征兆