簡體   English   中英

PHP 表格轉 Excel 文件

[英]PHP table to Excel file

我不知道如何將 php 表導出到現有的 Excel 文件。
我是 php 新手,直到現在我都不明白我該怎么做。

從我的搜索中我看到它需要一段whileforeach語句。

<?php
$sql_data = date('d.m.Y', strtotime('-1days'));

require(realpath(dirname(__FILE__)."/PHPExcel-1.8/Classes/PHPExcel.php"));

    $conn = oci_connect('USER', 'PASS', 'dark:1521/DAR');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    Else {echo 'Connection successfully !';
    }

$parametri = oci_parse($conn, "SELECT * FROM TABLE_1 WHERE TO_DATE('${sql_data}','DD.MM.YYYY') BETWEEN T_FROM AND T_TILL");

oci_execute($parametri);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($parametri, OCI_ASSOC+OCI_RETURN_NULLS)) {

echo "<tr>\n";
foreach ($row as $item) {
    echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
}
    echo "</tr>\n";
}
echo "</table>\n";

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'UD_NAME');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'UD_CODE');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'TECH_TYPE');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'PPE_NAME');

$H_col = $objPHPExcel -> setActiveSheetIndex(0)->getHighestColumn();

$nr_col = PHPExcel_Cell::columnIndexFromString($H_col);
$nr_row = $objPHPExcel -> setActiveSheetIndex(0)->getHighestRow();

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));






?>
  function saveExcelFile($objPHPExcel, $file){
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: ' . $file . ';filename="01simple.xls"');
        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, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

試試這個而不是

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

嘗試運行 phpexcel,“header was sent error” 你不能在 headers 之前使用“echo”

require(realpath(dirname(__FILE__)."/PHPExcel-1.8/Classes/PHPExcel.php"));

function saveExcelFile($objPHPExcel, $file){
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: ' . $file . ';filename="01simple.xls"');
    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, 'Excel5');
    $objWriter->save('php://output');
    exit;
}

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPExcel->getActiveSheet();


$objWorksheet->SetCellValue('A1', 'UD_NAME');
$objWorksheet->SetCellValue('B1', 'UD_CODE');
$objWorksheet->SetCellValue('C1', 'TECH_TYPE');
$objWorksheet->SetCellValue('D1', 'PPE_NAME');

saveExcelFile($objPHPExcel, 'attachment'); 

我不知道下面的代碼是如何工作的,但單獨測試

$sql_data = date('d.m.Y', strtotime('-1days'));

$conn = oci_connect('USER', 'PASS', 'dark:1521/DAR');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
Else {echo 'Connection successfully !';
}

$parametri = oci_parse($conn, "SELECT * FROM TABLE_1 WHERE TO_DATE('${sql_data}','DD.MM.YYYY') BETWEEN T_FROM AND T_TILL"

oci_execute($parametri);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($parametri, OCI_ASSOC+OCI_RETURN_NULLS)) {

echo "<tr>\n";
foreach ($row as $item) {
    echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
}
    echo "</tr>\n";
}
echo "</table>\n";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM