$phpexcel = new PHPExcel();
//(索引页)
if($index > 0)
{
$phpexcel->createSheet();
}
$phpexcel->setActiveSheetIndex($index);
//索引标题
$phpexcel->getActiveSheet()->setTitle('表');
// 设置样式
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => 'FF0000'),
'size' => 15,
'name' => 'Verdana'
));
$phpExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
//或
$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB('FF0000');
$phpExcel->getActiveSheet()->getStyle(‘A1’)->getFont()->setColor($phpColor);
//合并单元格
$phpexcel->getActiveSheet()->mergeCells('A1:H1');
//水平居中
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//所有单元格垂直居中 getDefaultStyle()
$phpexcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//垂直居中
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置字体大小
$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
//设置加粗
$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
// 输入内容(坐标:A1 A2 B1 B2,内容)
$phpexcel->getActiveSheet()->setCellValue('A1', 'ID');
//或者
$row = 1;
$col = 0;
$phpexcel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($col++) . $row, 'ID');
//以字符串形式输入
$phpexcel->getActiveSheet()->setCellValueExplicit('A1', 'ID');
//图片
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setCoordinates('A' . $y); //坐标
$objDrawing->setImageResource($image); //二进制图片
//$objDrawing->setPath($image); //图片路径
$objDrawing->setHeight(80);
$objDrawing->setWidth(80);
$objDrawing->setWorksheet($phpexcel->getActiveSheet());
//自动宽度
$phpexcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
// Set column widths/设置单元格的宽度
$phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
//自动换行
$phpexcel->getActiveSheet()->getStyle('B')->getAlignment()->setWrapText(true);
//行高
$phpexcel->getActiveSheet()->getRowDimension(3)->setRowHeight(100);
// 下拉框
$phpexcel->getActiveSheet()->getCell('A1')->getDataValidation()->setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
->setAllowBlank(false)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入值错误')
->setError('输入的值不再下拉列表内')
->setFormula1('"' . implode(',', $select_list) . '"');
//XLS
$writer = new PHPExcel_Writer_Excel5($phpexcel);
//XLSX
$writer = new PHPExcel_Writer_Excel2007($phpexcel);
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/octet-stream");
header("Content-type: application/download");
header('Content-Disposition:inline;filename=“a.xls”');
//header('Content-Disposition:inline;filename=“a.xlsx”');
header("Content-Transfer-Encoding:binary");
header("Cache-Control: must-revalidate, post-check=0, pri-check=0");
header("Pragma: no-cache");
$writer->save('php://output');
PHPExcel XLS XLSX记录
于 2020-05-27 10:56:23 首次发布