PHP使用PHPexcel导入导出数据的方法
作者:jackluo 发布时间:2024-05-13 09:21:10
标签:PHP,PHPexcel,导入,导出
本文实例讲述了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 . 'https://www.jb51.net/../Classes/');//设置环境变量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //这个是读 xls的
$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]",
);
//在这儿,你可以连接,你的数据库,写入数据库了
print_r($info);
echo '<br />';
}
?>
导出数据:
(如果有特殊的字符串 = 麻烦str_replace(array('='),'',$val['roleName']);)
private function _export_data($data = array())
{
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(FCPATH.APPPATH.'/libraries/Classes/');//设置环境变量
// Create new PHPExcel object
Include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
if($data){
$i = 1;
foreach ($data as $key => $value) {
$newobj = $objPHPExcel->setActiveSheetIndex(0);
$j = 0;
foreach ($value as $k => $val) {
$index = $letter[$j]."$i";
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($index, $val);
$j++;
}
$i++;
}
}
$date = date('Y-m-d',time());
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($date);
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$date.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
直接上代码:
public function export_data($data = array())
{
# code...
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/IWriter.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/Excel5.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/IOFactory.php') ;
$obj_phpexcel = new PHPExcel();
$obj_phpexcel->getActiveSheet()->setCellValue('a1','Key');
$obj_phpexcel->getActiveSheet()->setCellValue('b1','Value');
if($data){
$i =2;
foreach ($data as $key => $value) {
# code...
$obj_phpexcel->getActiveSheet()->setCellValue('a'.$i,$value);
$i++;
}
}
$obj_Writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5');
$filename = "outexcel.xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$obj_Writer->save('php://output');
}
希望本文所述对大家php程序设计有所帮助。
0
投稿
猜你喜欢
- 一、算法概述AdaBoost 是英文 Adaptive Boosting(自适应增强)的缩写,由 Yoav Freund 和Robert S
- 方式一:叠加文字水印最简单的一种方式是,在图片上绘制半透明文本来实现水印效果。主要用到Figure.text函数参数类型说明x, yfloa
- FTP服务的主动模式和被动模式在开始之前,先聊一下FTP的主动模式和被动模式,两者的区别 , 用两张图来表示可能会更加清晰一些:主动模式:主
- python操作excel主要用到 xlrd 和 xlwt 这两个库,xlrd读取excel表格数据, 支持 xlsx和
- 前言对于MySQL的理解,我认为很多性能优化工作、主从主主复制都是在调整参数,来适应不同时期不同数量级的数据。故,理解透彻my.cnf里的参
- numpy和matlab的几点差异Python numpy和matlab都是便捷灵活的科学计算语言,两者具有很多相似之处,但也有一些混淆的地
- 环境:Ubuntu14、Python3.4、Pycharm2018一、使用command=lambda: 的形式传参代码如下from tki
- python语言中的列表排序方法有3个:reverse反转/倒序排序sort正序排序sorted可以获取排序后的列表在更高级列表排序中,后两
- MySQL服务器有几个影响其操作的参数(变量)。如果缺省的参数值不合适,可以将其修改为对服务器运行环境更合适的值。例如,如果您有大量的内存,
- 本文实例讲述了原生JS实现Ajax通过POST方式与PHP进行交互的方法。分享给大家供大家参考,具体如下:一、代码conn.php<?
- Jupyter NotebookJupyter项目是从Ipython项目中分出去的,在Ipython3.x之前,他们两个是在一起发布的。在I
- 我就废话不多说了,大家还是直接看代码吧try: s = socket.socket() s.bind(('127.0.0.1'
- import urllib.parse,os.path,time,sys,re,urllib.requestfrom http.client
- 刚刚看了bootstrap的导航栏,发现有点弄混了,现在来整理一下;导航栏是一个很好的功能,是 Bootstrap 网站的一个突出特点。导航
- 在python中enumerate的用法多用于在for循环中得到计数,本文即以实例形式向大家展现python中enumerate的用法。具体
- class test { &nbs
- 挺久没写博客了,因为博主开始了今年另一段美好的实习经历,学习加做项目,时间已排满;很感谢今年这两段经历,让我接触了golang和python
- 在对于时间准确度的把握上,为了使操作的更加细化,很多人习惯把时间精确到秒。但在实际程序操作中,虽然秒数方便我们的查阅,但是计算机并不能直接的
- MySQL5升级为MySQL8问题my.ini首先推荐一个软件“everything”,一个轻巧的遍
- 显示下级的方法elementui的节点过滤默认是不显示下级的代码在 :filter-node-method="filterNode