无为清净楼资源网 Design By www.qnjia.com
首先是使用PHP Reader 读取Excle内容:
复制代码 代码如下:
require("https://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就可以了。
复制代码 代码如下:
require("https://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就可以了。
标签:
PHPEXCEL
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]