【问题标题】:How to convert a html form into excel file?如何将html表单转换为excel文件?
【发布时间】:2019-07-24 12:13:54
【问题描述】:

我正在制作一个 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>

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我不使用 JS,但在 python 中,您有用于读取/写入 excel 文件的库。通常你会打开文件,填写信息,然后关闭文件。 Excel 库为您提供了排序 lib.write(row,col,data) 的函数。你检查过 JS 中现有的 excel 库吗?
  • 我还没试过,你能帮帮我吗?

标签: javascript excel html html-table ms-office


【解决方案1】:

你可以使用 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);

【讨论】:

  • 这用于将 html 表格转换为 csv 文件。 Excel 支持 csv 文件并将其转换为 excel。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 2014-08-19
  • 2016-06-07
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多