0

我正在尝试使用 excel 数据创建描述构建器。我想在 HTML 模板中使用预定义的值,并希望将 excel 值放在出现预定义值的位置,例如

<table>
  <tr>
     <td>ISBN</td>
     <td>{ISBN}</td>
  </tr>
  <tr>
     <td>Publisher</td>
     <td>{Publisher}</td>
  </tr>
  <tr>
     <td>Year</td>
     <td>{Year}</td>
  </tr>
  <tr>
     <td>About Book</td>
     <td>{AboutBook}</td>
  </tr>
</table>

HTML 模板可以从物理 HTML 文件或文本区域传递。我希望用 excel 数据替换花括号中的文本。excel 文件中的标题与 HTML 模板中的值相同,但 excel 文件每次都可以更改,其标题也可以更改,excel 文件中可能有更多标题,其中只有少数包含在 HTML 模板中。我正在使用 excelreader 从 excel 文件中获取数据

for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
    $xlshead[$j] = trim($data->sheets[0]['cells'][1][$j]);
}

foreach($xlshead as $hkey=>$hval) {
        $row[$hkey] = trim($hval);
    }

现在我得到了数组 $row 键中的列号和数组 $row 值中的标题

我在用

for ($j = 1; $j <= $data->sheets[0]['numRows']-1; $j++) {
       str_replace(array_valyes($row),$data->sheets[0]['cells'][$j+1][array_$keys($row)],$_POST['strhtmltemplate'])
}

不,我不知道如何在替换功能中获得确切的密钥。请帮我。

4

1 回答 1

1

这是我为我的项目完成的完整代码

$data = new Spreadsheet_Excel_Reader();
$filename = $_SESSION['filename'];
$data->read(ROOT.$filename);

$xlshead = array();

for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
    $xlshead[$j] = trim($data->sheets[0]['cells'][1][$j]);
}

$TotalRecord = $data->sheets[0]['numRows']-1;
$row = array();

foreach($xlshead as $hkey=>$hval) {
    $row['<?php echo $data->sheets[0][\'cells\'][$j+1]['.$hkey.']; ?>'] = "{".trim($hval)."}";
}

function _loadFile($filename)
{
    $contents = '';
    if ( file_exists($filename ) )
    {
        ob_start();
        require_once $filename;
        $contents = ob_get_contents();
        ob_end_clean();
    }
    return $contents;
}

$datafile = session_id();
$writedata = "";

$writedata .= str_replace(array_values($row),array_keys($row),$_SESSION['template']);

$myFile = $datafile.".php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $writedata);
fclose($fh);

$showdata = _loadFile("code.php");
$_SESSION['finaldata'] = $showdata;

代码.php

require_once 'Excel/reader.php';

$datafile = session_id();

if(isset($_SESSION['filename'])) {
    $data = new Spreadsheet_Excel_Reader();
    $filename = $_SESSION['filename'];
    $data->read(ROOT.$filename);

    for ($j = 1; $j <= $data->sheets[0]['numRows']-1; $j++) {
        include($datafile.".php");
        echo '--br--';
    }
}

最后将文件导出到excel

<?php
session_start();
$filename ="Downloaded.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

$dataarr = explode("--br--",$_SESSION['finaldata']);

?>
<table>
<?php 

$dataarr = explode("--br--",$_SESSION['finaldata']);

foreach($dataarr as $res) {
    echo "<tr><td>".htmlentities($res)."</td></tr>";
}
?>
</table>
于 2012-07-20T07:26:50.453 回答