Skip to content

Instantly share code, notes, and snippets.

@GithubMrxia
Last active February 28, 2020 10:34
Show Gist options
  • Save GithubMrxia/7ea8d18be90d8b5152dbf8c2ad1ececb to your computer and use it in GitHub Desktop.
Save GithubMrxia/7ea8d18be90d8b5152dbf8c2ad1ececb to your computer and use it in GitHub Desktop.
php操作excel
<?php
$spreadsheet = @IOFactory::load(storage_path('app/public/template/开票申请单模板.xls'));
$spreadsheet->setActiveSheetIndex(0);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('C2')->setValue(date('Y'));
$worksheet->getCell('C2')->setValue(date('Y'));
$worksheet->getCell('E2')->setValue(date('m'));
$worksheet->getCell('G2')->setValue(date('d'));
$worksheet->getCell('C3')->setValue($merchantInfo->merchant_name);
$worksheet->getCell('C4')->setValue($merchantInfo->contact_name);
$worksheet->getCell('K2')->setValue($merchantInfo->contact_mobile);
$worksheet->getCell('A7')->setValue($invoiceTypeDesc);
$worksheet->getCell('L7')->setValue($invoiceData->invoice_amount);
$worksheet->getCell('B13')->setValue($merchantInfo->contact_name);
$worksheet->getCell('G13')->setValue($merchantInfo->contact_mobile);
$worksheet->getCell('K13')->setValue($merchantInfo->invoice_address);
$spreadsheet->setActiveSheetIndex(1);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('C2')->setValue(date('Y'));
$worksheet->getCell('C2')->setValue(date('Y'));
$worksheet->getCell('E2')->setValue(date('m'));
$worksheet->getCell('G2')->setValue(date('d'));
$spreadsheet->setActiveSheetIndex(0);
// 结束 进行导出
ob_end_clean(); // 清空(擦除)缓冲区并关闭输出缓冲
ob_start(); // 打开输出控制缓冲
$filename = $invoiceData->order_no;
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename . '.xls"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment