Skip to content

Commit

Permalink
auto
Browse files Browse the repository at this point in the history
  • Loading branch information
devkeep163 committed May 15, 2024
1 parent 2b69fa0 commit 89306b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
33 changes: 18 additions & 15 deletions php/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,24 @@ function createExcelTemplate(array $columns, string $filename = 'example.xlsx')


/**
* 读取Excel文件 (此代码由ChatGPT-4构建生成)
*
* 读取Excel文件
* @param string $filePath 要读取的文件路径
* @param int $maxColumnCount 限定读取的最大列数
* @param int $maxColumnCount 限定读取的最大列数(优化空间)
* @param int $activeSheetIndex 要读取的工作表索引(默认为第一个)
* @return array
*/
function readExcelFile(string $filePath, int $maxColumnCount = 0
function readExcelFile(string $filePath, int $maxColumnCount = 0, int $activeSheetIndex = 0): array
{
// 确保文件存在
if (!file_exists($filePath)) {
throw new Exception('文件不存在');
}

// 初始化Reader对象,以流式读取节省内存
$reader = new ReaderXlsx();
$reader->setReadDataOnly(true);

try {
// 加载文件
$spreadsheet = $reader->load($filePath); 
$spreadsheet = $reader->load($filePath);

// 设置当前活动工作表
$spreadsheet->setActiveSheetIndex($activeSheetIndex);

// 获取第一个工作表
$worksheet = $spreadsheet->getActiveSheet();
Expand All @@ -159,12 +157,12 @@ function readExcelFile(string $filePath, int $maxColumnCount = 0) 
// 设置迭代器只读取有数据的列
$cellIterator = $row->getCellIterator(
'A',
$maxColumnCount ? \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($maxColumnCount) : null 
$maxColumnCount ? \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($maxColumnCount) : null
);
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
$cellValue = $cell->getCalculatedValue(); 
$cellValue = $cell->getCalculatedValue();
$rowData[] = $cellValue;

if ($cellValue != null && $cellValue !== '') {
Expand Down Expand Up @@ -226,13 +224,18 @@ function export(array $data, string $fileName = 'export.xlsx', string $savePath
{
// 加粗第一行的标题
$activeSheet->getStyle([$colIndex + 1, $rowIndex + 1])->getFont()->setBold(true);

// 给第一行设置背景色
$activeSheet->getStyle([$colIndex + 1, $rowIndex + 1])->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('CCCCCC');
}
}
}

$writer = new WriterXlsx($spreadsheet);

if (empty($savePath))  
if (empty($savePath))
{
// 设置HTTP头部信息以强制浏览器下载文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
Expand All @@ -245,15 +248,15 @@ function export(array $data, string $fileName = 'export.xlsx', string $savePath
unset($spreadsheet);
exit;
}
else  
else
{
if (!is_dir($savePath))
{
mkdir($savePath, 0777, true);
}

$path = $savePath . '/' . $fileName;
$writer->save($path); 
$writer->save($path);

// 释放内存并返回文件路径
$spreadsheet->disconnectWorksheets();
Expand Down
29 changes: 29 additions & 0 deletions php/docs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$directoryPath = 'd:\phpEnv\www\buybestbuy\app\Console';

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directoryPath),
\RecursiveIteratorIterator::SELF_FIRST
);

// 遍历所有文件
foreach ($iterator as $file)
{
try {
if ($file->isFile())
{
$content = file_get_contents($file->getPathname());
$tokens = token_get_all($content);
foreach ($tokens as $token) {
if (is_array($token) && $token[0] === T_DOC_COMMENT) {
dd($this->extractDocComment($token[1]));
}
}
}
}
catch (\UnexpectedValueException $e)
{
echo "Could not access file: " . $file->getPathname() . "\n";
}
}

0 comments on commit 89306b4

Please sign in to comment.