一、导入PHPExcel文件步骤:
1、上传文件:$upload_result = Upload_Files:uploadFile('file', Q::ini('custom_flag/upload_file_type/sales_order/value'));
文件上传数据库::上传方法(‘文件框的命名’, 类型值)
2、判断是否上传成功:if ($upload_result['ack'] == SUCCESS)
3、包含PHPExcel文件
4、获取上传文件的存储路径
5、创建对象 : $reader = PHPExcel_IOFactory::createReader('Excel5'); 并指定类型;
6、加载文件:$PHPExcel = $reader->load($filename);
7、获取活动工作表 $wordsheet = $PHPExcel->getActiveSheet();
8、获取最大的行数 :$highest_row = $wordsheet->getHighestRow();
9、获取最大的列数 : $highest_column = PHPExcel_Cell::columnIndexFromString($worksheet->getHighestColumn());
PHPExcel_Cell::columnIndexFromString 通过获取到的列值作为index获取对应的数字值
10、做循环输出获取内容:
$data = array ();
for ($i =2 ; $i <=$highest_row; $i++)
{
$row = array ();
for ($j =0 ; $j <$highest_column; $j++)
{
$cell = $worksheet->getCellByColumnAndRow($j , $i )->getValue();
array_push( $row, $cell );
}
array_push( $data, $row );
}
dump( $data);exit ;
二、导出PHPExcel文件步骤:
//引进PHPExcel类
$include Q::ini('custom_system/lib_dir) . 'PHPExcel.php';
//创建一个PHPExcel对象
$phpexcel = new PHPExcel();
//设置版本和创造者
$phpexcel->getProperties()->setCreator(Q::ini('custom_system/system_name) . Q::ini('custom_system/version'));
$phpexcel->getProperties()->setLastModifiedBy(Q::ini('custom_system/system_name)
. Q::ini('custom_system/version'));
$phpexcel->getProperties()->setTitle(); //设置 excel 标题
$phpexcel->getProperties()->setSubject(); //设置 excel 项目
$phpexcel->getProperties()->setDescription(); //设置 excel 描述
$phpexcel->getProperties()->setKeywords(); //设置 excel 关键字
$phpexcel->getProperties()->setCategory(); //设置 excel 分类
$phpexcel->setActiveSheetIndex(0); //设置 excel 活动工作表的索引
$phpexcel->getActiveSheet()->setTitle(); //设置活动工作表的标题
$phpexcel->getActiveSheet()->setCellValue('A1', ''); //设置表格的位置和值
$i = 2;
foreach ($array as $a)
{
$phpexcel->getActiveSheet()->setCellValue('A' . $i, '');
$i++;
}
$outputFileName = "name.xls"; //excel 的文件名
$objWriter = new PHPExcel_Writer_Excel5($phpexcel); //把活动工作表的内容写入类中,用于生成excel 文件;
//设置文件的header格式
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/octet-stream");
header("Content-type: application/download");
header('Content-Disposition:inline;filename="' . $outputFileName . '"');
header("Content-Transfer-Encoding:binary");
header("Cache-Control: must-revalidate, post-check=0, pri-check=0");
header("Pragma: no-cache");
$objWriter->save('php:://output'); //保存 excel 文件
//输出格式化后的表格内容
方法引用:
-
设置数字格式:
$phpexcel->getActiveSheet()->getCell('D'.$i)->setValueExplicit($r['zip'], PHPExcel_Cell_DataType::TYPE_NUMERIC);
-
设置字符串格式:
$phpexcel->getActiveSheet()->getCell('D'.$i)->setValueExplicit($r['zip'], PHPExcel_Cell_DataType::TYPE_STRING);
-
设置宽度:
$phpexcel ->getActiveSheet("A1:A2" / "A1")->getColumnDimension( 'B')->setWidth(20 );
$phpexcel ->getActiveSheet("A1:A2" / "A1")->getRowDimension(10)->setRowHeight(40);
-
设置对齐方式:
$phpexcel ->getActiveSheet()->getStyle("A1:F$rows / A")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平线对齐
$phpexcel->getActiveSheet()->getStyle("A1:F $rows")->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直线对齐
-
设置字体:
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getFont()->setName('Courier New');
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getFont()->setSize(10);
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getFont()->setBold(true);
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getFont()->setColor()->setARGB('FF999999');
-
设置边框:
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getBorders()->getTop()/getBottom()/getLeft()/getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getBorders()->getTop()/getBottom()/getLeft()/getRight()->getColor()->setARGB('FFFF0000');
-
设置填充颜色:
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getFill()->setFillType(PHPExcel_Style_Fill:FILL_SOLID);
$phpexcel ->getActiveSheet()->getStyle("A1:F $rows")->getStratColor()->setARGB('FFEEEEEE');
-
合并单元格:
$phpexcel ->getActiveSheet()->mergeCells('A1:F1');
-
分析单元格(参数是合并单元格的范围)
$phpexcel ->getActiveSheet()->unmergeCells( 'A1:F1');
-
在一个excel文件中添加多个sheet(工作表):
-
$phpexcel->createSheet(1);
$phpexcel->getSheet(1)->setTitle('');
$phpexcel->getSheet(1)->setCellValue('');
-
$phpexcel->createSheet();
$phpexcel->setActiveSheetIndex();
$phpexcel->getActiveSheetIndex->setCellValue('');
-
PHPExcel->Save(Q::ini('custom_system/export_dir') . Q::ini('custom_system/export_url') . $file_name;
利用PHPExcel保存文件时的开销是非常大的,所消耗的时间很长。
三、直接导出纯文本的excel文件
$data = "数据";
$data = iconv("UTF-8", "UTF-16LE", $data);
$output_file_name = "****.xls";
header( "Content-Type: application/vnd.ms-excel; charset=utf-8");
header( "Content-Type: application/octet-stream" );
header( "Content-Type: application/download" );
header( 'Content-Disposition:inline; filename="' . $output_file_name . '"' );
header( "Content-Transfer-Encoding: binary" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0");
header( "Pragma: no-cache");
echo $data;
exit;