这是我写的一个方法,这个方法可以直接使用在你的代码上。下面我画红色的就是要修改或者删除的。
public function import(){
/*创建PHPEXCLE读取,默认excel2007,最好兼容csv格式*/
import("ORG/PHPExcel/PHPExcel");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
//createSheet: Create sheet and add it to this workbook setTitle:设置表格的名称
$objPHPExcel->createSheet(0)->setTitle('订单');
$objPHPExcel->setActiveSheetIndex(0);
//设置表头
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'status_id');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'name');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'order_id');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'is_end');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'description');
//获得属性设置一些值,这些都是可以不用修改。
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy(" ")
->setTitle("Office 2003 XLSX Test Document")
->setSubject("Office 2003 XLSX Test Document")
->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
->setKeywords("office 2003 openxml php")
->setCategory("Test result file");
$letter = array('A', 'B', 'C', 'D', 'E');
$m_businessstatus=M('business_status');
$arr = $m_businessstatus->select();
//print_r($arr);exit;
if($arr){
foreach($arr as $key => $value){
$j=0;
foreach($value as $ind =>$val){
$index=$letter[$j].($key+2); //$index = A2 .....B2 ..E2
// echo 'index:'.$index;
//循环设置表中的值
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($index, $val);
$j++;
}
}
}
$date = date('Y-m-d', time());
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $date . '.xlsx"');
header('Cache-Control: max-age=0');
$objWriter2 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter2->save('php://output');
exit;
}