php打包下载excel,php读取excel,以及php打包文件夹为zip文件

1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的files文件夹打包成files.zip4.访问index.php即可生成files文件夹,里面0.js---n.js 分别存放各个服务器人名,server_name_list.js存放服务器列表。5.Classes 存放的是php读取excel的功能模块,具体任务逻辑都在index.phpA.PHP读取excel支持excel2007demo逻辑代码:其中的(arrayRecursive,JSON方法是json数据处理功能,可兼容汉字)主要借助了:PHPExcel插件,附件中有Classes文件夹,官网:http://www.codeplex.com/PHPExcelindex.php

/** Error reporting */

error_reporting(0);

header("Content-type: text/html; charset=utf-8");

?>

标题

/**************************************************************

*

* 使用特定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."
";

$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."
";;

$fp=fopen("$file", "w+"); //打开文件指针,创建文件

if ( !is_writable($file) ){

die("文件:" .$file. "不可写,请检查!");

}

if (is_writable($file) == false) {

die('我是鸡毛,我不能');

}

file_put_contents ($file, JSON($sname));

echo "生成完毕!";

echo '打包生成文件'

?>

B.PHP打包文件夹为zip文件

zip.php

/** Error reporting */

error_reporting(0);

header("Content-type: text/html; charset=utf-8");

?>

标题

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 '打包完毕!'."
";

echo "下载files.zip"

?>

来源:https://www.cnblogs.com/zhidong123/p/3850861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.获取PHPExcel 2.添加如下方法: function Excel_Export($filename,$data,$sheet){ error_reporting(E_ALL); ini_set('display_errors','On'); /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/'); /** PHPExcel */ include 'api/excel/PHPExcel.php'; /** PHPExcel_Writer_Excel2007 */ include 'api/excel/PHPExcel/Writer/Excel2007.php'; // Create new PHPExcel object //echo date('H:i:s') . "Create new PHPExcel object\n"; $objPHPExcel = new PHPExcel(); // Set properties //echo date('H:i:s') . "Set properties\n"; $objPHPExcel->getProperties()->setCreator("E421083458"); $objPHPExcel->getProperties()->setLastModifiedBy("E421083458"); $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."); $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); $objPHPExcel->getProperties()->setCategory("Test result file"); // Add some data //echo date('H:i:s') . "Add some data\n"; $objPHPExcel->setActiveSheetIndex(0); $charlist = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"); //print_r($data); foreach($data as $key=>$value){ $j=0; if($key==0){ foreach($value as $k=>$v){ if($j<25){ $objPHPExcel->getActiveSheet()->setCellValue($charlist[$j].($key+1), mb_convert_encoding($k, "UTF-8", "GBK")); $j++; } } $j=0; } foreach($value as $k=>$v){ if($j<25){ //echo $charlist[$j].($key+1)."<br/>"; //echo $v."<br/>"; $objPHPExcel->getActiveSheet()->setCellValue($charlist[$j].($key+2), mb_convert_encoding($v, "UTF-8", "GBK")); $j++; } } } // Rename sheet //echo date('H:i:s') . "Rename sheet\n"; $objPHPExcel->getActiveSheet()->setTitle(mb_convert_encoding($sheet,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值