phpexcel合并
发布时间: 2023-10-22 20:46:47
❶ php如何实现将一个页面以excel格式保存,需要的时候能在转化成页面形式显示
需要用到PHPExcel
保存为Excel:
<?php
header("content-type:text/html;charset=utf-8");
/**
*Errorreporting
*/
error_reporting(E_ALL);
/**
*PHPExcel
*/
include_once'./Classes/PHPExcel.php';
/**
*PHPExcel_Writer_Excel2003用于创建xls文件
*/
include_once'./Classes/PHPExcel/Writer/Excel5.php';
//CreatenewPHPExcelobject
$objPHPExcel=newPHPExcel();
//Setproperties
$objPHPExcel->getProperties()->setCreator("RT");
$objPHPExcel->getProperties()->setLastModifiedBy("RT");
$objPHPExcel->getProperties()->setTitle("Office2007XLSXTestDocument");
$objPHPExcel->getProperties()->setSubject("Office2007XLSXTestDocument");
$objPHPExcel->getProperties()->setDescription("TestdocumentforOffice2007XLSX,generatesingPHPclasses.");
//Addsomedata
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1','Date');
//合并单元格:
$objPHPExcel->getActiveSheet()->mergeCells('B1:F1');
$objPHPExcel->getActiveSheet()->SetCellValue('B1','CSATScore');
$objPHPExcel->getActiveSheet()->SetCellValue('G1','GrandTotal');
$objPHPExcel->getActiveSheet()->SetCellValue('H1','CSAT');
$objPHPExcel->getActiveSheet()->SetCellValue('A2','08/01/11');
$objPHPExcel->getActiveSheet()->SetCellValue('B2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('C2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('D2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('E2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('F2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('G2','0');
$objPHPExcel->getActiveSheet()->SetCellValue('H2','0%');
$objPHPExcel->getActiveSheet()->SetCellValue('A3','08/01/11');
$objPHPExcel->getActiveSheet()->SetCellValue('B3','武汉');
$objPHPExcel->getActiveSheet()->SetCellValue('C3','湖北');
$objPHPExcel->getActiveSheet()->SetCellValue('D3','洪湖');
$objPHPExcel->getActiveSheet()->SetCellValue('E3','0');
$objPHPExcel->getActiveSheet()->SetCellValue('F3','0');
$objPHPExcel->getActiveSheet()->SetCellValue('G3','0');
$objPHPExcel->getActiveSheet()->SetCellValue('H3','0%');
$objPHPExcel->getActiveSheet()->SetCellValue('A4','08/01/11');
$objPHPExcel->getActiveSheet()->SetCellValue('B4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('C4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('D4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('E4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('F4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('G4','0');
$objPHPExcel->getActiveSheet()->SetCellValue('H4','0%');
//Renamesheet
//$objPHPExcel->getActiveSheet()->setTitle('Csat');
//SaveExcel2007file
//$objWriter=newPHPExcel_Writer_Excel2007($objPHPExcel);
$d=date('Y-m-d');
$objWriter=newPHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(str_replace('.php','.xls',__FILE__));
header("Pragma:public");
header("Expires:0");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=".$d.".xls");
header("Content-Transfer-Encoding:binary");
$objWriter->save("php://output");
?>
读取Excel内容:
<?php
header("Content-Type:text/html;charset=utf-8");
require_once'excel_reader2.php';
//创建对象
$data=newSpreadsheet_Excel_Reader();
//设置文本输出编码
$data->setOutputEncoding('UTF-8');
//读取Excel文件
$data->read("e.xls");
//$data->sheets[0]['numRows']为Excel行数
for($i=1;$i<=$data->sheets[0]['numRows'];$i++){
//$data->sheets[0]['numCols']为Excel列数
for($j=1;$j<=$data->sheets[0]['numCols'];$j++){
//显示每个单元格内容
//echo$data->sheets[0]['cells'][$i][$j].'';
$arr[$i]['id']=$data->sheets[0]['cells'][$i][1];
//$arr[$i]['idcard']=$data->sheets[0]['cells'][$i][2];
$arr[$i]['stuno']=$data->sheets[0]['cells'][$i][3];
//$arr[$i]['name']=$data->sheets[0]['cells'][$i][4];
$arr[$i]['dept']=$data->sheets[0]['cells'][$i][5];
$arr[$i]['major']=$data->sheets[0]['cells'][$i][6];
$arr[$i]['class']=$data->sheets[0]['cells'][$i][7];
}
/*
*exit();echo'<br>';
*/
}
print_r($arr);
?>
具体参考我的博客:http://my.oschina.net/rain21/blog/380349
❷ 请教PHPExcel的问题
1、$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );能够设置颜色。
2、合并后的单元格坐标是原最左上角的单元格,如合A1:B2合并后坐标是A1。
3、照你的提示信息,应该是你没有给出坐标。如$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');中你并没有指定A1这个参数。检查你用在此处的PHP变量是否引用正确(如拼写错误等)。
热点内容