如何将html表单转换为excel文件?

问题描述 投票:-1回答:1

我正在制作一个html表单,并希望将表单转换为excel文件

<table style="border: 1px solid black;">
	<tr><td colspan="2"style="border: 1px solid black;"><text style="text-align:center;"><h4>A/U/S/H</h4></text></td></tr>
	<tr>
	<td rowspan="5" style="border: 1px solid black;"><h4 style="text-align:center;">code</h4></td>
	<td style="border: 1px solid black;">Ay-NIA/code of peripheral centre/ADR Number/Year</td>

	</tr>
	<tr>
		<td style="border: 1px solid black;">Ay-IPGT/code of peripheral centre/ADR Number/Year</td>

	</tr>
	<tr>
		<td style="border: 1px solid black;">
			Un-NIUM/code of peripheral centre/ADR Number/Year
		</td>
   </tr>
   </table>
javascript excel html5 html-table ms-office
1个回答
0
投票

你可以使用str_get_html http://simplehtmldom.sourceforge.net/

请查看以下示例:

include "simple_html_dom.php";
$table = '<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>';

$html = str_get_html($table);



header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');

$fp = fopen("php://output", "w");

foreach($html->find('tr') as $element)
{
    $td = array();
    foreach( $element->find('th') as $row)  
    {
        $td [] = $row->plaintext;
    }
    fputcsv($fp, $td);

    $td = array();
    foreach( $element->find('td') as $row)  
    {
        $td [] = $row->plaintext;
    }
    fputcsv($fp, $td);
}


fclose($fp);
© www.soinside.com 2019 - 2024. All rights reserved.