首页 >后端开发 >php教程 >PHPEXCEL 使用小记_PHP

PHPEXCEL 使用小记_PHP

WBOY
WBOY原创
2016-06-01 12:08:23858浏览

首先是使用PHP Reader读取Excle内容:
复制代码如下代码:
require("http://www.bitsCN.com/PHPExcel/Classes/PHPExcel.php");
$file = "D:\datas.xlsx";
if(!file_exists($file)){
die("在 {$file} 中找不到文件");
}
$datasReader = PHPExcel_IOFactory::load($file);
$sheets = $datasReader->getAllSheets();
//如果有多个工作簿
$countSheets = count($sheets);
$sheetsinfo = array();
$sheetData = array();
if($countSheets==1){
$sheet = $sheets[0];
$sheetsinfo["rows"] = $sheet->getHighestRow();
$sheetsinfo["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
for($row=1;$rowfor($column=0;$column$sheetData[$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();
}
}
}else{
foreach ($sheets as $key => $sheet)
{
$sheetsinfo[$key]["rows"] = $sheet ->getHighestRow();
$sheetsinfo[$key]["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
for($row=1;$rowfor($column=0;$column$sheetData[$key][$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();
}
}
}
}
回显“

"; <br>print_r($sheetData); <br>echo "
”;

注:使用PHP读取excel文件内容,一般都是处理整理好的格式的csv或者excel,也可以读取xml文件

PHPExcel生成Excel
复制代码代码如下:
$sql = sprintf("select * from table where op_id=%d", intval($this->params['id']));
$query = $this->_db->query($sql);
require_once './PHPExcel_1.7.4/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objPHPExcel->getActiveSheet()->setCellValue('A1', "{$this->_packInfos['o_id']}");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "体积重量(公斤)");
$objPHPExcel->getActiveSheet()->setCellValue('D1', "实际体积重量(公斤)");


$objPHPExcel->getActiveSheet()->setCellValue('A2', "箱子编号");
$objPHPExcel->getActiveSheet()-> setCellValue('B2', "产品");
$objPHPExcel->getActiveSheet()->setCellValue('C2', "运输箱");
$objPHPExcel->getActiveSheet()->setCellValue(' D2', "系统");
$objPHPExcel->getActiveSheet()->setCellValue('E2', "输入");
$objActSheet = $objPHPExcel->getActiveSheet(); ->mergeCells("B1:C1");
$objActSheet->mergeCells("D1:E1");

$objPHPExcel->getActiveSheet()->getStyle('A1')-> getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$ objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$objPHPExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E2'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

if($this->_db->num_rows($query)>0)
{
$i=3;
while ($row = $this->_db->fetch_assoc($query))
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.($i),"BOX ".$row['box_num']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.($i),sprintf("%.2f",$row['volume_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('C'.($i),sprintf("%.2f",$row['box_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('D'.($i),sprintf("%.2f",$row['system_weight']));
$objPHPExcel->getActiveSheet()->setCellValue('E'.($i),sprintf("%.2f",$row['real_weight']));

$objPHPExcel->getActiveSheet()->getStyle('A'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$i ;
}
}

$fileName="exportBox.xls";
$filePath = dirname(dirname("__FILE__"))."/template/".$fileName;
$path = "./template/".$fileName;
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
if(file_exists($path)){
chmod($path, 0777);
取消链接($path);
$objWriter->save($path);
header('application/vnd.ms-excel');
header('内容处置:附件;文件名=权重-'.$this->_packInfos["o_id"].".xlsx");
readfile($filePath);
死();
}
else
{
$objWriter->save($path);
header('application/vnd.ms-excel');
header('内容处置:附件;文件名=权重-'.$this->_packInfos["o_id"].".xlsx");
readfile($filePath);
死();
}

注:上面的php生成excel的方式是直接使用标签形式的,如果使用ajax,可以不使用header,直接echo $path,前台window.location.href=返回来的路径就可以了。
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn