PHP上传并读取Excel


注意!此方法上传的文件不能超过1000行,读取的文件不能超过2000行

共需要4个文件:
1.upload_page.html 上传页面
2.import_data.php 读取数据
3.reader.php
4.oleread.inc

其中,3和4是PHP-ExcelReader中的文件。PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader

以下为upload_page.html ,和import_functions.php。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<!--
	$Id: upload_page.html 2010-08-26  ZhangJiali $
-->
 
 
<meta charset="utf-8">
 
<form enctype="multipart/form-data" action="import_team_data.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
Send this file: <input name="xlsfile" type="file">
<input type="submit" value="Send File">
</form>
 
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/*
	$Id: import_team_data.php 2010-08-26 02:43:18 ZhangJiali $
*/
 
/*----------------------UploadFile--------------------*/
define('DOC_ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);
$uploaddir =DOC_ROOT.'./docs/';
$uploadfile = $uploaddir . $_FILES['xlsfile']['name'];
 
 
if (move_uploaded_file($_FILES['xlsfile']['tmp_name'], $uploadfile)) 
	{		
		//print "File is valid, and was successfully uploaded. ";
		//echo "<br/>";
		// Here's some more debugging info:\n";
		// print_r($_FILES);		
	} 
else 
	{		
		// print "Possible file upload attack!  Here's some debugging info:\n";
		//print_r($_FILES);		
	}
 
/*-------read the file ------------------*/
require_once ('reader.php');  
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
$data->read($uploadfile);
error_reporting(E_ALL ^ E_NOTICE);
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
	for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
		echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
	}
	echo "\n";
 
}
?>

One Comments to “PHP上传并读取Excel”

  1. 看来博主对PHP有很深的研究

Leave a Reply

(required)

(required)

:alien: :angel: :angry: :blink: :blush: :cheerful: :cool: :cwy: :devil: :dizzy: :ermm: :face: :getlost: :biggrin: :happy: :heart: :kissing: :lol: :ninja: :pinch: :pouty: :sad: :shocked: :sick: :sideways: :silly: :sleeping: :smile: :tongue: :unsure: :w00t: :wassat: :whistle: :wink: :wub: