PHPOffice / PhpSpreadsheet出力xlsx文件
时间: 2023-11-28 15:48:02 浏览: 132
要使用 PHPOffice / PhpSpreadsheet 来输出 xlsx 文件,您需要按照以下步骤进行操作:
1. 安装 PHPOffice / PhpSpreadsheet
您可以使用 Composer 安装 PHPOffice / PhpSpreadsheet,命令如下:
```
composer require phpoffice/phpspreadsheet
```
2. 创建 Xlsx 文件
下面的代码可以帮助您创建一个简单的 Xlsx 文件:
```php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// 创建新的 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
// 获取当前活动的工作表
$sheet = $spreadsheet->getActiveSheet();
// 设置单元格的值
$sheet->setCellValue('A1', 'Hello World!');
// 将数据写入 Xlsx 文件
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');
```
在此示例中,我们创建了一个新的 Spreadsheet 对象,并获取了当前活动的工作表。然后,我们在单元格 A1 中设置了一个值,并使用 XlsxWriter 将数据写入了一个名为 hello_world.xlsx 的 Xlsx 文件中。
3. 添加更多数据
您可以使用 PhpSpreadsheet 添加更多数据到工作表中。例如:
```php
// 设置标题行
$sheet->setCellValue('A1', 'Name')
->setCellValue('B1', 'Email')
->setCellValue('C1', 'Phone');
// 添加数据行
$sheet->setCellValue('A2', 'John Doe')
->setCellValue('B2', 'john.doe@example.com')
->setCellValue('C2', '123-456-7890');
// 冻结第一行
$sheet->freezePane('A2');
```
在这个示例中,我们设置了标题行和数据行,并使用 freezePane() 方法冻结了第一行。
4. 格式化单元格
您可以使用 PhpSpreadsheet 格式化单元格。例如:
```php
// 设置数字格式
$sheet->getStyle('C2')->getNumberFormat()->setFormatCode('000-000-0000');
// 设置字体颜色和背景色
$sheet->getStyle('A1')->getFont()->getColor()->setARGB('FFFFFF');
$sheet->getStyle('A1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('000000');
```
在此示例中,我们设置了 C2 单元格的数字格式,以及 A1 单元格的字体颜色和背景色。
5. 下载文件
要将 Xlsx 文件下载到用户的计算机上,您可以使用以下代码:
```php
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="hello_world.xlsx"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
```
在此示例中,我们设置了响应标头,以便将文件下载到用户的计算机上。
这些是使用 PHPOffice / PhpSpreadsheet 输出 xlsx 文件的基本步骤。您可以根据自己的需求进一步操作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)