Excel正在从CSV文件中删除前导0
它相信Excel正在剥离领先的0。 有人告诉我,在导出期间将列格式更新为文本将修复此问题,并修改Excel输出(来自“ProofAndTracking actionIpromoteuAutomation”文件,但我找不到此文件或如何访问生成Excel格式代码的位置) 。
我从来没有使用过Excel。 我试过这个,但它似乎是一个本地修复: http : //excelribbon.tips.net/T010262_Handling_Leading_Zeros_in_CSV_Files.html有人可以指出我在正确的方向开始?
我已经研究了这些答案如何阻止从数据表导出为excel时被剥离的前导0? 和导出Excel:避免剥离前导零
提前致谢!
这是我相信造成问题的function,但不知道如何在这里格式化列的文本。 / * *格式并发送订单和跟踪信息(通过Excel电子表格)ipromoteu(150837)* /
public function actionIpromoteuAutomation() { $ordersGroup1 = $this->getIpromoteuProof(); $ordersGroup2 = $this->getIpromoteuProofHistory(); $orders = array_merge($ordersGroup1, $ordersGroup2); $fileName = 'Hit Promo Order Tracking ' . date('md-Y'); $this->_export($orders, $fileName); $fileName = $fileName . '.xls'; $toIPROMOTEU = array(//email to be sent to HR when request is submitted 'body' => 'Attached, please find the Excel spreadsheet containing' . ' order details for orders on ' . date('m/d/Y') . '.', 'from_email' => 'donotreply@hitpromo.net', 'from_name' => 'Hit Promotional Products', 'subject' => 'Order Tracking Information - ' . date('m/d/Y'), 'to_emails' => 'orders@ipromoteu.com', 'attachments' => array(array('path' => Yii::app()->basePath . '/../tmp/' . $fileName, 'filename' => $fileName)), ); Hit::email((object) $toIPROMOTEU); $this->logForDeveloper('Order records sent to iPromoteU for '. date('m/d/Y')); } private function _export($data, $fileName, $format = 'excel', $output = false) { // get a reference to the path of PHPExcel classes $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel'); // Turn off our yii library autoload spl_autoload_unregister(array('YiiBase', 'autoload')); include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); if ($format == 'excel') { // Set properties $objPHPExcel->getProperties()->setCreator(Yii::app()->user->name) ->setLastModifiedBy(Yii::app()->user->name) ->setTitle('Order Tracking Report'); } $styleArray = array( 'font' => array( 'bold' => true, 'underline' => true, ) ); $objPHPExcel->setActiveSheetIndex(0); $sheet = $objPHPExcel->getActiveSheet(); for ($rowCounter = 0; $rowCounter < sizeof($data); $rowCounter++) { $sheet->getStyle("A" . ($rowCounter + 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $sheet->getStyle("B" . ($rowCounter + 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $sheet->getStyle("C" . ($rowCounter + 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $sheet->setCellValue("A" . ($rowCounter + 1), date('m/d/Y', strtotime($data[$rowCounter]['date']))); //$sheet->setCellValue("A".($rowCounter+1), $data[$rowCounter]['date']); if ($data[$rowCounter]['fedex_tracking'] != '') $sheet->setCellValue("B" . ($rowCounter + 1), $data[$rowCounter]['fedex_tracking']); else $sheet->setCellValue("B" . ($rowCounter + 1), $data[$rowCounter]['other_tracking']); $sheet->setCellValue("C" . ($rowCounter + 1), $data[$rowCounter]['sales_order_number']); $sheet->getStyle("B" . ($rowCounter + 1))->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); } $sheet->setCellValue("A1", "DATE") ->setCellValue("B1", "TRACKING") ->setCellValue("C1", "PO NUMBER") ->getStyle("A1:C1")->applyFromArray($styleArray); ////Set the column widths $sheet->getColumnDimension("A")->setWidth(25); $sheet->getColumnDimension("B")->setWidth(25); $sheet->getColumnDimension("C")->setWidth(25); // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('Order Tracking Report'); // Set active sheet index to the first sheet, // so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); $fileName = $fileName . ($format == 'excel' ? '.xls' : '.csv'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $path = Yii::app()->basePath . '/../tmp/' . $fileName; $objWriter->save($path); // Once we have finished using the library, give back the // power to Yii... spl_autoload_register(array('YiiBase', 'autoload')); } public function getIpromoteuProof() { $date = date('Ymd'); $db2params = Yii::app()->params['db2params']; $db = Zend_Db::factory('Db2', $db2params); $select = $db->select() ->from( // table array('t' => 'WBPIPRAE'), // columns array( 'sales_order_number' => 'TRIM(WAESPO#)', ), // schema $db2params['schemas']['hitdta']) ->joinLeft( // table array('f' => 'MFD1MD'), 'CONCAT(TRIM(WAEORD#),RIGHT(TRIM(MDORDR),3))=TRIM(MDORDR)', // columns array( 'fedex_tracking' => 'TRIM(MDFTRK)', 'other_tracking' => 'TRIM(MDFBRC)', 'date' => 'TRIM(MDUPDT)', ), // schema $db2params['schemas']['varfil']) ->where("MDUPDT = '" . $date . "'") ->where("WAEIVKY='150837'") ->order('sales_order_number') ->distinct(true); $stmt = $db->query($select); $orders = $stmt->fetchAll(); return $orders; }
好的,Excel可以创buildcsv列作为text
但问题是与导入。
当Excel打开csv文件时,“帮助”将任何看起来像数字的东西都转换成数字,从而去掉前导零。
首先确保Excel正在导出它,然后在文本编辑器中打开该文件,并检查前导零是否存在。
您可能必须更改文件的导入方式。 而不是他们只需双击csv文件,他们需要运行数据导入向导:
导入“从文本”
select分隔符
确保逗号被打勾
select列作为文本
这应该解决另一方面的问题。
另一种方法是在数字之前添加一个单引号,但如果使用macros/软件与文件进行交互,则可能会破坏该文件。
编辑
看完PHP代码(我无法testing),您可能可以更改该行:
$sheet->getStyle("B" . ($rowCounter + 1))->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
至:
$sheet->getStyle("B" . ($rowCounter + 1))->getNumberFormat()->setFormatCode('000000000000'); // With however many zero's you need to keep
但是,这在导出时仍可能会被删除,因为它在技术上仍然是一个数字,格式将被删除。