//读取
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 '
';<br> var_dump($content);<br> echo '';
//输出写入
header('Content-Type:text/html;charset=utf-8');
include 'PHPExcel_1.8.0_doc/Classes/PHPExcel.php';
include 'PHPExcel_1.8.0_doc/Classes/PHPExcel/IOFactory.php';
set_time_limit(0);
function write_xls($data=array(), $title=array(), $filename='report'){
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//设置标题
for($i=0,$length=count($title); $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('a.xlsx');
}
$arr=array();
mysql_connect('localhost','root','');
mysql_query('set names utf8');
mysql_query('use test');
$sql='select id,name,pid from shop where id $res=mysql_query($sql);
while($ret=mysql_fetch_assoc($res))
{
$arr[]=$ret;
}
$arr_type=array('商品id','名称','品牌id');
write_xls($arr,$arr_type,'report');