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變數是否引用正確(如拼寫錯誤等)。
熱點內容