鉴于大家对PHP十分关注,我们编辑小组在此为大家搜集整理了“PHPEXCEL 使用小记”一文,供大家参考学习!
首先是使用PHP Reader 读取Excle内容:
复制代码 代码如下:
require("http://www.jb51.net/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 "<pre>";
print_r($sheetData);
echo "</pre>";
注:使用PHP 读取excel文件内容,一般都是处理整理好格式的csv或者excel,也可以读取xml文件
PHPExcel生成Exceel
复制代码 代码如下:
$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'', "Volume weight (kg)");
$objPHPExcel->getActiveSheet()->setCellValue(''D1'', "Actual weight (kg)");
$objPHPExcel->getActiveSheet()->setCellValue(''A2'', "Box No.");
$objPHPExcel->getActiveSheet()->setCellValue(''B2'', "Products");
$objPHPExcel->getActiveSheet()->setCellValue(''C2'', "Shipping Box");
$objPHPExcel->getActiveSheet()->setCellValue(''D2'', "System");
$objPHPExcel->getActiveSheet()->setCellValue(''E2'', "Input");
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->mergeCells("B1:C1");
$objActSheet->mergeCells("D1:E1");
$objPHPExcel->getActiveSheet()->getStyle(''A1'')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle(''B1'')->