Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Nov 8, 2023
1 parent 94baeb2 commit f044983
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Extreme fast in-memory Excel (XLSX) file writer.
- Multiple sheets in a workbook.
- Header columns with bold font.
- Custom worksheet name.
- Data types for rows: string, int, float
- Data types for columns: string

## Limitations

Expand Down Expand Up @@ -52,13 +54,13 @@ $sheet->addColumns($columns);

// Write data
$rows = [
['2023-01-31', 'James', '220'],
['2023-03-28', 'Mike', '153.5'],
['2024-07-02', 'John', '34.12'],
['2023-01-31', 'James', 220],
['2023-03-28', 'Mike', 153.5],
['2024-07-02', 'Sally', 34.12],
];

foreach ($rows as $row) {
$sheet->writeRow($row);
$sheet->addRow($row);
}

// Save as Excel file
Expand Down
14 changes: 11 additions & 3 deletions src/ExcelWorksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ public function addRow(array $values): void

// s = 0 = Normal font (see styles.xml)
$column->setAttribute('s', '0');
$column->setAttribute('t', 's');
$sharedStringIndex = $this->sharedStrings->add($value);
$valueNode = $this->sheetXml->createElement('v', (string)$sharedStringIndex);

// Detect numeric types (not values)
if (is_int($value) || is_float($value)) {
$column->setAttribute('t', 'n');
$valueNode = $this->sheetXml->createElement('v', (string)$value);
} else {
$column->setAttribute('t', 's');
$sharedStringIndex = $this->sharedStrings->add((string)$value);
$valueNode = $this->sheetXml->createElement('v', (string)$sharedStringIndex);
}

$column->appendChild($valueNode);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function test(): void
$sheet2->addColumns($columns);

$data = [
['2023-12-31', 'Max', '220', '0.1'],
['2023-8-23', 'John', '1234.5', '0.3'],
['2023-06-01', 'Daniel', '6789.12', '1.4'],
['2023-12-31', 'Max', '220', 0.1],
['2023-8-23', 'John', '1234.5', 0.3],
['2023-06-01', 'Daniel', '6789.12', 1.4],
];

// Write data
Expand Down

0 comments on commit f044983

Please sign in to comment.