get_results( $query, ARR-6ren">
gpt4 book ai didi

php - 使用 PHPExcel 脚本时如何以日期格式格式化某些列

转载 作者:行者123 更新时间:2023-12-04 20:41:23 26 4
gpt4 key购买 nike

使用 PHPExcel 脚本时出现日期格式问题

我有从 wpdb 查询生成 xlsx 文件的代码:

<?php
# Load slim WP
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );
# http://phpexcel.codeplex.com/
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
global $wpdb;
$query = "Select
tsales_funnel.ID As ID,
wp_users.display_name As Darijuma_vaditajs,
tcportal_starpnieks.Cp_Name As Starpnieks,
tcportal_stucture.Cp_Name As OWCA,
n_products.Product_Nos As Produkts,
tsales_funnel_mrecord.Product_type as Produkta_kods,
tsales_funnel.Sanems_date as Saņēmšanas_datums,
tsales_funnel_mrecord.Deadline As Deadline,
n_sf_statusi.nosaukums_lv As Statuss,
tsales_funnel_clients.Reg_nr As Klienta_Regnr,
tfirmas_reg.name_in_quotes As Klients,
tsales_funnel_mrecord.Faze_date as Faze_date,
n_sf_fazes.nosaukums_lv As Faze,
tsales_funnel_mrecord.Summa As Apdrošīnājuma_summa,
tsales_funnel_mrecord.Vien_skaits As TRL_skaits,
tsales_funnel_mrecord.Compensa_cena,
tsales_funnel_mrecord.Tirgus_cena,
wp_users02.display_name As Riska_parakstitajs,
comm.Comment as Aizveršanas_komentārs
From
tsales_funnel Left Join
tsales_funnel_mrecord On tsales_funnel.ID = tsales_funnel_mrecord.Funnel_ID
Left Join
tcportal_starpnieks On tcportal_starpnieks.Cp_code = tsales_funnel.Starpnieks
Left Join
tcportal_stucture On tcportal_stucture.Cp_code = tsales_funnel.OWCA Left Join
tsales_funnel_clients On tsales_funnel_clients.Funnel_ID = tsales_funnel.ID
Left Join
tfirmas_reg On tfirmas_reg.regcode = tsales_funnel_clients.Reg_nr Left Join
wp_users On tsales_funnel.Darijuma_vaditajs = wp_users.user_login Left Join
n_sf_statusi On n_sf_statusi.id = tsales_funnel.Statuss
Left Join n_sf_fazes on tsales_funnel_mrecord.Product_faze = n_sf_fazes.id
Left Join
n_products On tsales_funnel_mrecord.Product_type = n_products.Product_Code
Left Join
(SELECT * FROM tsales_funnel_comments WHERE Comm_type = 4) as comm On tsales_funnel.ID = comm.Funnel_ID
Left Join
wp_users As wp_users02
On wp_users02.user_login = tsales_funnel_mrecord.Risk_acceptance
WHERE
tsales_funnel_clients.Tips_Galvenais = 1
";
$error = "Error: the query failed...
<pre style='width:700px;word-wrap:break-word;white-space:normal;'>$query</pre>";
$result = $wpdb->get_results( $query, ARRAY_A ) or wp_die( $error );
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("user")
->setLastModifiedBy("user")
->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");
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 0;
// Sheet cells
$cell_definition = array(
'A' => 'ID',
'B' => 'Darijuma_vaditajs',
'C' => 'Starpnieks',
'D' => 'OWCA',
'E' => 'Produkts',
'F' => 'Produkta_kods',
'G' => 'Saņēmšanas_datums',
'H' => 'Deadline',
'I' => 'Statuss',
'J' => 'Klienta_Regnr',
'K' => 'Klients',
'L' => 'Faze_date',
'M' => 'Faze',
'N' => 'Apdrošīnājuma_summa',
'O' => 'TRL_skaits',
'P' => 'Compensa_cena',
'Q' => 'Tirgus_cena',
'R' => 'Riska_parakstitajs',
'S' => 'Aizveršanas_komentārs'
);
// Build headers
foreach( $cell_definition as $column => $value )
$objPHPExcel->getActiveSheet()->setCellValue( "{$column}1", $value );
// Build cells
while( $rowCount < count($result) ){
$cell = $rowCount + 2;
foreach( $cell_definition as $column => $value )
$objPHPExcel->getActiveSheet()->setCellValue($column.$cell, $result[$rowCount][$value]);

$rowCount++;
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="iPortal_Atskaite_'.date('Y-m-d_H.i.s', strtotime('+3 hour')).'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

我收到的日期列为 YY-MM-DD,Excel 不支持。
但我需要:
'G' => 'Saņēmšanas_datums',

格式 (YYYY.MM.DD) 喜欢:2015.12.31

对于:
'H' => 'Deadline', and 'L' => 'Faze_date',

我需要日期格式 (YYYY.MM.DD HH:MM:SS) 比如 2015.12.31 15:31:22

我怎样才能做到这一点?我试图在互联网上找到一些东西,但没有找到。

最佳答案

将您从数据库中获取的日期设为 YY-MM-DD , 并将其转换为 PHP DateTime目的

$dateTimeObject = DateTime::createFromFormat('y-m-d', <date value from database row>, new DateTimeZone('UTC'));

然后将其转换为 MS Excel 序列化时间戳
$excelDate = PHPExcel_Shared_Date::PHPToExcel($dateTimeObject);

最后,将该值写入单元格,并为该单元格设置格式掩码
$objPHPExcel->getActiveSheet()
->setCellValue(<cell address>, $excelDate);
$objPHPExcel->getActiveSheet()
->getStyle(<cell address>)
->getNumberFormat()
->setFormatCode('yyyy.mm.dd');

同样适用于日期/时间值

关于php - 使用 PHPExcel 脚本时如何以日期格式格式化某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com