将表单数据写入xls文件

时间:2016-03-29 19:25:39

标签: php phpexcel

我只是想知道这是否可能,如果是的话,我可以得到一个起点。

现在,我正在寻找创建一个html表单,并将数据发布到excel电子表格。我将使用PHP来发布表单数据。表格将设置为2个表格。这两个表将标记为Shortage表,另一个表标记为Overage表。我有一个带有人名的下拉菜单。我想要完成的是将数据写入excel电子表格,并将短缺表中的值和过剩表中的值分开。例如,如果短缺表包含4个条目,并且超出包含2个条目,则在Excel电子表格中我希望单元格A1表示超量,第2行将包含列标题(即日期,描述,大小等)。 。),然后让第8行开始Overage,第9行将有标题,第10行将包含输入的值。虽然这并非总是如此,只是一个例子。

我的主要目标是让它以这种方式设置。每个人都将拥有专门为他们创建的工作表。此表单将每天使用,因此文件名将在那里有日期。因此,每天打开表单,一旦按下提交按钮,我想为包含2个表中数据的人创建一个工作表。例如,比尔史密斯是今天的第一个参赛作品。输入了2个表中的信息,我选择了提交按钮。在目录中,有一个标记为data03 / 29 / 16.xls的excel文件。在该excel工作簿中,工作表将包含标签/选项卡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文件后,这是主要目标。

Main output goal

编辑2: 实现代码后,这是我收到的输出。 Output screen

1 个答案:

答案 0 :(得分:1)

您需要做的第一件事是学习PHPEXCEL。第二种也是更快捷的方法是使用header()并在普通的html表中打印您的预期输出:

$this->app->bind(\Illuminate\Contracts\Auth\PasswordBroker::class, \App\Http\Controllers\Auth::class)

将此代码保存为excelout.php并运行它。你将获得excel的表格。希望这可能有助于开始。

CSV已更新且正在运行的版本:

<?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;
?>

除了以下内容之外,我不会改变你的形式:

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()">

你可以使用array_filter()检查$ _POST是否为空:

<input type="submit" value ="submit" name="mydrivers">

Demo