|
复制代码 代码如下:
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')->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);
$objphpExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);
$objphpExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_LEFT);
$objphpExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);
$objphpExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);
$objphpExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);
$objphpExcel->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))
{
$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()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_LEFT);
$objphpExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objphpExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objphpExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objphpExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->setHorizontal(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);
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();
}
注:上面的php生成excel的方式是直接使用A标签形式的,如果使用ajax,可以不使用header,直接echo $path,前台window.location.href=返回来的path就可以了。
php技术:PHPEXCEL 使用小记,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。