如何在laravel中处理Excel SUM公式

zbdgwd5y  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(246)

上面的图像是一个显示dd方法在laravel导入我的excel文件.问题是在processing它之后,其他列得到存储,但行23和25得到存储为0,但如果查看与microsoft excel软件它显示确切的数字,它假设.

  1. <?php
  2. namespace App\Imports;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\ToCollection;
  5. use DB;
  6. class SummaryImport implements ToCollection
  7. {
  8. /**
  9. * @param Collection $collection
  10. */
  11. public function collection(Collection $rows){
  12. unset($rows[0]);
  13. unset($rows[1]);
  14. unset($rows[2]);
  15. unset($rows[3]);
  16. unset($rows[4]);
  17. unset($rows[5]);
  18. unset($rows[6]);
  19. foreach ($rows as $key) {
  20. // dd($key);
  21. if($key[1] !== null){
  22. DB::table('summary_uploads')->insert(
  23. [
  24. 'lga' => $key[2],
  25. 'reg_voters' => $key[3],
  26. 'accred_voters' => $key[4],
  27. 'A' => $key[5],
  28. 'AA' => $key[6],
  29. 'AAC' => $key[7],
  30. 'ADC' => $key[8],
  31. 'ADP' => $key[9],
  32. 'APC' => $key[10],
  33. 'APGA' => $key[11],
  34. 'APM' => $key[12],
  35. 'APP' => $key[13],
  36. 'BP' => $key[14],
  37. 'LP' => $key[15],
  38. 'NNPP' => $key[16],
  39. 'NRM' => $key[17],
  40. 'PDP' => $key[18],
  41. 'PRP' => $key[19],
  42. 'SDP' => $key[20],
  43. 'YPP' => $key[21],
  44. 'ZLP' => $key[22],
  45. 'total_valid_votes' => $key[23],
  46. 'rejected_votes' => $key[24],
  47. 'total_votes_cast' => $key[25],
  48. ]
  49. );
  50. }
  51. }
  52. }
  53. }

有效投票总数投票总数始终存储为0

yuvru6vn

yuvru6vn1#

通过实现WithCalculatedFormulas,它解决了这个问题。

  1. <?php
  2. namespace App\Imports;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\ToCollection;
  5. use DB;
  6. use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
  7. class SummaryImport implements ToCollection,WithCalculatedFormulas
  8. {
  9. /**
  10. * @param Collection $collection
  11. */
  12. public function collection(Collection $rows){
  13. unset($rows[0]);
  14. unset($rows[1]);
  15. unset($rows[2]);
  16. unset($rows[3]);
  17. unset($rows[4]);
  18. unset($rows[5]);
  19. unset($rows[6]);
  20. foreach ($rows as $key) {
  21. if($key[1] !== null){
  22. DB::table('summary_uploads')->insert(
  23. [
  24. 'lga' => $key[2],
  25. 'reg_voters' => $key[3],
  26. 'accred_voters' => $key[4],
  27. 'A' => $key[5],
  28. 'AA' => $key[6],
  29. 'AAC' => $key[7],
  30. 'ADC' => $key[8],
  31. 'ADP' => $key[9],
  32. 'APC' => $key[10],
  33. 'APGA' => $key[11],
  34. 'APM' => $key[12],
  35. 'APP' => $key[13],
  36. 'BP' => $key[14],
  37. 'LP' => $key[15],
  38. 'NNPP' => $key[16],
  39. 'NRM' => $key[17],
  40. 'PDP' => $key[18],
  41. 'PRP' => $key[19],
  42. 'SDP' => $key[20],
  43. 'YPP' => $key[21],
  44. 'ZLP' => $key[22],
  45. 'total_valid_votes' => $key[23],
  46. 'rejected_votes' => $key[24],
  47. 'total_votes_cast' => $key[25],
  48. ]
  49. );
  50. }
  51. }
  52. }
  53. }

**$key[23]$key[25]**不再存储为0,而是存储为计算值。

感谢**Tobias k.**的评论,我只提供了一个更具解释性的示例

展开查看全部

相关问题