31_Document_properties_write.php
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\IOFactory;
require __DIR__ . '/../Header.php';
$inputFileType = 'Xlsx';
$inputFileName = __DIR__ . '/../templates/31docproperties.xlsx';
$spreadsheetReader = IOFactory::createReader($inputFileType);
$callStartTime = microtime(true);
$spreadsheet = $spreadsheetReader->load($inputFileName);
$helper->logRead($inputFileType, $inputFileName, $callStartTime);
$helper->log('Adjust properties');
$spreadsheet->getProperties()->setTitle('Office 2007 XLSX Test Document')
->setSubject('Office 2007 XLSX Test Document')
->setDescription('Test XLSX document, generated using PhpSpreadsheet')
->setKeywords('office 2007 openxml php');
// Save Excel 2007 file
$filename = $helper->getFilename(__FILE__);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
$helper->logEndingNotes();
// Reread File
$helper->log('Reread Xlsx file');
$spreadsheetRead = IOFactory::load($filename);
// Set properties
$helper->log('Get properties');
$helper->log('Core Properties:');
$helper->log(' Created by - ' . $spreadsheet->getProperties()->getCreator());
$helper->log(' Created on - ' . date('d-M-Y' . $spreadsheet->getProperties()->getCreated()) . ' at ' . date('H:i:s' . $spreadsheet->getProperties()->getCreated()));
$helper->log(' Last Modified by - ' . $spreadsheet->getProperties()->getLastModifiedBy());
$helper->log(' Last Modified on - ' . date('d-M-Y' . $spreadsheet->getProperties()->getModified()) . ' at ' . date('H:i:s' . $spreadsheet->getProperties()->getModified()));
$helper->log(' Title - ' . $spreadsheet->getProperties()->getTitle());
$helper->log(' Subject - ' . $spreadsheet->getProperties()->getSubject());
$helper->log(' Description - ' . $spreadsheet->getProperties()->getDescription());
$helper->log(' Keywords: - ' . $spreadsheet->getProperties()->getKeywords());
$helper->log('Extended (Application) Properties:');
$helper->log(' Category - ' . $spreadsheet->getProperties()->getCategory());
$helper->log(' Company - ' . $spreadsheet->getProperties()->getCompany());
$helper->log(' Manager - ' . $spreadsheet->getProperties()->getManager());
$helper->log('Custom Properties:');
$customProperties = $spreadsheet->getProperties()->getCustomProperties();
foreach ($customProperties as $customProperty) {
$propertyValue = $spreadsheet->getProperties()->getCustomPropertyValue($customProperty);
$propertyType = $spreadsheet->getProperties()->getCustomPropertyType($customProperty);
if ($propertyType == Properties::PROPERTY_TYPE_DATE) {
$formattedValue = date('d-M-Y H:i:s', (int) $propertyValue);
} elseif ($propertyType == Properties::PROPERTY_TYPE_BOOLEAN) {
$formattedValue = $propertyValue ? 'TRUE' : 'FALSE';
} else {
$formattedValue = $propertyValue;
}
$helper->log(' ' . $customProperty . ' - (' . $propertyType . ') - ' . $formattedValue);
}
$helper->logEndingNotes();