当前位置:首页 » 编程语言 » phpexcel合并

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变量是否引用正确(如拼写错误等)。

热点内容
如何让给文件夹设置密码查看 发布:2025-01-31 22:49:07 浏览:2
配置动态路由协议配错了怎么改 发布:2025-01-31 22:49:07 浏览:77
扫行程码为什么需要支付密码 发布:2025-01-31 22:47:08 浏览:738
什么样的配置能玩地平线4 发布:2025-01-31 22:44:05 浏览:241
python正则表达式符号 发布:2025-01-31 22:43:50 浏览:391
androidmime 发布:2025-01-31 22:34:44 浏览:782
ftp和http的中文含义是 发布:2025-01-31 22:33:48 浏览:402
sqlite3存储图片 发布:2025-01-31 22:27:14 浏览:162
sqlserverphp 发布:2025-01-31 22:22:55 浏览:877
曲马多存储 发布:2025-01-31 22:22:52 浏览:538