经常碰到需要通过EXCEL往系统中上传数据的情况。

当然我们可以直接上传EXCEL,然后用PHP分析。但大家都知道,PHP分析EXCEL还是比较麻烦的!

不知道大家有没有想过直接把EXCEL粘贴到页面上,然后提交表单呢?

下面这段程序可以给大家提供个思路。

从EXCEL里复制部分单元格,在这个页面中点击“粘贴”按钮,EXCEL的内容就贴到网页上了。根据需要,可以修改下面的代码,在里面添加form或input,以达到直接生成表单的目的。

演示页面:http://www.enjoyphp.com/myfile/excel2web.html



<html>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<head>
<script language="javascript">
function clip() {
   
    var clipText = window.clipboardData.getData('Text');

   
    clipRows = clipText.split(String.fromCharCode(13));

   
    for (i=0; i<clipRows.length; i++) {
        clipRows[i] = clipRows[i].split(String.fromCharCode(9));
    }

   
    newTable = document.createElement("table")
    newTable.border = 1;
    for (i=0; i<clipRows.length - 1; i++) {

        newRow = newTable.insertRow();

        for (j=0; j<clipRows[i].length; j++) {
                newCell = newRow.insertCell();
                if (clipRows[i][j].length == 0) {
                        newCell.innerText = ' ';
                }
                else {
                        newCell.innerText = clipRows[i][j];
                }
        }
    }

    document.body.appendChild(newTable);
}
</script>
</head>
<body>
从EXCEL中复制一个表格,然后点击本页的“粘贴”按钮。<br/>
<input type="button" onclick="clip()" value='粘贴' />
</body>
</html>


  用PHP生成EXCEL的方法很多,当然我们可以用一些现成的类,像PHPEXCEL。但只用现成的类,就失去了学习的意义。下面介绍两个不用现成类的方法,代码清晰,方便大家学习。
参考原文略做修改


<?
//定义一些需要的函数
//如果需要可以放到单独的文件中去

function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}

function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

//开始正式的写输出

$result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")

//发送头
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$courseid-$sec.xls ");
header("Content-Transfer-Encoding: binary ");

//输出单元格

xlsBOF();
xlsWriteLabel(1,0,"Student Register $semester/$year");
xlsWriteLabel(2,0,"COURSENO : ");
xlsWriteLabel(2,1,"$courseid");
xlsWriteLabel(3,0,"TITLE : ");
xlsWriteLabel(3,1,"$title");
xlsWriteLabel(4,0,"SETION : ");
xlsWriteLabel(4,1,"$sec");
xlsWriteLabel(6,0,"NO");
xlsWriteLabel(6,1,"ID");
xlsWriteLabel(6,2,"Gender");
xlsWriteLabel(6,3,"Name");
xlsWriteLabel(6,4,"Lastname");
$xlsRow = 7;
while(list($id,$prename,$name,$sname,$grade)=mysql_fetch_row($result)) {
++$i;
xlsWriteNumber($xlsRow,0,"$i");
xlsWriteNumber($xlsRow,1,"$id");
xlsWriteLabel($xlsRow,2,"$prename");
xlsWriteLabel($xlsRow,3,"$name");
xlsWriteLabel($xlsRow,4,"$sname");
$xlsRow++;
}
xlsEOF();
exit();

?>