我已经设法使用php创建了一个excel文档但是我每次打开文档时都会收到错误,即使其他一切都很好。错误是the file you are trying to open is in different format than specified by the file extension ...
我要导出到excel的代码:
public function actionExportToExcel() {
//header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="project-report-' . date('YmdHi') .'.xls"');
header("Content-Type: application/ms-excel");
$model=new ViewWebprojectreport('search');
$model->unsetAttributes(); // clear any default values
if(Yii::app()->user->getState('exportModel'))
$model=Yii::app()->user->getState('exportModel');
$dataProvider = $model->search(false);
$dataProvider->pagination->pageSize = $model->count();
// csv header
echo ViewWebprojectreport::model()->getAttributeLabel("StartDATE")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PROJECT")."\t".
"Survey Number\t".
ViewWebprojectreport::model()->getAttributeLabel("ActualEndDate")."\t".
ViewWebprojectreport::model()->getAttributeLabel("OFFICE")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PERCENT")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PERCENTPlanned")."\t".
ViewWebprojectreport::model()->getAttributeLabel("KM")."\t".
ViewWebprojectreport::model()->getAttributeLabel("KMPlanned")."\t".
ViewWebprojectreport::model()->getAttributeLabel("COUNTRY")."\t".
ViewWebprojectreport::model()->getAttributeLabel("AREA")."\t".
ViewWebprojectreport::model()->getAttributeLabel("ASAAREA").
" \r\n";
// csv data
foreach ($dataProvider->getData() as $data) {
//if you want all data use this looop
/*foreach ($data as $key => $value) {
echo $value.",";
}
echo "\r\n";*/
echo "$data->StartDATE\t$data->PROJECT\t".$data->PROJCODE . $data->PROJID ."\t$data->ActualEndDate\t$data->OFFICE\t$data->PERCENT\t$data->PERCENTPlanned\t$data->KM\t$data->KMPlanned\t$data->COUNTRY\t$data->AREA\t$data->ASAAREA\t\r\n";
}
}
我不想导出为csv,而是直接导入excel格式文件。我错过了什么?
答案 0 :(得分:1)
这是因为该文件实际上基本上只是一个带有XLS扩展名的CSV文件,以便在Excel中打开它。有关详细信息,请参阅此Microsoft文档:http://support.microsoft.com/kb/948615 - 它出现在新版本的Excel中。年纪较大的人会乐意出口他们。
这样做的原因是因为导出CSV文件比使用Excel文件简单得多。我想在某个时候写一个合适的Excel导出器,但这需要时间来阅读和理解Excel文件格式,我还没有机会这样做。
一个选项只是将文件名重命名为.csv,并保持用户界面说它是一个Excel文件(Excel非常乐意阅读csv文件)。鉴于Windows倾向于隐藏文件扩展名,这似乎是一个相当有吸引力的选择。
这对解决各种excel问题很有帮助 - link
请告诉我是否可以为您提供更多帮助。
答案 1 :(得分:0)
我使用过PHPExcel
$objPHPExcel = new PHPExcel();
spl_autoload_register(array('YiiBase', 'autoload'));
$objPHPExcel->getProperties()->setCreator(Yii::app()->user->__userInfo['name'])
->setLastModifiedBy(Yii::app()->user->__userInfo['name'])
->setTitle("Weekly Status")
->setSubject("Weekly Status");
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$model=new ViewWebprojectreport('search');
$model->unsetAttributes(); // clear any default values
if(Yii::app()->user->getState('exportModel'))
$model=Yii::app()->user->getState('exportModel');
$dataProvider = $model->weeklystatus(array(),true,false);
$dataProvider->pagination->pageSize = $model->count();
//data
foreach ($dataProvider->getData() as $data) {
//if you want all data use this looop
$highestColumn = "A";
foreach ($data as $key => $value) {
if(! in_array($key,array("id","PROCESSOR","DEPTCODE","PERCENTPlanned","MCSALE"))){
if($key == "name")
$key = "Client";
else
$key = ViewWebprojectreport::model()->getAttributeLabel("$key");
if($highestRow == 1){
$sheet->setCellValue($highestColumn.$highestRow,$key);
//Yii::log($key,"ERROR");
}
//echo $value.",";
if($highestRow == 1){
$highestRow++;
$sheet->setCellValue($highestColumn.$highestRow,$value);
$highestRow--;
}else
$sheet->setCellValue($highestColumn.$highestRow,$value);
$highestColumn++;
}
}
//Yii::log($highestRow,"ERROR");
if($highestRow == 1)
$highestRow++;
$highestRow++;
//echo "\r\n";*/
//echo "$data->StartDATE\t$data->ProjectEndDate\t$data->PROJECT\t".$data->PROJCODE . $data->PROJID ."\t$data->ActualEndDate\t$data->PROCESSOR\t$data->OFFICE\t$data->DEPTCODE\t$data->PERCENT\t$data->PERCENTPlanned\t$data->KM\t$data->KMPlanned\t$data->MC\t$data->MCSALE\t$data->CATEGORY\t$data->COUNTRY\t$data->AREA\t$data->PROJINFO\t$data->REGION\t$data->ASAAREA\t\r\n";
}
$filename = $_GET['type'].'statusreport_'.date('Y-m-d_H-i-s_T').'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
//header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(Yii::app()->params['exportToDir'].$filename);
$objWriter->save('php://output');
Yii::app()->end();