繁体   English   中英

我们如何导出excel表格中的数据显示在不同的列中

[英]how can we export data in excel sheet date show in different column

我们如何导出Excel工作表中的数据显示在不同的列中

代码 :

error_reporting(0);
ini_set('display_errors',0);
error_reporting(E_ALL);
include('include/function.php');
include('include/report_header.php');

$user=new User();

session_start();

$sql= "SELECT enginner_name,in_time,out_time FROM `enginner_timesheet`  ";          
$results = mysql_query($sql);



$sql=$_SESSION["sql"];

$fdate=$_SESSION["fdate"];

$todate=$_SESSION["todate"];


$begin =  new DateTime($fdate. " 00:00:00");
$end = new DateTime($todate. " 23:59:59");

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$count=0;
$new1='';

foreach ( $period  as $dt ) {
    $new=$dt->format("Y-m-d");
    $new1.=" ' ".$new." ' , ";
}

$new1=substr($new1, 0, -1);
//echo $new1;

date_default_timezone_set('Asia/Calcutta');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; 
filename=pikaObs_'.date("Ymd").'.csv');

$handle = fopen('php://output', 'w');

fputcsv($handle, array('Engineer_Name','in_time','out_time',$new1));

while($row=mysql_fetch_array($results)) {

    fputcsv($handle, array($row['enginner_name'],$row['in_time'],$row['out_time']))
}

fclose($handle);

由于in-time和out-time都在一天之内,所以最好用一列。 所以我使用 concat() 将两列组合起来。

 //Here we grab date from and to from form
$fdate=$_SESSION["fdate"];//suppose it is in yyyy-mm-dd format
$todate=$_SESSION["todate"];

$db = new PDO('mysql:host=localhost;dbname=test', 'root', 'pass');
$q = $db->query("SELECT id, enginner_name,  `date` as datework, 
                 concat(in_time, '-', out_time) as timespent
                 FROM `enginner_timesheet` 
                 WHERE `date` BETWEEN '$fdate' AND '$todate' 
                 GROUP BY enginner_name, `date`");
$sub=array();
$eng=array();
while($row=$q->fetch(PDO::FETCH_ASSOC)){
    $sub[$row['id']][$row['datework']] = $row['timespent'];
    $eng[$row['id']]= $row['enginner_name'];
}
$subkey=key($sub);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="yourexcel.xls"');
header('Pragma: no-cache');
header('Expires: 0');

echo '<table>';
echo '<tr>';
echo '<th>Engineer name</th>';
foreach($sub[$subkey] as $keys=>$vals){ 
        echo '<th>'.formatDate($keys).'</th>';//date column
}
echo '</tr>';
foreach($sub as $key=>$val){
       echo '<tr>';
        echo '<td>'.$eng[$key].'</td>';//engineer name
        foreach($val as $v){
            echo '<td>'.$v.'</td>';//time loop
        }
    echo '</tr>';
}
echo '</table>';
function formatDate($d){
   $date = new DateTime($d);    
   return $date->format('d/D');
}
?>

您正在使用旧的已弃用的 mysql 扩展。 在这个答案中,我使用 PDO 是因为我觉得它更适合你。 您甚至可以使用更安全的准备好的语句。 由于 phpfiddle 不支持 concat() 函数,我无法在线演示。 只是测试看看。 我已经在我的本地机器上测试过,它工作正常。 注意:mysql 扩展已弃用。 使用 PDO 或 myqli。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM