PHP导入及导出excel数据

  1. 以下导出
			#写入表头
            $letter = [
                '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', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF',
            ];

            $template = 'data/example/testList.xlsx';#本地空白模板(写入时调用此模板写入,然后生成新文件)
            $objPHPExcel = PHPExcel_IOFactory::load($template);
            $objPHPExcel->setactivesheetindex(0); #页码(0为第一页,以此类推)
            #写入数据
            $row = 1;#写入行数
            $objPHPExcel->getActiveSheet()->setCellValue($letter[0] . $row, "这是写入的数据", \PHPExcel_Cell_DataType::TYPE_STRING);#给A1写入数据
            $objPHPExcel->getActiveSheet()->getStyle($letter[0] . $row)->getFont()->getColor()->setARGB('000080');#设置A1字体颜色 
            $objPHPExcel->getActiveSheet()->getStyle($letter[0] . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); //设置填充颜色第一步
            $objPHPExcel->getActiveSheet()->getStyle($letter[0] . $row)->getFill()->getStartColor()->setARGB('00FFFFFF'); #设置填充颜色第二步
            $objPHPExcel->getActiveSheet()->getStyle($letter[0] . $row)->getAlignment()->setWrapText(true); #单元格自动换行
            $objPHPExcel->getActiveSheet()->getRowDimension($letter[0] . $row)->setRowHeight(50); #设置行高度
            $objPHPExcel->getActiveSheet()->getStyle($letter[0] . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); #居左
			$objPHPExcel->getActiveSheet()->insertNewRowBefore($row, 1);  #在某行前插入一行(参数1:被插入的行,参数2:插入行数)
			$mergeRow = 6;
			$mergeRange = "A5" . ':' . 'A' . $mergeRow;#A5至A6
			$objPHPExcel->getActiveSheet()->mergeCells($mergeRange); #合并

            #保存
            ob_end_clean();
            $filename = '测试文件(' . date('Y-m-d', strtotime($time)) . ').xls';#保存的文件名  
            ob_end_clean();
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit;
  1. 以下导入
		$template = 'upload/text.xlsx';#上传至upload目录的text.xlsx文件
		try {
        	$objPHPExcel = PHPExcel_IOFactory::load($template);
        } catch (Exception $e) {
            die('
加载文件发生错误:"' . pathinfo($template, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } #读取excel的第一个工作表 $sheet = $objPHPExcel->getSheet(0); #取得总行数 $highestRow = $sheet->getHighestRow('C'); #取得总列数 $highestColumm = $sheet->getHighestColumn(); #获取最大列值字母对应的数值 $highestColummNum = PHPExcel_Cell::columnIndexFromString($highestColumm); if (!$highestRow || !$highestColumm) { exit('表中无数据'); } #获取表格值进行存储 $dataExcel = array(); $i = 0; for ($row = 2; $row <= $highestRow; $row++) { #行数是以第2行开始 for ($column = 'A'; PHPExcel_Cell::columnIndexFromString($column) <= $highestColummNum; $column++) { //列数是以A列开始 foreach ($newOtOption as $otOpkey => $otOpval) { $dataExcel[$i][$otOpval] = $sheet->getCell($column . $row)->getValue();#获取指定行列的数据 } $dataExcel[$i]['month'] = date('Y-m', time()); } $i++; } print_r($dataExcel);

你可能感兴趣的:(个人记录,php)