PHPExcel

一、PHP导出、输入EXCEL

1.百度:phpexcel,结果如图所示,点击第一个结果;

PHP导出Excel,PHP输入Excel

 

2.进入官网后,找到右边的download按钮,下载,下载完成的是一个压缩文件,解压放到你的项目目录里,根据个人情况而定;

PHP导出Excel,PHP输入Excel
PHP导出Excel,PHP输入Excel

3.因为这里给大家做演示,所以建了一个测试文件,有点基础的都能明白是怎么回事,下面进入代码;

PHP导出Excel,PHP输入Excel

 

4.

//引入PHPExcel库文件(路径根据自己情况)

include './phpexcel/Classes/PHPExcel.php';

//创建对象

$excel = new PHPExcel();

//Excel表格式,这里简略写了8列

$letter = array('A','B','C','D','E','F','F','G');

//表头数组

$tableheader = array('学号','姓名','性别','年龄','班级');

 

//填充表头信息

for($i = 0;$i < count($tableheader);$i++) {

$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");

}

 

PHP导出Excel,PHP输入Excel

 

5.

//表格数组

$data = array(

array('1','小王','男','20','100'),

array('2','小李','男','20','101'),

array('3','小张','女','20','102'),

array('4','小赵','女','20','103')

);

//填充表格信息

for ($i = 2;$i <= count($data) + 1;$i++) {

$j = 0;

foreach ($data[$i - 2] as $key=>$value) {

$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");

$j++;

}

}

 

PHP导出Excel,PHP输入Excel

 

6.

//创建Excel输入对象

$write = new PHPExcel_Writer_Excel5($excel);

header("Pragma: public");

header("Expires: 0");

header("Cache-Control:must-revalidate, post-check=0, pre-check=0");

header("Content-Type:application/force-download");

header("Content-Type:application/vnd.ms-execl");

header("Content-Type:application/octet-stream");

header("Content-Type:application/download");;

header('Content-Disposition:attachment;filename="testdata.xls"');

header("Content-Transfer-Encoding:binary");

$write->save('php://output');

 

PHP导出Excel,PHP输入Excel

 

7.打开页面,刷新的时候会弹出对话框,让你选择文件保存路径和文件名称,我直接放在了桌面上,如图所示;

PHP导出Excel,PHP输入Excel
PHP导出Excel,PHP输入Excel

 

8.打开表格后,数据和格式跟代码中的一致,说明PHP导出的Excel是正确的。如果出现错误,检查一下你的表格数组和数据数组吧。

PHP导出Excel,PHP输入Excel

 

二、使用phpexcel导出xls文件时乱码解决方案

<?php
include 'global.php';
$ids = $_GET['ids'];

$sql = "select * from crm_cost_end where id in ( {$ids} )";
$result = $db->findAll($sql);
//echo $result[1]['sn'];

//创建一个excel对象
$objPHPExcel = new PHPExcel();
// Set properties

$objPHPExcel->getProperties()->setCreator("ctos")
->setLastModifiedBy("ctos")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");

//set width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(30);

//设置行高度
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);

$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);

//set font size bold
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A2:J2')->getFont()->setBold(true);

$objPHPExcel->getActiveSheet()->getStyle('A2:J2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:J2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

//设置水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

//
$objPHPExcel->getActiveSheet()->mergeCells('A1:J1');

// set table header content
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '订单数据汇总 时间:' . date('Y-m-d H:i:s'))
->setCellValue('A2', '订单ID')
->setCellValue('B2', '下单人')
->setCellValue('C2', '客户名称')
->setCellValue('D2', '下单时间')
->setCellValue('E2', '需求机型')
->setCellValue('F2', '需求数量')
->setCellValue('G2', '需求交期')
->setCellValue('H2', '确认BOM料号')
->setCellValue('I2', 'PMC确认交期')
->setCellValue('J2', 'PMC交货备注');

// Miscellaneous glyphs, UTF-8

for ($i = 0; $i < count($result) - 1; $i++) {
$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($i + 3), $result[$i]['id']);
$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 3), $result[$i]['realname']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($i + 3), $result[$i]['customer_name']);
$objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $OrdersData[$i]['create_time']);
$objPHPExcel->getActiveSheet(0)->setCellValue('E' . ($i + 3), $result[$i]['require_product']);
$objPHPExcel->getActiveSheet(0)->setCellValue('F' . ($i + 3), $result[$i]['require_count']);
$objPHPExcel->getActiveSheet(0)->setCellValue('G' . ($i + 3), $result[$i]['require_time']);
$objPHPExcel->getActiveSheet(0)->setCellValue('H' . ($i + 3), $result[$i]['product_bom_encoding']);
$objPHPExcel->getActiveSheet(0)->setCellValue('I' . ($i + 3), $result[$i]['delivery_time']);
$objPHPExcel->getActiveSheet(0)->setCellValue('J' . ($i + 3), $result[$i]['delivery_memo']);
$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':J' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':J' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);
}


// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('订单汇总表');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
ob_end_clean();//清除缓冲区,避免乱码
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="订单汇总表(' . date('Ymd-His') . ').xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>

 

posted @ 2019-11-14 14:31  诔栩  阅读(232)  评论(0编辑  收藏  举报