我可以使用Phpspreadsheet库设法将数据导出到Excel工作表,并且它可以工作,但是我需要在单元格上添加边框,因此我编写了如下函数,但没有变化:
function translatorscity_xslx_export_users($header, $data){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($header, NULL, 'A1');
$sheet->fromArray($data, NULL, 'A2');
// redirect output to client browser
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="translatorscity-users-'.gmdate('Y-d-m-h-i-sa').'.xlsx"');
header('Cache-Control: max-age=0');
header("Content-Transfer-Encoding: binary");
//-------------This is added for borders-------------------
$styleArray = array(
'borders' => array(
'outline' => array(
'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => array('argb' => 'FFFF0000'),
),
),
);
$sheet = $sheet ->getStyle('A1:D109')->applyFromArray($styleArray);
//------------End borders-----------------------------------
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
# Free Memory
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}
样式代码包含在注释行中。我在这里想念什么
答案 0 :(得分:0)
进行一些搜索后,问题在于我在发送标题后设置了样式,因此在设置样式之前先下载文件,因此我应该在标题之前设置样式,因此代码应为:
function translatorscity_xslx_export_users($header, $data){
//Get number of rows
$dataCount = count($data) + 1;
$spreadsheet = new Spreadsheet();
//Set sheet meta data
$spreadsheet->getProperties()
->setCreator("Translators City")
->setLastModifiedBy("Translators City")
->setTitle("Translators City's users data")
->setSubject("These are users data exported from translatorscity.com")
->setDescription(
"Program created by mo7amed.maki@gmail.com"
)
->setKeywords("data")
->setCategory("users");
$sheet = $spreadsheet->getActiveSheet();
//Set column width to auto
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
$sheet->getColumnDimension('D')->setAutoSize(true);
//Set table outer borders
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'borders' => [
'top' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'right' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'bottom' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'left' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
],
];
$sheet->getStyle('A1:D'.$dataCount.'')->applyFromArray($styleArray);
//Set header outer borders
$styleArray = [
'font' => [
'bold' => true,
'size' => 18,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'borders' => [
'top' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'right' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'bottom' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
'left' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
],
],
];
$sheet->getStyle('A1:D1')->applyFromArray($styleArray);
//Fill data
$sheet->fromArray($header, NULL, 'A1');
$sheet->fromArray($data, NULL, 'A2');
// redirect output to client browser
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="translatorscity-users-'.gmdate('Y-d-m-h-i-sa').'.xlsx"');
header('Cache-Control: max-age=0');
header("Content-Transfer-Encoding: binary");
//Write and save
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
# Free Memory
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}