Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話

hirobe
March 08, 2024

PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話

PHPerKaigi2024 LT登壇資料です

hirobe

March 08, 2024
Tweet

More Decks by hirobe

Other Decks in Programming

Transcript

  1. 8 スプレッドシートの新規作成はもちろん PhpSpreadsheet $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet();

    $activeWorksheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx'); 参考:https://phpspreadsheet.readthedocs.io/en/latest/
  2. 10 Wordファイルの新規作成 PHPWord $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection();

    $section->addText('Hello World!'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx'); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
  3. 11 Wordファイルの読み込み PHPWord $reader = IOFactory::createReader('Word2007'); $reader->load(__DIR__ . '/sample.docx'); $reader

    = IOFactory::createReader('MsDoc'); $reader->load(__DIR__ . '/sample.doc'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
  4. 12 rtfやodtもサポートしている PHPWord $reader = IOFactory::createReader('RTF'); $reader->load(__DIR__ . '/sample.rtf'); $reader

    = IOFactory::createReader('ODText'); $reader->load(__DIR__ . '/sample.odt'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
  5. 13 書式の指定もできます PHPWord $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' =>

    'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) ); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
  6. 14 pptxファイルの新規作成 PHPPresentation $presentation = new PhpPresentation(); $writerPPTX = IOFactory::createWriter($presentation,

    'PowerPoint2007'); $writerPPTX->save(__DIR__ . '/sample.pptx'); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
  7. 15 pptファイルの読み込み PHPPresentation $reader = IOFactory::createReader('PowerPoint2007'); $reader->load(__DIR__ . '/sample.pptx'); $reader

    = IOFactory::createReader('PowerPoint97'); $reader->load(__DIR__ . '/sample.ppt'); 参考:https://phpoffice.github.io/PHPPresentation/usage/readers.html
  8. 16 ⽂字や画像の位置指定が、ちょっと⾯倒? PHPPresentation $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo')

    ->setPath('./resources/logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
  9. 22 PhpSpreadsheet、超メモリ容量⾷います > PhpSpreadsheet uses an average of about 1k

    per cell (1.6k on 64-bit PHP) in your worksheets 1セルごとに1kbのメモリを使⽤する →10000(100x100)セルで10MB使⽤する計算 とりあえず、読み込み範囲の制限をかけて対策 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/ 苦労したところ