Skip to content

Commit 347e80c

Browse files
author
jianyan74
committed
修改导出数字是科学计数
1 parent 960eb9a commit 347e80c

File tree

1 file changed

+68
-55
lines changed

1 file changed

+68
-55
lines changed

src/Excel.php

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
namespace jianyan\excel;
34

45
use Exception;
56
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
7+
use PhpOffice\PhpSpreadsheet\Cell\DataType;
68
use PhpOffice\PhpSpreadsheet\Spreadsheet;
79
use PhpOffice\PhpSpreadsheet\Writer\Html;
810
use PhpOffice\PhpSpreadsheet\Writer\Xls;
@@ -23,19 +25,25 @@ class Excel
2325
/**
2426
* 导出Excel
2527
*
26-
* @param array $list 数据
28+
* @param array $list 数据
2729
* @param array $header 数据处理格式
28-
* @param string $filename 导出的文件名
29-
* @param string $suffix 导出的格式
30-
* @param string $path 导出的存放地址 无则不在服务器存放
31-
* @param string $image 导出的格式 可以用 大写字母 或者 数字 标识 哪一列
30+
* @param string $filename 导出的文件名
31+
* @param string $suffix 导出的格式
32+
* @param string $path 导出的存放地址 无则不在服务器存放
33+
* @param string $image 导出的格式 可以用 大写字母 或者 数字 标识 哪一列
3234
* @return bool
3335
* @throws \PhpOffice\PhpSpreadsheet\Exception
3436
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
3537
*/
36-
public static function exportData($list = [], $header = [], $filename = '', $suffix = 'xlsx', $path = '', $image = [])
37-
{
38-
if (!is_array ($list) || !is_array ($header)) {
38+
public static function exportData(
39+
$list = [],
40+
$header = [],
41+
$filename = '',
42+
$suffix = 'xlsx',
43+
$path = '',
44+
$image = []
45+
) {
46+
if (!is_array($list) || !is_array($header)) {
3947
return false;
4048
}
4149

@@ -58,26 +66,26 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
5866
// 开始写入内容
5967
$column = 2;
6068
$size = ceil(count($list) / 500);
61-
for($i = 0; $i < $size; $i++) {
69+
for ($i = 0; $i < $size; $i++) {
6270
$buffer = array_slice($list, $i * 500, 500);
6371

64-
foreach($buffer as $k => $row) {
72+
foreach ($buffer as $k => $row) {
6573
$span = 1;
6674

67-
foreach($header as $key => $value) {
75+
foreach ($header as $key => $value) {
6876
// 解析字段
6977
$realData = self::formatting($header[$key], trim(self::formattingField($row, $value[1])), $row);
7078
// 写入excel
7179
$rowR = Coordinate::stringFromColumnIndex($span);
7280
$sheet->getColumnDimension($rowR)->setWidth(20);
73-
if(in_array($span,$image) || in_array($rowR,$image) ){ // 如果这一列应该是图片
74-
if(file_exists($realData)){ // 本地文件
81+
if (in_array($span, $image) || in_array($rowR, $image)) { // 如果这一列应该是图片
82+
if (file_exists($realData)) { // 本地文件
7583
$drawing = new Drawing();
7684
$drawing->setName('image');
7785
$drawing->setDescription('image');
78-
try{
86+
try {
7987
$drawing->setPath($realData);
80-
}catch(\Exception $e){
88+
} catch (\Exception $e) {
8189
echo $e->getMessage();
8290
echo '<br>可能是图片丢失了或者无权限';
8391
die;
@@ -89,20 +97,20 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
8997
$drawing->setOffsetX(12);
9098
$drawing->setOffsetY(12);
9199
$drawing->setWorksheet($spreadsheet->getActiveSheet());
92-
}else{ // 可能是 网络文件
93-
$img = self::curlGet($realData);
100+
} else { // 可能是 网络文件
101+
$img = self::curlGet($realData);
94102
$file_info = pathinfo($realData);
95103
$extension = $file_info['extension'];// 文件后缀
96-
$dir = '.' . DIRECTORY_SEPARATOR . 'execlImg'. DIRECTORY_SEPARATOR . \date('Y-m-d'). DIRECTORY_SEPARATOR;// 文件夹名
97-
$basename = time(). mt_rand(1000,9999).'.'.$extension;// 文件名
98-
is_dir($dir) OR mkdir($dir, 0777, true); //进行检测文件夹是否存在
99-
file_put_contents($dir.$basename , $img);
104+
$dir = '.' . DIRECTORY_SEPARATOR . 'execlImg' . DIRECTORY_SEPARATOR . \date('Y-m-d') . DIRECTORY_SEPARATOR;// 文件夹名
105+
$basename = time() . mt_rand(1000, 9999) . '.' . $extension;// 文件名
106+
is_dir($dir) or mkdir($dir, 0777, true); //进行检测文件夹是否存在
107+
file_put_contents($dir . $basename, $img);
100108
$drawing = new Drawing();
101109
$drawing->setName('image');
102110
$drawing->setDescription('image');
103-
try{
104-
$drawing->setPath($dir.$basename);
105-
}catch(\Exception $e){
111+
try {
112+
$drawing->setPath($dir . $basename);
113+
} catch (\Exception $e) {
106114
echo $e->getMessage();
107115
echo '<br>可能是图片丢失了或者无权限';
108116
die;
@@ -115,8 +123,10 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
115123
$drawing->setOffsetY(12);
116124
$drawing->setWorksheet($spreadsheet->getActiveSheet());
117125
}
118-
}else{
119-
$sheet->setCellValue($rowR . $column, $realData);
126+
} else {
127+
// $sheet->setCellValue($rowR . $column, $realData);
128+
// 写入excel
129+
$sheet->setCellValueExplicit(Coordinate::stringFromColumnIndex($span) . $column, $realData, DataType::TYPE_STRING);
120130
}
121131

122132

@@ -129,8 +139,7 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
129139
}
130140

131141
// 直接输出下载
132-
switch ($suffix)
133-
{
142+
switch ($suffix) {
134143
case 'xlsx' :
135144
$writer = new Xlsx($spreadsheet);
136145
if (!empty($path)) {
@@ -184,6 +193,7 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
184193

185194
break;
186195
}
196+
187197
return true;
188198
}
189199

@@ -197,7 +207,7 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
197207
*/
198208
public static function exportCsvData($list = [], $header = [], $filename = '')
199209
{
200-
if (!is_array ($list) || !is_array ($header)) {
210+
if (!is_array($list) || !is_array($header)) {
201211
return false;
202212
}
203213

@@ -208,7 +218,7 @@ public static function exportCsvData($list = [], $header = [], $filename = '')
208218
!$filename && $filename = time();
209219

210220
$html = "\xEF\xBB\xBF";
211-
foreach($header as $k => $v) {
221+
foreach ($header as $k => $v) {
212222
$html .= $v[0] . "\t ,";
213223
}
214224

@@ -218,13 +228,13 @@ public static function exportCsvData($list = [], $header = [], $filename = '')
218228
$info = [];
219229
$size = ceil(count($list) / 500);
220230

221-
for($i = 0; $i < $size; $i++) {
231+
for ($i = 0; $i < $size; $i++) {
222232
$buffer = array_slice($list, $i * 500, 500);
223233

224-
foreach($buffer as $k => $row) {
234+
foreach ($buffer as $k => $row) {
225235
$data = [];
226236

227-
foreach($header as $key => $value) {
237+
foreach ($header as $key => $value) {
228238
// 解析字段
229239
$realData = self::formatting($header[$key], trim(self::formattingField($row, $value[1])), $row);
230240
$data[] = str_replace(PHP_EOL, '', $realData);
@@ -249,19 +259,19 @@ public static function exportCsvData($list = [], $header = [], $filename = '')
249259
*
250260
* @param $filePath excel的服务器存放地址 可以取临时地址
251261
* @param int $startRow 开始和行数
252-
* @param bool $hasImg 导出的时候是否有图片
253-
* @param string $suffix 格式
254-
* @param string $imageFilePath 作为临时使用的 图片存放的地址
262+
* @param bool $hasImg 导出的时候是否有图片
263+
* @param string $suffix 格式
264+
* @param string $imageFilePath 作为临时使用的 图片存放的地址
255265
* @return array|mixed
256266
* @throws Exception
257267
* @throws \PhpOffice\PhpSpreadsheet\Exception
258268
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
259269
*/
260-
public static function import($filePath, $startRow = 1,$hasImg = false,$suffix = 'Xlsx',$imageFilePath = null)
270+
public static function import($filePath, $startRow = 1, $hasImg = false, $suffix = 'Xlsx', $imageFilePath = null)
261271
{
262-
if($hasImg){
263-
if($imageFilePath == null){
264-
$imageFilePath = '.' . DIRECTORY_SEPARATOR . 'execlImg'. DIRECTORY_SEPARATOR . \date('Y-m-d'). DIRECTORY_SEPARATOR;
272+
if ($hasImg) {
273+
if ($imageFilePath == null) {
274+
$imageFilePath = '.' . DIRECTORY_SEPARATOR . 'execlImg' . DIRECTORY_SEPARATOR . \date('Y-m-d') . DIRECTORY_SEPARATOR;
265275
}
266276
if (!file_exists($imageFilePath)) {
267277
//如果目录不存在则递归创建
@@ -282,16 +292,16 @@ public static function import($filePath, $startRow = 1,$hasImg = false,$suffix =
282292
for ($i = 0; $i < $sheetCount; $i++) {
283293
$objWorksheet = $spreadsheet->getSheet($i); // 读取excel文件中的第一个工作表
284294
$data = $objWorksheet->toArray();
285-
if($hasImg){
295+
if ($hasImg) {
286296
foreach ($objWorksheet->getDrawingCollection() as $drawing) {
287297
list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
288298
$imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
289-
$imageFileName .= '.'.$drawing->getExtension();
299+
$imageFileName .= '.' . $drawing->getExtension();
290300
$source = imagecreatefromjpeg($drawing->getPath());
291301
imagejpeg($source, $imageFilePath . $imageFileName);
292-
302+
293303
$startColumn = self::ABC2decimal($startColumn);
294-
$data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
304+
$data[$startRow - 1][$startColumn] = $imageFilePath . $imageFileName;
295305
}
296306
}
297307
$excleDatas[$i] = $data; // 多个sheet的数组的集合
@@ -301,19 +311,22 @@ public static function import($filePath, $startRow = 1,$hasImg = false,$suffix =
301311
$returnData = $excleDatas ? array_shift($excleDatas) : [];
302312

303313
// 第一行数据就是空的,为了保留其原始数据,第一行数据就不做array_fiter操作;
304-
$returnData = $returnData && isset($returnData[$startRow]) && !empty($returnData[$startRow]) ? array_filter($returnData) : $returnData;
314+
$returnData = $returnData && isset($returnData[$startRow]) && !empty($returnData[$startRow]) ? array_filter($returnData) : $returnData;
315+
305316
return $returnData;
306317
}
307318

308-
private static function ABC2decimal($abc){
319+
private static function ABC2decimal($abc)
320+
{
309321
$ten = 0;
310322
$len = strlen($abc);
311-
for($i=1;$i<=$len;$i++){
312-
$char = substr($abc,0-$i,1);//反向获取单个字符
323+
for ($i = 1; $i <= $len; $i++) {
324+
$char = substr($abc, 0 - $i, 1);//反向获取单个字符
313325

314326
$int = ord($char);
315-
$ten += ($int-65)*pow(26,$i-1);
327+
$ten += ($int - 65) * pow(26, $i - 1);
316328
}
329+
317330
return $ten;
318331
}
319332

@@ -327,8 +340,7 @@ protected static function formatting(array $array, $value, $row)
327340
{
328341
!isset($array[2]) && $array[2] = 'text';
329342

330-
switch ($array[2])
331-
{
343+
switch ($array[2]) {
332344
// 文本
333345
case 'text' :
334346
return $value;
@@ -339,7 +351,7 @@ protected static function formatting(array $array, $value, $row)
339351
break;
340352
// 选择框
341353
case 'selectd' :
342-
return $array[3][$value] ?? null ;
354+
return $array[3][$value] ?? null;
343355
break;
344356
// 匿名函数
345357
case 'function' :
@@ -365,9 +377,9 @@ protected static function formattingField($row, $field)
365377
{
366378
$newField = explode('.', $field);
367379
if (count($newField) == 1) {
368-
if(isset($row[$field])){
380+
if (isset($row[$field])) {
369381
return $row[$field];
370-
}else{
382+
} else {
371383
return false;
372384
}
373385
}
@@ -382,7 +394,7 @@ protected static function formattingField($row, $field)
382394

383395
return is_array($row) ? false : $row;
384396
}
385-
397+
386398
public static function curlGet($url)
387399
{
388400
$ch = \curl_init();
@@ -392,6 +404,7 @@ public static function curlGet($url)
392404
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 这个是重点 请求https。
393405
$data = \curl_exec($ch);
394406
\curl_close($ch);
407+
395408
return $data;
396409
}
397410
}

0 commit comments

Comments
 (0)