php如何实现将一个页面以excel格式保存,需要的时候能在转化成页面形式显示 10
2个回答
2015-04-24
展开全部
需要用到PHPExcel
保存为Excel:
<?php
header ( "content-type:text/html;charset=utf-8" );
/**
* Error reporting
*/
error_reporting ( E_ALL );
/**
* PHPExcel
*/
include_once './Classes/PHPExcel.php';
/**
* PHPExcel_Writer_Excel2003用于创建xls文件
*/
include_once './Classes/PHPExcel/Writer/Excel5.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel ();
// Set properties
$objPHPExcel->getProperties ()->setCreator ( "RT" );
$objPHPExcel->getProperties ()->setLastModifiedBy ( "RT" );
$objPHPExcel->getProperties ()->setTitle ( "Office 2007 XLSX Test Document" );
$objPHPExcel->getProperties ()->setSubject ( "Office 2007 XLSX Test Document" );
$objPHPExcel->getProperties ()->setDescription ( "Test document for Office 2007 XLSX, generated using PHP classes." );
// Add some data
$objPHPExcel->setActiveSheetIndex ( 0 );
$objPHPExcel->getActiveSheet ()->SetCellValue ( 'A1', 'Date' );
// 合并单元格:
$objPHPExcel->getActiveSheet ()->mergeCells ( 'B1:F1' );
$objPHPExcel->getActiveSheet ()->SetCellValue ( 'B1', 'CSAT Score' );
$objPHPExcel->getActiveSheet ()->SetCellValue ( 'G1', 'Grand Total' );
$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%' );
// Rename sheet
// $objPHPExcel->getActiveSheet()->setTitle('Csat');
// Save Excel 2007 file
// $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$d = date ( 'Y-m-d' );
$objWriter = new PHPExcel_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 = new Spreadsheet_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 );
?>
展开全部
推荐一个简单粗暴的输出办法,虽然excel会报个错,但是不影响浏览
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");
//输出内容如下:
echo "姓名"."\t";
echo "年龄"."\t";
echo "学历"."\t";
echo "\n";
echo "张三"."\t";
echo "25"."\t";
echo "本科"."\t";
?>
但是这单纯是导出,再导入的时候就要复杂很多了
推荐phpExcel插件,这个操作我就不在这里细说了,一句半句真说不清楚。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询