PHPExcel XLS XLSX记录

$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');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值