本文整理汇总了PHP中PHPExcel_Writer_Excel2007::setPHPExcel方法的典型用法代码示例。如果您正苦于以下问题:PHP PHPExcel_Writer_Excel2007::setPHPExcel方法的具体用法?PHP PHPExcel_Writer_Excel2007::setPHPExcel怎么用?PHP PHPExcel_Writer_Excel2007::setPHPExcel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHPExcel_Writer_Excel2007
的用法示例。
在下文中一共展示了PHPExcel_Writer_Excel2007::setPHPExcel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: extraExcel
/**
* excel导出方法
* @param array $content 导出内容
*/
public function extraExcel($content)
{
if (!$this->_excelId && in_array($this->_excelRecordSaveType, array('database', 'file'))) {
$this->_halt('You must create a record with getOneRecord() first!');
}
//参数是否存在
if (empty($this->_excelTitle) || empty($this->_excelFields) || empty($content)) {
$this->_halt('excelTitle,excelFields,content can not be empty!');
}
// 创建PHPExcel对象
$objPHPExcel = new PHPExcel();
// 设置excel文件名称
$objPHPExcel->getProperties()->setTitle($this->_excelTitle);
// 设置当前工作表
$objPHPExcel->setActiveSheetIndex(0);
//设置宽width
//$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
//$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
// 列编号从0开始,行编号从1开始
$col = 0;
$row = 1;
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($this->_excelFields as $field) {
$objActSheet->getColumnDimensionByColumn($col)->setAutoSize(true);
$objActSheet->setCellValueByColumnAndRow($col, $row, $field);
$objActSheet->getStyleByColumnAndRow($col, $row)->getFont()->setBold(true);
$objActSheet->getStyleByColumnAndRow($col, $row)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objActSheet->getStyleByColumnAndRow($col, $row)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objActSheet->getStyleByColumnAndRow($col, $row)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objActSheet->getStyleByColumnAndRow($col, $row)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$col++;
}
//数组转为对象,自动匹配传递参数fields表头字段和内容
$result = new StdClass();
foreach ($content as $key => $value) {
$result->{$key} = new StdClass();
foreach ($this->_excelFields as $k => $v) {
$result->{$key}->{$k} = isset($value[$k]) ? $value[$k] : '';
}
}
// 从第二行开始输出数据内容
$row = 2;
foreach ($result as $data) {
$col = 0;
foreach ($this->_excelFields as $field => $name) {
if (is_numeric($data->{$field}) && strlen($data->{$field}) < 12 && (substr($data->{$field}, 0, 1) != 0 || $data->{$field} <= 0)) {
$objActSheet->setCellValueExplicitByColumnAndRow($col, $row, $data->{$field}, PHPExcel_Cell_DataType::TYPE_NUMERIC);
} else {
$objActSheet->setCellValueExplicitByColumnAndRow($col, $row, $data->{$field}, PHPExcel_Cell_DataType::TYPE_STRING);
}
$col++;
}
$row++;
}
//输出excel文件
//实例化excel写方法,并调用函数
$PHPExcel2007 = new PHPExcel_Writer_Excel2007();
$PHPWriter = $PHPExcel2007->setPHPExcel($objPHPExcel);
//判断文件夹是否存在,如果不存在就创建新的文件夹
$savePath = self::EXCELFILE_PATH . date('Ymd') . '/';
$this->checkFloderExists($savePath);
//设置文件名
list($usec, $sec) = explode(' ', microtime());
$tail = date('YmdHis') . str_replace('0.', '', $usec);
$fileName = $savePath . mb_convert_encoding($this->_excelTitle, 'GB2312', 'UTF-8') . '_' . $tail . '.xlsx';
$downUrl = $savePath . $this->_excelTitle . '_' . $tail . '.xlsx';
//导出的文件
$PHPWriter->save($fileName);
$this->setFinished($downUrl);
return $downUrl;
}