上载CSV时如何保留前导零

时间:2019-04-26 15:56:14

标签: csv phpexcel

我正在尝试将包含项目和数量列表的csv加载到我的网页中。但是,当将csv加载到phpexcel中时,它将在产品ID上删除前导零。如何为不同的csv大小保留单个列的前导零。我也在使用createReader('CSV')

我尝试使用setFormatCode,但收到一条错误消息,指出该函数不存在。即使在我包含文件之后。我也尝试过使用setCellValueExplicit,但它仍会删除前导零。

以下是正在处理的标头:

$objPHPExcel = $objReader->load($uploadFile);
$sheet = $objPHPExcel->getActiveSheet();
$validateDelimiter = $sheet->getCell('A1')->getValue();

这是处理非标头行的地方:

    //process rows
    if(isset($headers['asin/upc']) || isset($headers['asin']) || isset($headers['barcode/asin'])) {
        foreach($sheet->getRowIterator() as $row) {
          $cellIterator = $row->getCellIterator();
          foreach($cellIterator as $cell) {
            if(!$S_EW_Errors->Errors()) {
              //Set useful vars
              $column = $cell->getColumn();
              $columnIndex = PHPExcel_Cell::columnIndexFromString($column);
              $rowIndex = $row->getRowIndex();

              //grab header detail details
              if($rowIndex > 1) {
                //This code throws an error
                //$sheet->getStyle($cell->getCoordinate())->getNumberFormat()->setFormatCode('0000000000');
                //This code doesn't preserve leading zeros
                //$sheet->setCellValueExplicit('A'.$rowIndex, $sheet->getCell($asinColumn.$rowIndex)->getValue(), PHPExcel_Cell_DataType::TYPE_STRING);
                $asin = trim($sheet->getCell($asinColumn.$rowIndex)->getValue());
                //see if this needs explored or not
                $detailArr[$asin]['explored'] = $PoMgr->asinExists($asin);
                if(isset($columnMappings[$column])) {
                  if($columnMappings[$column] == 'asin' || $columnMappings[$column] == 'asin/upc' || $columnMappings[$column] == 'barcode/asin') {
                    //stringify asin
                    $detailArr[$asin][$columnMappings[$column]] = trim($sheet->getCell($column.$rowIndex)->getValue());
                  } else if ($columnMappings[$column] == 'unit_price' || $columnMappings[$column] == 'buy cost') {
                    //replace $ values
                    $detailArr[$asin]['unit_price'] = trim(str_replace('$', '', $sheet->getCell($column.$rowIndex)->getValue()));
                  } else if ($columnMappings[$column] == 'quantity') {
                    if(isset($detailArr[$asin]['quantity'])) {
                      $detailArr[$asin]['quantity'] += trim($sheet->getCell($column.$rowIndex)->getValue());
                    } else {
                      $detailArr[$asin][$columnMappings[$column]] = trim($sheet->getCell($column.$rowIndex)->getValue());
                    }
                  } else if ($columnMappings[$column] == 'title') {
                    $detailArr[$asin]['name'] = trim($sheet->getCell($column.$rowIndex)->getValue());
                  } else {
                    //special case for android csv
                    if (!isset($asin) && $columnMappings[$column] == 'productid') {
                      $asin = trim($sheet->getCell('G'.$rowIndex)->getValue());
                    } 
                    $detailArr[$asin][$columnMappings[$column]] = trim($sheet->getCell($column.$rowIndex)->getValue());
                  }
                }

              } //index > 1
            } //no errors
          } //foreach as cell
          if($rowIndex > 1) {
            if(!empty($vendorId)) $detailArr[$asin]['vendor_id'] = $vendorId;
            if(!empty($affiliateId)) $detailArr[$asin]['affiliate_id'] = $affiliateId;
            if(!empty($accountId)) $detailArr[$asin]['account_id'] = $accountId;
            if(!empty($userId)) $detailArr[$asin]['user_id'] = $userId;
          }
        } //foreach as row
    }

我已经评论过我以前尝试过的台词。

所有ID均为10个字符,因此这是网页所期望的,并且需要这些ID准确无误才能创建发货计划。但是,如果它们以0开头,则页面将创建具有9个字符ID的计划。

0 个答案:

没有答案