【发布时间】:2014-04-21 22:24:56
【问题描述】:
首先我有下面的mysql表(sold_items_records)如下
item_no qty price date time
x1 2 10 2014-4-21 2014-4-21 00:8:20
x2 4 12 2014-4-21 2014-4-21 00:8:20
x3 1 15 2014-4-22 2014-4-22 00:10:55
x1 2 11 2014-4-22 2014-4-22 01:11:40
x1 2 11 2014-4-22 2014-4-22 01:11:40
日期的结构是 (date),时间的结构是 (datetime)。
-
下一步就是根据时间将上表导出为Excel格式.xls!所以这是下面的PHP代码:
error_reporting(E_ALL); require_once ('Classes/PHPExcel.php'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $query = "SELECT * from sold_items_records ORDER BY time"; $result = mysql_query($query); while ( $row = mysql_fetch_assoc($result) ) { if ( !isset($curtime) || $curtime != $row['time'] ) { $curtime = $row['time']; $query2 = "SELECT item_no,qty,price,date,time from sold_items_records where time = '".$curtime."'"; $result2 = mysql_query($query2); //Excuting Values from Mysql to Excel $row2 = 2; // 1-based index while($row_data2 = mysql_fetch_assoc($result2)) { echo $row_data2['time']; $col2 = 0; //Row Headers $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Item No') ->setCellValue('B1', 'QTY') ->setCellValue('C1', 'Price') ->setCellValue('D1', 'Date') ->setCellValue('E1', 'Time'); //Row Values foreach($row_data2 as $key2=>$value2) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value2); $col2++; } $row2++; } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); //Check if File Name Exists $name = 'C:/Sales-'.date("d-m-Y",strtotime($row['time'])).'.xls'; $index = 1; while(file_exists($name)) { $name = $name.'--'.$index.".xls"; $index++; } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save($name); } }
需要的输出结果是有多个基于分组时间的 excel 文件...我的意思是有文件名为 A) (Sale-2014-4-21) B) (Sale-2014-4- 22) 和 (Sale-2014-4-22---1) 是的,两个日期相同,因为同一日期的时间不同。
打开每个excel文件时,文件A)显示正确的项目x1和x2......而文件B)它将显示X3 X1和X1......这不是我需要的,而是应该只显示 X3...文件 C 也是如此)结果将是 X1 X1 和 X3...它应该只显示 X1 和 X1...
老实说,这是一个具有挑战性的问题,我花了 5 天时间试图找出原因......幸运的是,我尝试使用替代方式作为 CSV 格式,结果是 100% 正确。但是当我回到 Excel 格式,时间似乎搞砸了,或者它可能忽略了时间。
所以我猜它在 Excel 中的时间格式,任何想法如何强制 excel 读取整个时间行?
谢谢
【问题讨论】:
-
你的主键是什么?
-
我没有看到任何日期或时间到 Excel 时间戳的转换,或任何单元格到日期或时间格式的格式....您在代码中的哪个位置执行此操作?
-
@MarkBaker 哦,我明白了,你能帮我更多地了解如何使用转换吗?谢谢
-
@Strawberry 我在 item_no 之前有自动递增 id 但它不会改变输出。
标签: php mysql excel time export