반응형
phpoffice의 깃허브:https://github.com/PHPOffice
설치하다.
우리는 Composer를 사용하여 PHPWord를 설치합니다.
composer require phpoffice/phpword
어떻게 사용하는가
자동 로드
phpword 설치 후php 문서를 새로 만들고 autoload.php를 도입한다
require 'vendor/autoload.php';
인스턴스화
빈 페이지를 인스턴스화하고 새로 추가합니다
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
텍스트 내용 추가
빈 페이지에 텍스트 추가내용, 글꼴, 색상, 글꼴 크기, 굵은 글씨 등 문자 스타일을 설정할 수 있습니다
$fontStyle = [
'name' => 'Microsoft Yahei UI',
'size' => 20,
'color' => '#ff6600',
'bold' => true
];
$textrun = $section->addTextRun();
$textrun->addText('안녕하세요, 이것은 워드 문서입니다。 ', $fontStyle);
링크
Word 문서의 텍스트를 클릭하기 위한 링크를 추가할 수 있습니다
$section->addLink('https://www.helloweba.net', '欢迎访问Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
$section->addTextBreak();
그림
word에서 이미지 주소 logo.png, 64x64와 같은 이미지를 추가할 수 있습니다.이미지 소스는 원격 이미지일 수도 있다
$section->addImage('logo.png', array('width'=>64, 'height'=>64));
머리글
Word 문서의 머리글을 추가합니다
$header = $section->addHeader();
$header->addText('Subsequent pages in Section 1 will Have this!');
바닥글
word 문서에 대한 푸터를 추가합니다. 푸터의 내용은 페이지 번호, 형식 중앙입니다
$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
한 페이지 추가
한 페이지를 더 추가해 내용을 추가한다.
$section = $phpWord->addSection();
$section->addText('새 페이지.');
표
기본 양식을 하나 더 추가하면 양식의 양식을 설정할 수 있다
$header = array('size' => 16, 'bold' => true);
$rows = 10;
$cols = 5;
$section->addText('Basic table', $header);
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
$table->addRow();
for ($c = 1; $c <= 5; $c++) {
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
}
}
워드 문서 생성
만약 네가 wor를 생성하고 싶다면d 문서는 다음 작업을 위해 서버에 저장됩니다.
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('hellwoeba.docx');
워드 문서 다운로드
W를 다운로드하고 싶다면ord 문서를 서버에 저장하지 않으면 다음을 사용할 수 있습니다
$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
위 코드는 브라우저에 워드 문서로 다운로드를 강제합니다
반응형
'개발 꿀팁 > PHP' 카테고리의 다른 글
php에서 인터페이스를 호출하고 인터페이스를 작성합니다 (0) | 2022.07.11 |
---|---|
eclipse php 플러그인 설치 및 환경 설정 (0) | 2022.07.11 |
PhpWord의 간단한 용법 (0) | 2022.07.11 |
PHP를 사용하여 몇 가지 99 곱셈표를 쓰는 방식 (0) | 2022.07.11 |
docker php 환경 구축 웹 프로젝트 실행 (0) | 2022.07.09 |