php读取excel,以及php打包文件夹为zip文件
1.把文件下载到本地,放在在Apache环境下
2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码
3.访问zip.php的功能是把生成的files文件夹打包成files.zip
4.访问index.php即可生成files文件夹,里面0.js---n.js 分别存放各个服务器人名,server_name_list.js存放服务器列表。
5.Classes 存放的是php读取excel的功能模块,具体任务逻辑都在index.php A.PHP读取excel支持excel2007 demo逻辑代码:其中的(arrayRecursive,JSON方法是json数据处理功能,可兼容汉字)
主要借助了:PHPExcel插件,附件中有Classes文件夹,官网:http://www.codeplex.com/PHPExcel
index.php
<?php
/** Error reporting */
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
</head> <body>
<?php
/**************************************************************
*
* 使用特定function对数组中所有元素做处理
* @param string &$array 要处理的字符串
* @param string $function 要执行的函数
* @return boolean $apply_to_keys_also 是否也应用到key上
* @access public
*
*************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
} if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
} /**************************************************************
*
* 将数组转换为JSON字符串(兼容中文)
* @param array $array 要转换的数组
* @return string 转换得到的json字符串
* @access public
*
*************************************************************/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
} require_once 'Classes\PHPExcel.php';
require_once 'Classes\PHPExcel\IOFactory.php';
require_once 'Classes\PHPExcel\Reader\Excel2007.php';
$uploadfile='d.xlsx'; $objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/
$objPHPExcel = PHPExcel_IOFactory::load($uploadfile);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数 /*方法【推荐】*/
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // 取得总行数
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
$list = array();
for ($row = 1;$row <= $highestRow;$row++) {
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++) {
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
array_push($list, $strs);
}
//读取完毕 $list
//处理数据,生成新的结构
$n = 0;
$ser = array();
for($i = 0 ; $i < count($list); $i++ ){
$ser[$n][0] = $list[$i][0];
if(!is_array(@$ser[$n][1])){
$ser[$n][1] = array();
}
array_push($ser[$n][1], $list[$i][1]);
if($i != count($list) -1){
if($list[$i][0] != $list[$i+1][0]){
$n++;
}
}
}
/*输出文件*/
$sname = array();
$f = 'files/';//存放目录
if (! file_exists ( $f )) {
mkdir ( $f );
}
for($j = 0;$j < count($ser); $j++){
$file = $f.$j.'.js';
echo $file."<br />";
$fp=fopen("$file", "w+"); //打开文件指针,创建文件
if ( !is_writable($file) ){
die("文件:" .$file. "不可写,请检查!");
}
if (is_writable($file) == false) {
die('我是鸡毛,我不能');
}
$data = $ser[$j][1];
array_push($sname, $ser[$j][0]);
file_put_contents ($file, JSON($data));
fclose($fp); //关闭指针
}
$file = $f.'server_name_list.js';
echo $file."<br />";;
$fp=fopen("$file", "w+"); //打开文件指针,创建文件
if ( !is_writable($file) ){
die("文件:" .$file. "不可写,请检查!");
}
if (is_writable($file) == false) {
die('我是鸡毛,我不能');
}
file_put_contents ($file, JSON($sname));
echo "生成完毕!";
echo '<a href="zip.php">打包生成文件</a>'
?>
</body>
</html>
B.PHP打包文件夹为zip文件
zip.php
<?php
/** Error reporting */
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
</head> <body>
<?php
function addFileToZip($path,$zip){
$handler=opendir($path); //打开当前文件夹由$path指定。
while(($filename=readdir($handler))!==false){
if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
addFileToZip($path."/".$filename, $zip);
}else{ //将文件加入zip对象
$zip->addFile($path."/".$filename);
}
}
}
@closedir($path);
} $zip=new ZipArchive();
if($zip->open('files.zip', ZipArchive::OVERWRITE)=== TRUE){
addFileToZip('files', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
}
echo '打包完毕!'."<br />";
echo "<a href='files.zip'>下载files.zip</a>"
?>
</body>
</html>
代码下载:php-read-excel
http://files.cnblogs.com/zhidong123/php-read-excel.zip
php读取excel,以及php打包文件夹为zip文件的更多相关文章
- php打包文件夹成zip文件
function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. while(($filenam ...
- java打包文件夹为zip文件
//待压缩的文件目录 String sourceFile=sourceFilePath+"\\"+userName; //存放压缩文件的目录 String zipFilePath ...
- 批量压缩文件夹到Zip文件
实现效果: 实现代码:
- Unity 从StreamingAssets文件夹和PersistentData文件夹 保存读取AssetBundle资源
项目中的资源一般我们打包成AssetBundle格式 方便我们加载和热更 而AssetBundle文件 一般保存在StreamingAssets文件夹或则PersistentData文件夹 首先我们看 ...
- python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)
# python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- 下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt。File构造函数中正确的路径和文件名的表示是( )。
下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt.File构造函数中正确的路径和文件名的表示是( ). ./表示当前项目的路径../表示当 ...
- C# 读取指定文件夹下所有文件
#region 读取文件 //返回指定目录中的文件的名称(绝对路径) string[] files = System.IO.Directory.GetFiles(@"D:\Test" ...
- HALCON初步:文件夹遍历,文件筛选,文件名拆分,图片读取及保存
[1]文件夹遍历 list_image_files ( : : ImageDirectory, Extensions, Options : ImageFiles) ImageDirectory: 文件 ...
随机推荐
- Shell while循环
while循环用于不断执行一系列命令,也用于从输入文件中读取数据:命令通常为测试条件.其格式为: while command do Statement(s) to be executed if com ...
- weblogic开发模式与生产模式介绍
weblogic开发模式与生产模式介绍 开发模式:该模式启用自动部署 生产模式:该模式关闭自动部署 weblogic server 三种部署方法:自动部署.控制台部署.命令部署 自动部署:当其处于启用 ...
- clas
- 网上下载的 java开源项目 如何 打jar包
目前很多java开源项目(例如qlexpress)只提供了源码,没有提供jar,下面提供maven打jar包的方法. 1.进入qlexpress下载后源代码所在的目录,此目录应包含pom.xml,如下 ...
- having的用法以及与where区别介绍
having子句可以让我们筛选成组后的各种数据,having子句在查询过程中慢于聚合语句,接下来通过实例介绍下,感兴趣的你可以参考下,希望可以帮助到你 having的用法 having子句可以让我们筛 ...
- 在 VS14 上安装 Visual F# Power Tools
在 VS14 上安装 Visual F# Power Tools Visual F# Power Tools 眼下版本号 1.3,下载地址:http://visualstudiogallery.msd ...
- [D3] 13. Cleaner D3 code with selection.call()
selection.call() method in D3 can aid in code organization and flexibility by eliminating the need t ...
- 【OpenCV十六新手教程】OpenCV角检测Harris角点检测
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/29356187 作者:毛星云(浅墨) ...
- 第33日 我疯了集成平台(六)-步履轻盈JQuery(四)
6一个月28日本,天阴下雨. " 微雨过,小荷翻,榴花开欲燃.玉盆纤手弄清泉,琼珠碎却圆." 古老的JavaScript,且乱且复杂.封装成库,青春焕发,这样人们 ...
- linux-0.11内核 调试教程+GCC源代码
http://pan.baidu.com/share/home?uk=453348606&view=share#category/type=0 http://blog.csdn.net/old ...