我正在使用PHPExcel从我的数据库导出一个列表。
但细胞间距不均匀,如图所示。
问题:如何确保每个单元格在PHPExcel中自动均匀分布
我在这里尝试了答案,但没有正常工作
PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically
控制器代码
<?php
class Events extends MX_Controller {
private $error = array();
public function __construct() {
parent::__construct();
}
public function generate_excel() {
$query = $this->db->get('event');
$excelresults = $query->result_array();
require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
require (APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setLastModifiedBy("");
$objPHPExcel->getProperties()->setSubject("");
$objPHPExcel->getProperties()->setCreator("");
$objPHPExcel->getProperties()->setDescription("");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue("A1", 'Event');
$objPHPExcel->getActiveSheet()->SetCellValue("B1", 'Event Title');
$objPHPExcel->getActiveSheet()->SetCellValue("C1", 'Event Date');
$objPHPExcel->getActiveSheet()->SetCellValue("D1", 'Event Start Time');
$excelrow = 2;
foreach ($excelresults as $excelresult => $excelvalue) {
$objPHPExcel->getActiveSheet()->SetCellValue("A" . $excelrow, $excelvalue['event']);
$objPHPExcel->getActiveSheet()->SetCellValue("B" . $excelrow, $excelvalue['event_title']);
$objPHPExcel->getActiveSheet()->SetCellValue("C" . $excelrow, $excelvalue['event_date']);
$objPHPExcel->getActiveSheet()->SetCellValue("D" . $excelrow, $excelvalue['event_start_time']);
$excelrow++;
}
$filename = 'Bowling-Events-For ' . date('Y') . '.xlsx';
$objPHPExcel->getProperties()->setTitle("Riwaka Bowling Club Events");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' .$filename. '"');
header('Cache-Control: max-age=0');
$Writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$Writer->save('php://output');
exit;
}
}
答案 0 :(得分:1)
这里有更多谷歌搜索答案 PHPExcel auto size column width
foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}