digitaldream / data-converter
在Web应用中,我们需要将数据从一种类型转换为另一种类型。例如,将数组转换为Excel文件或将Excel文件转换为数组。
1.3.0
2024-04-14 13:45 UTC
Requires
- php: ^8.2
- phpoffice/phpspreadsheet: ^1.6
README
在Web应用中,我们需要将Excel文件转换为数组,将数组转换为Excel文件等。##安装方法
"require-dev": { "digitaldream/data-converter": "1.0.*" }
##特性
- Excel到数组及关联数组(如果第一行标记为标题行)
- Excel到json(json到Excel)
- Excel到文本(文本到Excel)
- 在导入Excel、json、txt、csv时过滤数据
- 并且可以从一个格式轻松交换到其他格式
##用途
示例01
$fileManager = new \DataConverter\FileExcel();
许多其他配置,例如您可以从哪一行开始读取,读取多少行。您还可以向文件追加数据。
$config = [ 'file_path' => 'C:\Users\Tuhin\Downloads\apiv1567623ccbcfc1_570a009e10650.xlsx', 'first_row_as_headline' => true, ]; $data = $fileManager->config($config)->read()->getData(); //$data = $fileManager->config($config)->read()->toJson(); //$data = $fileManager->config($config)->read()->toText(); //$data = $fileManager->config($config)->read()->toAssoc(); print_r($data); ?>
示例02
//Here it will process the file based on file mimetype. $fileFullPath='test.txt'; $fileManager = \DataConverter\FileManager::initByFileType($fileFullPath); if ($fileManager === FALSE) { // exit() File tye does not mathch } //here we used filter. It will take only these two columns value and other data from the souce will be ignored. $data = $fileManager->config($config)->read()->makeAssoc()->filter(['column_1','column_2])->getData();
示例03
$fileManager = new \DataConverter\FileExcel(); $configFile = [ 'file_path' => $fullPath,// full file path where file will be saved. 'data' => $data,//data as associative array. 'mode' => 'w+'//only valid if file type is txt ]; $fileManager = $fileManager->config($configFile)->filter(['column 1','column 2'])->write(true);
更多示例在demo/exmaples.php文件中