php操作Excel
php操作Excel
1.new PHPExcel对象
$objPHPExcel = new PHPExcel();
2表的初始化设置
$objPHPExcel->getProperties()->setCreator($project_info['chief_name']) //创建人
->setLastModifiedBy($project_info['chief_name']) //最后修改人
->setTitle($project_info['chenghu'] . '先生/女士装修报价单') //标题
->setSubject($project_info['chenghu'] . '先生/女士装修报价单') //主题
->setDescription($project_info['chenghu'] . '先生/女士装修报价单') //描述
->setKeywords($project_info['chenghu'] . '先生/女士装修报价单') //关键词
->setCategory($project_info['chenghu'] . '先生/女士装修报价单'); //分类
//表格头部
$objPHPExcel->setActiveSheetIndex(0);//第一个sheet
$objPHPExcel->getActiveSheet()->setTitle($project_info['chenghu'] . '先生&女士装修报价单');//第一页对应的标题
$objPHPExcel->getDefaultStyle()->getFont()->setSize(13);//设置默认字体大小13
//表格内容
$objPHPExcel->getActiveSheet()->getStyle($cv)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle($cv . ($bench + 1))->getFont()->setBold(true);//字体加粗
$objPHPExcel->getActiveSheet()->mergeCells('A' . ($bench + 1) . ':G' . ($bench + 1));//水平合并A-G的列
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($bench + 1), $str);//设置单元格的值
$bench 代表行数
$colsymbol代表按顺序用到的列的集合,是个数组
$cellwidth 代表按顺序用到的单元格宽度的集合,是个数组
$rowheight 代表单元格高度的集合,是个数组
foreach ($rowheight as $rk => $rv) {
foreach ($colsymbol as $k => $cv) {
$objPHPExcel->getActiveSheet()->getColumnDimension($cv)->setWidth($cellwidth[$k]);
$objPHPExcel->getActiveSheet()->getRowDimension($rk + 1)->setRowHeight($rv);
if ($cv == 'G') {
$objPHPExcel->getActiveSheet()->mergeCells('A' . ($rk + 1) . ':G' . ($rk + 1));
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($rk + 1), $headercontent[$rk]);
}
}
}
以上代码段奠定了这张表的格局
总体思路,以操作单元格为基准来定位,横坐标是行号,纵坐标是列数
设置单元格格式,设置单元格内容,合并单元格这几个常规操作的方法要运用自如
简单的导出
YiiUtils::exportExcel($title, $exceldata);
$title = date('Y-m-d').'数据';
var_dump($exceldata)的数据如下:
array (size=288)
0 =>
array (size=4)
0 => string '项目名称' (length=12)
1 => string '单位' (length=6)
2 => string '单价' (length=6)
3 => string '工艺说明及材料说明' (length=27)
1 =>
array (size=4)
0 => string '卫生间基层回填(从楼盘运碎砖)' (length=45)
1 => string '㎡' (length=3)
2 => string '250' (length=3)
3 => string '⑴用外楼盘碎砖及砼结构碎渣回填,从楼盘外运到工地。' (length=75)
2 =>
array (size=4)
0 => string '卫生间基层回填' (length=21)
1 => string '㎡' (length=3)
2 => string '11500' (length=5)
3 => string '⑴用本工地碎砖及砼结构碎渣回填(深度500mm以内)' (length=66)
3 =>
array (size=4)
0 => string '卫生间基层回填(从楼下运碎砖)' (length=45)
1 => string '㎡' (length=3)
2 => string '12000' (length=5)
3 => string '⑴用本楼盘其他碎砖及砼结构碎渣回填,楼下有碎砖需从楼下运到工地。' (length=96)
......
已key为行号,二维数组的每个值为列的值
/**
*
*读取excel文件中的第一个工作表并将内容已数组形式输出
*$filePath为上传文件的绝对路径
**/
public static function readExcel($filePath)
{
$PHPExcel = new PHPExcel();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$PHPReader = new PHPExcel_Reader_Excel2007();
if (!$PHPReader->canRead($filePath)) {
$PHPReader = new PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filePath)) {
return array();
}
}
$contentArr = array();
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
/**从第A列开始输出*/
$item = array();
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
$item[] = $val;
}
$contentArr[] = $item;
}
return $contentArr;
}