/**
* 文件导出导入
* @author 冀松松 ijisong@qq.com
* 2016年8月4日 12:59:03
*
*/
class ExcelData{
function __construct(){
require_once dirname(__FILE__).'/PHPExcel.php';
require_once dirname(__FILE__).'/PHPExcel/IOFactory.php';
}
/**
* 数据导出
* @param string $name 保存路径以及文件名,后缀为xlsx;为空时直接导出
* @param array $arr 导出的数据,二维数组 [['ss'=>1111]]
* @param array $freewith 自适应宽度列 ['A','B']
* @author 冀松松 ijisong@qq.com
*/
function write($name='',$arr=[],$freewith=[]){
$objPHPExcel = new \PHPExcel();
// echo date('H:i:s') , "设置文档属性" , EOL;
$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");
// Common::dump($arr,1);
// echo date('H:i:s') , "设置显示得字体大小" , EOL;
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')
->setSize(10);
$key=0;
foreach ($arr as $v){
if (count($v)>26){
exit('列长超出');
}
$numchr=65;
$excel=$objPHPExcel->getActiveSheet();
foreach ($v as $vstr){
$excel->setCellValue(chr($numchr++).($key+1), $vstr);
}
$key++;
}
//自适应宽度
foreach ($freewith as $v){
$objPHPExcel->getActiveSheet()->getColumnDimension($v)->setAutoSize(true);
}
// 重命名工作表,是指excel文档下的选项卡名称
// $objPHPExcel->getActiveSheet()->setTitle('Datatypes');
// $objPHPExcel->setActiveSheetIndex(0);
if ($name){
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($_SERVER['DOCUMENT_ROOT'].'/'.$name.'.xlsx');
}else {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.date('Y-m-d H:i').'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}
/**
* 读取数据 支持exel以及html('xlsx','xlsm','xltx','xltm','xls','xlt','ods','ots','slk','xml','gnumeric','htm','html','csv')
* 此处可以使用临时文件路径
* @param unknown $filepath
* @return multitype:
* @author 冀松松 ijisong@qq.com
*/
function read($filepath){
$objPHPExcel = \PHPExcel_IOFactory::load($filepath);
return $objPHPExcel->getActiveSheet()->toArray();
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史