将表单数据写入xls文件
write form data to xls file
我只是想知道这是否可行,如果可行,我能得到一个起点吗?
现在,我想创建一个 html 表单,并将 POST 数据添加到 excel 电子表格中。我将使用 PHP 到 POST 表单数据。表单将设置为 2 tables。 2 个 table 将标记为短缺 table,另一个标记为超额 table。我有一个带有人名的下拉菜单。我想要完成的是将数据写入 excel 电子表格并将值与短缺 table 和超额值 table 分开。例如,如果短缺 table 包含 4 个条目,而超额包含 2 个条目,在 excel 电子表格中,我希望单元格 A1 显示超额,第 2 行将包含列 headers(即日期、描述、尺寸等),然后第 8 行开始显示 Overage,第 9 行将显示 headers,第 10 行将包含输入的值。当然,情况并非总是如此,这只是一个例子。
我的主要目标是以这种方式设置它。每个人都会有一个专门为他们创建的工作表。该表格将每天使用,因此文件名中将包含日期。所以每天都会打开表单,一旦按下提交按钮,我想为那个人创建一个工作表,其中包含 2 table 中的数据。例如,Bill Smith 是今天的第一个条目。来自 2 table 的信息已输入,我 select 提交按钮。在该目录中,有一个标记为 data03/29/16.xls 的 excel 文件。在该 excel 工作簿中,工作表将包含 label/tab Bill Smith。
这是否可能,或者将表单数据写入 CSV 文件会更容易、更高效吗?
编辑:
<!DOCTYPE HTML>
<html>
<head>
<title> Drive Check In </title>
<script type = "text/javascript">
function reset()
{
var driver = document.getElementById("drivers");
var date = docuemt.getElementById("dates");
var custnum = document.getElementById("customernum");
var invnum = document.getElementById("invoicenum");
var proddesc = document.getElementById("proddes");
var sz = document.getElementById("size");
var cs = document.getElementById("cs");
var btls = document.getElementById("btls");
var chkint = document.getElementById("chkitls");
driver.value.reset();
date.value.reset();
custnum.value.reset();
}
</script>
</head>
<body>
<form action = '' method = "post">
<table id = "shortages" >
<h1> Shortages </h1>
<thead>
<tr>
<th> Driver </th>
<th> Date </th>
<th> Customer# </th>
<th> Invoice# </th>
<th> Product Description </th>
<th> Size </th>
<th> CS </th>
<th> Btls </th>
<th> CHK Itls </th>
</tr>
</thead>
<tr>
<td>
<select id = "drivers" name = 'drivers'>
<option value = " "> </option>
<option value = "Driver1"> Driver1 </option>
<option value = "Driver2"> Driver2 </option>
<option value = "Driver3"> Driver3 </option>
</select></td>
<td> <input type = "text" id = "dates"size = "10" name = "dates"> </input> </td>
<td> <input type = "number" min = "1" max = "99999999" id = "customernum" name = "customernum"> </input> </td>
<td> <input type = "number" min = "1" max = "99999999" id = "invoicenum" name = "invoicenum"> </input> </td>
<td> <input type = "text" id = "proddes" name = "proddes"> </input> </td>
<td> <input type = "number" id = "size" name = "size"> </input> </td>
<td> <input type = "number" id = "cs" name = "cs"> </input> </td>
<td> <input type = "number" id = "btls" name = "btls"> </input> </td>
<td> <input type = "text" id = "chkitls" name = "chkitls"> </input> </td>
</tr>
</table>
<input type = "submit" value = "submit" name = "mydrivers>
<?php
function myfputcsv($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
if(isset($_POST['mydrivers'])) {
$header=array();
$data=array();
foreach (array_slice($_POST,0,count($_POST)-1) as $key => $value) {
//$header[]=$key;
$data[]=$value;
}
$fp = fopen('driver.csv', 'a+');
//fputcsv($fp, $header);
myfputcsv($fp, $data);
fclose($fp);
}
?>
</form>
</html>
这是我目前拥有的 php 文件。我在没有实施你的建议的情况下使用它,但我会实施它们。
这是我的主要产出目标。 cvs 文件打开并格式化后,这就是主要目标。
编辑 2:
实施您的代码后,这是我收到的输出。 Output screen
您需要做的第一件事是学习 PHPEXCEL. The second and quicker way is to use header() 并以正常 html 打印您的预期输出 table:
<?php
$col1row = $_POST['col1row1'];//Getting data from form
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="16.xls"');
header('Pragma: no-cache');
header('Expires: 0');
/*Saving purpose
**$output= @fopen('filename', 'w'); //error turn off
**$output = fopen('php://output', 'w');
*/
//HTML table will go here
$table = '<table><tr>';
$table .= '<th>Column 1</th>';
$table .= '<th>Column 2</th>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>'.$col1row1.'</td>';
$table .= '<td>'.$col2row1.'</td>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>'.$col1row2.'</td>';
$table .= '<td>'.$col2row2.'</td>';
$table .= '</tr>';
$table .= '</table>';
echo $table;
?>
将此代码保存为 excelout.php 和 运行。您将在 excel 中获得 table。希望这对入门有所帮助。
CSV 更新和工作版本:
function myfputcsv($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
if(isset($_POST['mydrivers'])) {
$header=array();
$data=array();
foreach (array_slice($_POST,0,count($_POST)-1) as $key => $value) {
//$header[]=$key;
$data[]=$value;
}
$fp = fopen('driver.csv', 'a+');
//fputcsv($fp, $header);
myfputcsv($fp, $data);
fclose($fp);
}
除了以下内容外,我没有对您的表格做太多更改:
<form action = 'driver.php' target = 'driver.php' method = "post">
到
<form action="" method ="post">
和
<input type = "submit" value = "submit" onsubmit = "this.reset()">
到
<input type="submit" value ="submit" name="mydrivers">
您可以使用 array_filter():
检查 $_POST 是否为空
$valid= array_filter($_POST);
if (!empty($valid)) {
//all fields are furnished and can be saved.
}
我只是想知道这是否可行,如果可行,我能得到一个起点吗?
现在,我想创建一个 html 表单,并将 POST 数据添加到 excel 电子表格中。我将使用 PHP 到 POST 表单数据。表单将设置为 2 tables。 2 个 table 将标记为短缺 table,另一个标记为超额 table。我有一个带有人名的下拉菜单。我想要完成的是将数据写入 excel 电子表格并将值与短缺 table 和超额值 table 分开。例如,如果短缺 table 包含 4 个条目,而超额包含 2 个条目,在 excel 电子表格中,我希望单元格 A1 显示超额,第 2 行将包含列 headers(即日期、描述、尺寸等),然后第 8 行开始显示 Overage,第 9 行将显示 headers,第 10 行将包含输入的值。当然,情况并非总是如此,这只是一个例子。
我的主要目标是以这种方式设置它。每个人都会有一个专门为他们创建的工作表。该表格将每天使用,因此文件名中将包含日期。所以每天都会打开表单,一旦按下提交按钮,我想为那个人创建一个工作表,其中包含 2 table 中的数据。例如,Bill Smith 是今天的第一个条目。来自 2 table 的信息已输入,我 select 提交按钮。在该目录中,有一个标记为 data03/29/16.xls 的 excel 文件。在该 excel 工作簿中,工作表将包含 label/tab Bill Smith。
这是否可能,或者将表单数据写入 CSV 文件会更容易、更高效吗?
编辑:
<!DOCTYPE HTML>
<html>
<head>
<title> Drive Check In </title>
<script type = "text/javascript">
function reset()
{
var driver = document.getElementById("drivers");
var date = docuemt.getElementById("dates");
var custnum = document.getElementById("customernum");
var invnum = document.getElementById("invoicenum");
var proddesc = document.getElementById("proddes");
var sz = document.getElementById("size");
var cs = document.getElementById("cs");
var btls = document.getElementById("btls");
var chkint = document.getElementById("chkitls");
driver.value.reset();
date.value.reset();
custnum.value.reset();
}
</script>
</head>
<body>
<form action = '' method = "post">
<table id = "shortages" >
<h1> Shortages </h1>
<thead>
<tr>
<th> Driver </th>
<th> Date </th>
<th> Customer# </th>
<th> Invoice# </th>
<th> Product Description </th>
<th> Size </th>
<th> CS </th>
<th> Btls </th>
<th> CHK Itls </th>
</tr>
</thead>
<tr>
<td>
<select id = "drivers" name = 'drivers'>
<option value = " "> </option>
<option value = "Driver1"> Driver1 </option>
<option value = "Driver2"> Driver2 </option>
<option value = "Driver3"> Driver3 </option>
</select></td>
<td> <input type = "text" id = "dates"size = "10" name = "dates"> </input> </td>
<td> <input type = "number" min = "1" max = "99999999" id = "customernum" name = "customernum"> </input> </td>
<td> <input type = "number" min = "1" max = "99999999" id = "invoicenum" name = "invoicenum"> </input> </td>
<td> <input type = "text" id = "proddes" name = "proddes"> </input> </td>
<td> <input type = "number" id = "size" name = "size"> </input> </td>
<td> <input type = "number" id = "cs" name = "cs"> </input> </td>
<td> <input type = "number" id = "btls" name = "btls"> </input> </td>
<td> <input type = "text" id = "chkitls" name = "chkitls"> </input> </td>
</tr>
</table>
<input type = "submit" value = "submit" name = "mydrivers>
<?php
function myfputcsv($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
if(isset($_POST['mydrivers'])) {
$header=array();
$data=array();
foreach (array_slice($_POST,0,count($_POST)-1) as $key => $value) {
//$header[]=$key;
$data[]=$value;
}
$fp = fopen('driver.csv', 'a+');
//fputcsv($fp, $header);
myfputcsv($fp, $data);
fclose($fp);
}
?>
</form>
</html>
这是我目前拥有的 php 文件。我在没有实施你的建议的情况下使用它,但我会实施它们。
这是我的主要产出目标。 cvs 文件打开并格式化后,这就是主要目标。
编辑 2: 实施您的代码后,这是我收到的输出。 Output screen
您需要做的第一件事是学习 PHPEXCEL. The second and quicker way is to use header() 并以正常 html 打印您的预期输出 table:
<?php
$col1row = $_POST['col1row1'];//Getting data from form
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="16.xls"');
header('Pragma: no-cache');
header('Expires: 0');
/*Saving purpose
**$output= @fopen('filename', 'w'); //error turn off
**$output = fopen('php://output', 'w');
*/
//HTML table will go here
$table = '<table><tr>';
$table .= '<th>Column 1</th>';
$table .= '<th>Column 2</th>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>'.$col1row1.'</td>';
$table .= '<td>'.$col2row1.'</td>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>'.$col1row2.'</td>';
$table .= '<td>'.$col2row2.'</td>';
$table .= '</tr>';
$table .= '</table>';
echo $table;
?>
将此代码保存为 excelout.php 和 运行。您将在 excel 中获得 table。希望这对入门有所帮助。
CSV 更新和工作版本:
function myfputcsv($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
if(isset($_POST['mydrivers'])) {
$header=array();
$data=array();
foreach (array_slice($_POST,0,count($_POST)-1) as $key => $value) {
//$header[]=$key;
$data[]=$value;
}
$fp = fopen('driver.csv', 'a+');
//fputcsv($fp, $header);
myfputcsv($fp, $data);
fclose($fp);
}
除了以下内容外,我没有对您的表格做太多更改:
<form action = 'driver.php' target = 'driver.php' method = "post">
到
<form action="" method ="post">
和
<input type = "submit" value = "submit" onsubmit = "this.reset()">
到
<input type="submit" value ="submit" name="mydrivers">
您可以使用 array_filter():
检查 $_POST 是否为空$valid= array_filter($_POST);
if (!empty($valid)) {
//all fields are furnished and can be saved.
}