PHPEXCEL 使用小记
首先是使用PHP Reader 读取Excle内容:
<div class="codetitle"><a style="CURSOR: pointer" data="40433" class="copybut" id="copybut40433" onclick="doCopy('code40433')"> 代码如下:
<div class="codebody" id="code40433">
require("//www.jb51.cc/
PHPExcel/Classes/
PHPExcel.
PHP");
$file = "D:\datas.xlsx";
if(!file_exists($file)){
die("no file found in {$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;$row<=$sheetsinfo["rows"];$row++){
for($column=0;$column<$sheetsinfo["column"];$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;$row<=$sheetsinfo[$key]["rows"];$row++){
for($column=0;$column<$sheetsinfo[$key]["column"];$column++){
$sheetData[$key][$column][$row] = $sheet->getCellByColumnAndRow($column,$row)->getValue();
}
}
}
}
echo "
";
print_r($sheetData);
echo "
";
注:使用
PHP 读取excel
文件内容,一般都是处理整理好格式的csv或者excel,也可以读取xml
文件 PHPExcel
生成Exceel
<div class="codetitle">
<a style="CURSOR: pointer" data="32147" class="copybut" id="copybut32147" onclick="doCopy('code32147')"> 代码如下: <div class="codebody" id="code32147">
$
sql = sprintf("select * from table where op_id=%d",intval($this->p
arams['id']));
$query = $this->_db->query($
sql);
require_once './
PHPExcel_1.7.4/Classes/
PHPExcel.
PHP';
$obj
PHPExcel = new
PHPExcel();
$obj
PHPExcel->setActiveSheetIndex(0);
$obj
PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$obj
PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$obj
PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$obj
PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$obj
PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$obj
PHPExcel->getActiveSheet()->setCellValue('A1',"{$this->_packInfos['o_id']}");
$obj
PHPExcel->getActiveSheet()->setCellValue('B1',"Volume weight (kg)");
$obj
PHPExcel->getActiveSheet()->setCellValue('D1',"Actual weight (kg)");
$obj
PHPExcel->getActiveSheet()->setCellValue('A2',"
Box No.");
$obj
PHPExcel->getActiveSheet()->setCellValue('B2',"Products");
$obj
PHPExcel->getActiveSheet()->setCellValue('C2',"Shipping
Box");
$obj
PHPExcel->getActiveSheet()->setCellValue('D2',"Sy
stem");
$obj
PHPExcel->getActiveSheet()->setCellValue('E2',"Input");
$objActSheet = $obj
PHPExcel->getActiveSheet();
$objActSheet->mergeCells("B1:C1");
$objActSheet->mergeCells("D1:E1"); $obj
PHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$obj
PHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$obj
PHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $obj
PHPExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$obj
PHPExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$obj
PHPExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$obj
PHPExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$obj
PHPExcel->getActiveSheet()->getStyle('E2'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER); if($this->_db->num_rows($query)>0)
{
$i=3;
while ($row = $this->_db->fetch_assoc($query))
{
$obj
PHPExcel->getActiveSheet()->setCellValue('A'.($i),"
Box ".$row['
Box_num']);
$obj
PHPExcel->getActiveSheet()->setCellValue('B'.($i),sprintf("%.2f",$row['volume_weight']));
$obj
PHPExcel->getActiveSheet()->setCellValue('C'.($i),$row['
Box_weight']));
$obj
PHPExcel->getActiveSheet()->setCellValue('D'.($i),$row['sy
stem_weight']));
$obj
PHPExcel->getActiveSheet()->setCellValue('E'.($i),$row['real_weight'])); $obj
PHPExcel->getActiveSheet()->getStyle('A'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$obj
PHPExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$obj
PHPExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$obj
PHPExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$obj
PHPExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$i++;
}
} $fileName="export
Box.xls";
$filePath = dirname(dirname("
FILE"))."/template/".$fileName;
$path = "./template/".$fileName;
$objWriter = new
PHPExcel_Writer_Excel2007($obj
PHPExcel);
if(file_exists($path)){
chmod($path,0777);
unlink($path);
$objWriter->save($path);
header('application/vnd.ms-excel');
header('Content-
disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx");
readfile($filePath);
die();
}
else
{
$objWriter->save($path);
header('application/vnd.ms-excel');
header('Content-
disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx");
readfile($filePath);
die();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。