PHPExcel_IOFactory :: createWriter和Save花了一分多钟写一个文件

时间:2017-06-05 13:43:52

标签: php excel-2007

以下是我的代码。我将数据作为一个包含2个部分的字符串。第一部分是关于Excel工作表的标题。第二部分是关于实际数据。它们由特殊的分隔符分隔。

同样,第二部分(实际数据)用' \ n'对于每一行,每列再次用分隔符' |'。

分隔

现在,如果我点击下载,excel将在1分20秒后生成..请帮助我找到罪魁祸首,因为我是php的新手。

我在这里使用PHPExcel_IOFactory插件..

if(!empty($ json_decodeArr))         $ dataExists = 1;     其他         $ dataExists = 0;

$service_name = $params['service_name'];
$company_name = $params["company_name"];
    $language = $params["language"];

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array(' memoryCacheSize ' => '32MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

if (isset($GLOBALS[$service_name]["TEMPLATE_NAME"]) && $GLOBALS[$service_name]["TEMPLATE_NAME"] != '') {
    $template = $GLOBALS[$service_name]["TEMPLATE_NAME"];
    $ext = $GLOBALS[$service_name]["TEMPLATE_NAME_EXT"];
    $path = $GLOBALS[$service_name]["TEMPLATE_PATH"];
    $filepath = $path . $template.$language . "." . $ext;
    try {
        //load Excel template file
        $objPHPExcel = PHPExcel_IOFactory::load($filepath);
    } catch(Exception $e) {
        die('Error loading file ' . $e -> getMessage());
    }

} else {
    $template = $GLOBALS["TEMPLATE_BLANK"];
    $ext = $GLOBALS[$service_name]["TEMPLATE_BLANK_EXT"];
    $path = $GLOBALS["TEMPLATE_BLANKPATH"];
    $filepath = $path . $template . "." . $ext;
    try {
        //load Excel template file
        $objPHPExcel = PHPExcel_IOFactory::load($filepath);
    } catch(Exception $e) {
        die('Error loading file ' . $e -> getMessage());
    }
}

$temp_array = array();

//Named Configuration XLS Generation


//Writing Values into the excel sheet
$counter = 0;
$objPHPExcel -> setActiveSheetIndex(1);
$currentSheet = $objPHPExcel -> getActiveSheet();


foreach ($json_decodeArr as $key => $value) {
    if ($key == 0) {

        $headerNamesArr = explode("|",$GLOBALS[$service_name][$template][0]["HeaderNames"]);
        $headerExcelPositionArr = explode("|",$GLOBALS[$service_name][$template][0]["HeaderExcelPosition"]);
        $headerDataArr = explode("|",$value);
        foreach ($headerNamesArr as $headerName) {  
            $headerData = getHeaderValue($headerName,$headerDataArr);
            $currentSheet -> setCellValue(getHeaderValue($headerName,$headerExcelPositionArr), $headerData);                    
        }
        //file_put_contents("testResponse.txt", getHeaderValue('daysleft:',$headerDataArr));
    } else {

        //$activeWS = "B";
        $index = $GLOBALS[$service_name][$template][0]["ROW_START"];

        //file_put_contents("testResponse.txt", $GLOBALS[$service_name][$template][0]["COL_START"]);

        $rows = explode("\n", $value);

        foreach ($rows as $values1) {
            $activeWS = $GLOBALS[$service_name][$template][0]["COL_START"];
            $rowData = explode("|", $values1);
            //file_put_contents("testResponse.txt",$rowData);
            foreach ($rowData as $vals) {
                $WSName = $activeWS . $index;
                $currentSheet -> setCellValue($WSName, $vals);
                $activeWS++;
            }
            $index++;

        }
        $counter++;
    }
}

//$objPHPExcel->removeSheetByIndex($rowStart);
$objPHPExcel -> setActiveSheetIndex(0);

// Redirect output to a client.s web browser (Excel5)
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=" . $filename . ".xlsx");
header("Cache-Control: max-age=0");
// If you"re serving to IE 9, then the following may be needed
header("Cache-Control: max-age=1");

// If you"re serving to IE over SSL, then the following may be needed
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header("Cache-Control: cache, must-revalidate");
// HTTP/1.1
header("Pragma: public");
// HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
unset($objPHPExcel);
ob_start(); 
$objWriter -> save("php://output");
ob_flush();
exit ;

0 个答案:

没有答案