如何在我的PHP代码中使用粗体数组并使用PHPExcel类将其保存在excel文件中?我还想知道如何使保存文件中的背景有颜色:
我想要的例子:
我当前的代码:
<?php
if (!isset($_POST['send'])) { ?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php } else {
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php';
$filename = 'file.xlsx';
$title = $_POST['title'];
mysql_connect("localhost","root","") or die ("cant connect!");
mysql_select_db("test") or die ("cant find database!");
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){
$headings = array(
'ID',
'NAME',
'SCORE 1',
'SCORE 2',
'OTHER QUALITIES',
'INTERVIEW',
'TOTAL',
'AIC',
'BATCHCODE',
);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$objPHPExcel->getActiveSheet()
->getStyle('A1:I5')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
$row++;
}
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}
if (!isset($_POST['send'])) { ?>
<form id="form1" name="form1" method="post" action="" >
<input name="title" type="text" id="title" value="title" />
<input type="submit" name="send" value="send to excel" id="send" />
</form>
</body>
</html>
<?php }
答案 0 :(得分:2)
从数组设置if可能是最简单的方法:
$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->applyFromArray(
array(
'font' => array(
'bold' => true
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
'borders' => array(
'top' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startcolor' => array(
'argb' => 'FFA0A0A0'
),
'endcolor' => array(
'argb' => 'FFFFFFFF'
)
)
)
);
答案 1 :(得分:1)
只需更改单元格样式获取字体。
$objPHPExcel->getActiveSheet()
->getStyle("$cell:$cell")
->getFont()
->setBold(true);
或将此功能用作背景颜色
function cellColor($cells, $bgcolor, $color){
global $objPHPExcel;
$objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()
->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array('rgb' => $bgcolor),
'endcolor' => array('rgb' => $color)
));
}
答案 2 :(得分:0)
试一试!
<?php
if (!isset($_POST['send'])) { ?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php } else {
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php';
$filename = 'file.xlsx';
$title = $_POST['title'];
mysql_connect("localhost","root","") or die ("cant connect!");
mysql_select_db("test") or die ("cant find database!");
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){
$headings = array(
'ID',
'NAME',
'SCORE 1',
'SCORE 2',
'OTHER QUALITIES',
'INTERVIEW',
'TOTAL',
'AIC',
'BATCHCODE',
);
$objPHPExcel->getActiveSheet()->getStyle('A1:I5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:I5')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'CCCCCC') ), ) );
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$objPHPExcel->getActiveSheet()
->getStyle('A1:I5')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
$row++;
}
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}
if (!isset($_POST['send'])) { ?>
<form id="form1" name="form1" method="post" action="" >
<input name="title" type="text" id="title" value="title" />
<input type="submit" name="send" value="send to excel" id="send" />
</form>
</body>
</html>
<?php }