function down_xls($data, $keynames, $name='dataxls') {
$xls[] = "<html><meta http-equiv=content-type content=\"text/htmlcharset=UTF-8\"><body><table border='1'>"
$xls[] = "<tr><td>ID</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>'
foreach($data As $o) {
$line = array(++$index)
foreach($keynames AS $k=>蔽指键$v) {
$line[] = $o[$k]
}
$xls[] = '<tr><td>'. implode("</td><td>", $line) . '</td></tr>'
}
$xls[] = '<逗吵/table></body></html>'
$xls = join("\r\n", $xls)
header('Content-Disposition: attachmentfilename="'.$name.'.xls"')
die(mb_convert_encoding($xls,'UTF-8','UTF-8'))
}
函数引用:
if(strval($_GET['download'])){
$orders = DB::LimitQuery('order', array(
'condition' =>$condition,
'order' =>'ORDER BY id DESC',
))
if (!$orders) die('没有符合条件的记录')
$name = 'order_'.date('Ymd')
$kn = array( //excel表列名与数据字段的对应关系
'id' =>'订单号',
'price' =>'订单金额',
'card' =>'代金券',
'create_time' =>'下单时间',
'pay_time' =>'付款宏巧时间',
)
foreach( $orders AS $one ){
$one['create_time'] =date("Y-m-d",$one['create_time'])
$one['pay_time']=date("Y-m-d",$one['pay_time'])
$eorders[] = $one
}
down_xls($eorders, $kn, $name)
}
需要用到PHPExcel
保存为Excel:
<?phpheader ( "content-type:text/htmlcharset=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:attachmentfilename=" . $d . ".xls" )
header ( "Content-Transfer-Encoding:binary" )
$objWriter->save ( "php://output" )
?>
读取Excel内容:
<?phpheader ( "Content-Type:text/htmlcharset=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 )
?>
具体参考我的博客:http://my.oschina.net/rain21/blog/380349
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)