无法使用PHP更改Excel中的日期格式

时间:2016-02-12 09:34:21

标签: php excel

我使用class.writeexcel_worksheet.inc.php PHP类将大量(大约900行)数据写入.xls文件。写入excel的所有数据都被视为简单文本,甚至是日期和时间戳。

现在的问题是在excel中过滤日期时,几个月或几年都没有过滤器。由于所有单元格值都是简单文本。 有没有办法在通过PHP代码创建excel时格式化我的单元格到日期数据类型?

CODE:

<?php
    require_once "class.writeexcel_workbookbig.inc.php";
    require_once "class.writeexcel_worksheet.inc.php";
    require_once "db_params.php";
    if(isset($_POST['submit']))
    {
    $heure = date("H_i");
    $fname = tempnam("/tmp", $date."_".$heure.".xls");
    $workbook = &new writeexcel_workbookbig($fname);
    $worksheet = &$workbook->addworksheet();
    $mainheading =& $workbook->addformat();
    $mainheading->set_color('white');
    $mainheading->set_pattern(0x1);
    $mainheading->set_align('center');
    $mainheading->set_border_color('black');
    $mainheading->set_border(1);
    $mainheading->set_merge();
    $worksheet->write("A1", "Observation", $mainheading, $border2);
    $worksheet->set_column(0, 50, 18);
    $worksheet->freeze_panes(2, 0); # 1 row
    $worksheet->merge_cells("A1:L1");
    $heading =& $workbook->addformat();
    $heading->set_color('white');
    $heading->set_align('center');
    $heading->set_align('vcenter');
    $worksheet->set_column(0, 50, 18);  
    //$worksheet->set_outline();
    $worksheet->set_margins(2); 
    # Create a border format
    $border1 =& $workbook->addformat();
    $border1->set_color('black');
    $border1->set_pattern(0x1);
    $border1->set_fg_color('white');
    $border1->set_border_color('black');
    $border1->set_border(1);
    $border1->set_text_wrap();
    $col=0;
    $query = "SELECT * from Incidents";
    $res=sqlsrv_query($conn,$query,array(), array('Scrollable'=>SQLSRV_CURSOR_STATIC));
    $Entete1 = array("Incident ID - Hyperlink to AR" , "Reported Date", "Service", "Priority", "Operational  Categorization Tier 1", "Summary", "Assignment Date", "Status", "Last Assigned Group", "Resolution Status", "Time bef. first assignment", "Number of assignments");
    foreach($Entete1 as $value) {
    $worksheet->write(1, $col, $value,$heading);
    $col++;
    }
    $col=0;
    $Entete11 = array("IncidentID", "ReportDate", "Service", "Priority", "OperationalCategoryT1", "Summary", "AssignmentDate", "Status", "LastAssignedGroup", "ResolutionStatus", "AcknowledgmentTime", "NoOFAssignment", "Team", "Remarks", "RemarkCategory" , "PloneforgeNo" ,"ProdCategorisationT1", "ProdCategorisationT2", "ProdCategorisationT3", "Urgency", "Impact", "Resolved_date", "Category_tier1", "ResolutionDetails", "Name", "LastResolvedDate", "Resolution", "OLAStatus", "OLAUpTime", "ReopenedDate", "StatusReason", "BucketDate", "OLAProgress", "DueDateTime", "GoalTime");
    foreach($Entete11 as $value) 
    {
        $x=2;
        $y=2;
        for( $position = SQLSRV_SCROLL_FIRST ; ($row=sqlsrv_fetch_array($res, SQLSRV_FETCH_BOTH, $position)) ; $position = SQLSRV_SCROLL_NEXT )
        {   

            $chaine = strip_tags($row[$value]);

                $worksheet->write($x, $col, $chaine, $border1);
                $x++;   
        }
        $col++;

    }
    $workbook->close();
    header("Content-Type: application/x-msexcel; name=".$date."_".$heure.".xls");
    header("Content-Disposition: inline; filename=".$date."_".$heure.".xls");
    $fh=fopen($fname, "rb");
    fpassthru($fh);
    unlink($fname);
    }
    ?>

0 个答案:

没有答案