澳门新葡萄京官网注册php导出CSV方法 (转)

制作网站时,经常会遇到检索数据列表的情况。通常用户希望下载这些列表数据并存储到客户端。当然下载这些数据的时候是需要固定的格式,以便于用Excel等软件阅览。说的简单一些就是CSV/Excel数据导出

//读取

注意事项:

<?php

输出文档的基本格式为:列1,列2,列3,,列nn

header(“Content-Type:text/html;charset=utf-8”);
include ‘Classes/PHPExcel.php’;
include ‘Classes/PHPExcel/IOFactory.php’;

格式化数据的同时要对特殊的字符进行过滤。譬如,如果不转换为全角,会导致数据格式的混乱。

function readxls($file, $type) {
$xlsReader = PHPExcel_IOFactory::createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$sheets = $xlsReader->load($file);
$content = $sheets->getSheet(0)->toArray();
//读取第一个工作表(注意编号从0开始)
如果读取多个可以做一个循环0,1,2,3….
//得到二维数组,每个小数组是excel表格内容的一行
里面包含此行的每列的数据
return $content;
}

实现方法:

//$type = ‘Excel2007’; //设置要解析的Excel类型
Excel5(2003或以下版本)或Excel2007
$type = ‘Excel5’;
$content = readxls(‘data.xls’, $type);
echo ‘<pre>’;
var_dump($content);
echo ‘</pre>’;

举例:检索数据库表,将结果保存为字符串,进行格式和特殊字符的过滤后,导出到客户端的CSV文件。

 

OrderSearchEdit_saveCSV.php

//输出写入,数据多的话必须用类一次次调用方法导出,不然会内存溢出

?phpinclude($_SERVER[”DOCUMENT_ROOT”]./ftcart/OrderInfoManager.class.php);include($_SERVER[”DOCUMENT_ROOT”]./ftcart/CommonUtil.php);include($_SERVER[”DOCUMENT_ROOT”]./ftcart/CommonConst.php);include($_SERVER[”DOCUMENT_ROOT”]./ftcart/CommonErrorMsg.php);include($_SERVER[”DOCUMENT_ROOT”]./webadmin.php);$searchcase=newOrderInfoManager();$nowTime=microtime_float();$searchcase=unserialize($_SESSION[ORDER_SEARCH_CASE]);$listcsv=$searchcase-doCSV();//调用doCSV()方法$filename=$nowTime;//$filename=str_replace(:,,$nowTime);//$filename=trim($filename);//echo$filename;header(Content-Disposition:attachment;filename=.$filename..csv);header(”Content-Type:APPLICATION/OCTET-STREAM”);echo注文No.,注文日,氏名,住所,支

<?php
header(‘Content-Type:text/html;charset=utf-8’);
class excel
{
public function export()
{
//导出函数
function write_xls($data=array(), $title=array(),
$filename=’report’){
include ‘PHPExcel_1.8.0_doc/Classes/PHPExcel.php’;
include ‘PHPExcel_1.8.0_doc/Classes/PHPExcel/IOFactory.php’;
set_time_limit(0);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$cols = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
//设置标题
for($i=0,$length=count($title); $i<$length; $i++) {
$objPHPExcel->getActiveSheet()->setCellValue($cols{$i}.’1′,
$title[$i]);
}
//设置标题样式(可按需求决定是否需要)
// $titleCount = count($title);
// $r = $cols{0}.’1′;
// $c = $cols{$titleCount}.’1′;
//
$objPHPExcel->getActiveSheet()->getStyle(“$r:$c”)->applyFromArray(
// array(
// ‘font’ => array(
// ‘bold’ => true
// ),
// ‘alignment’ => array(
// ‘horizontal’ => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
// ),
// ‘borders’ => array(
// ‘top’ => array(
// ‘style’ => PHPExcel_Style_Border::BORDER_THIN
// )
// ),
// ‘fill’ => array(
// ‘type’ => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
// ‘rotation’ => 90,
// ‘startcolor’ => array(
// ‘argb’ => ‘FFA0A0A0’
// ),
// ‘endcolor’ => array(
// ‘argb’ => ‘FFFFFFFF’
// )
// )
// )
// );

$i = 0;
foreach($data as $d) { //这里用foreach,支持关联数组和数字索引数组
$j = 0;
foreach($d as $v) { //这里用foreach,支持关联数组和数字索引数组
$objPHPExcel->getActiveSheet()->setCellValue($cols{$j}.($i+2),
$v);
$j++;
}
$i++;
}
// 生成2003excel格式的xls文件直接下载
/* header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition:
attachment;filename=”‘.$filename.’.xls”‘);
header(‘Cache-Control: max-age=0’);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
‘Excel5’);
$objWriter->save(‘php://output’);*/

// 生成2007excel格式的xls文件保存到指定路径
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,
‘Excel2007’);
$objWriter->save($filename.’.xlsx’);
}

//导出

$start_num=trim($_GET[‘start_num’]);
$total_num=trim($_GET[‘total_num’]);
mysql_connect(‘localhost’,’root’,”);
mysql_query(‘set names utf8’);
mysql_query(‘use test’);

$sql=”select id,name,pid from shop order by id asc limit
$start_num,$total_num”;
$res=mysql_query($sql);
while($ret=mysql_fetch_assoc($res))
{
$arr[]=$ret;
}
$arr_type=array(‘商品id’,’名称’,’品牌id’);
write_xls($arr,$arr_type,time());
}
}

//实例化调用
$obj=new excel();
$obj->export();

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注