1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-02 02:40:58 +01:00

Use PHPExcel, not Spreadsheet_Excel_Writer, to export Excel sheets

Summary: Sandra had trouble opening the Spreadsheet_Excel_Writer ones so use
PHPExcel, which is way better, just a bit more complicated.

Test Plan:
  - Generated modern Excel 2007 .xslx sheets.
  - Opened them in Excel in Office Mac 2011.
  - Opened them in Apple Numbers from the app store.

Reviewers: btrahan, jungejason

Reviewed By: jungejason

CC: aran, epriestley

Maniphest Tasks: T911

Differential Revision: https://secure.phabricator.com/D1744
This commit is contained in:
epriestley 2012-03-01 17:23:29 -08:00
parent b18fa48c89
commit b5da96f67a

View file

@ -29,24 +29,31 @@ final class ManiphestExportController extends ManiphestController {
} }
/** /**
* @phutil-external-symbol class Spreadsheet_Excel_Writer * @phutil-external-symbol class PHPExcel
* @phutil-external-symbol class PHPExcel_IOFactory
* @phutil-external-symbol class PHPExcel_Style_NumberFormat
*/ */
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$ok = @include_once 'Spreadsheet/Excel/Writer.php'; $ok = @include_once 'PHPExcel.php';
if (!$ok) { if (!$ok) {
$dialog = new AphrontDialogView(); $dialog = new AphrontDialogView();
$dialog->setUser($user); $dialog->setUser($user);
$dialog->setTitle('Excel Export Not Configured'); $dialog->setTitle('Excel Export Not Configured');
$dialog->appendChild( $dialog->appendChild(
'<p>This system does not have Spreadsheet_Excel_Writer installed. '. '<p>This system does not have PHPExcel installed. This software '.
'This software component is required to export tasks to Excel. Have '. 'component is required to export tasks to Excel. Have your system '.
'your system administrator install it with:</p>'. 'administrator install it from:</p>'.
'<br />'. '<br />'.
'<p><code>$ sudo pear install Spreadsheet_Excel_Writer</code></p>'); '<p>'.
'<a href="http://www.phpexcel.net/">http://www.phpexcel.net/</a>'.
'</p>'.
'<br />'.
'<p>Your PHP "include_path" needs to be updated to include the '.
'PHPExcel Classes/ directory.</p>');
$dialog->addCancelButton('/maniphest/'); $dialog->addCancelButton('/maniphest/');
return id(new AphrontDialogResponse())->setDialog($dialog); return id(new AphrontDialogResponse())->setDialog($dialog);
@ -87,34 +94,35 @@ final class ManiphestExportController extends ManiphestController {
->loadHandles(); ->loadHandles();
$handles += $project_handles; $handles += $project_handles;
$workbook = new Spreadsheet_Excel_Writer(); $workbook = new PHPExcel();
$sheet = $workbook->addWorksheet('Exported Maniphest Tasks');
$date_format = $workbook->addFormat(); $sheet = $workbook->setActiveSheetIndex(0);
$date_format->setNumFormat('M/D/YYYY h:mm AM/PM'); $sheet->setTitle('Tasks');
$widths = array( $widths = array(
null,
20,
null, null,
15, 15,
20, null,
20, 10,
75, 15,
40, 15,
60,
30, 30,
400, 20,
100,
); );
foreach ($widths as $col => $width) { foreach ($widths as $col => $width) {
if ($width !== null) { if ($width !== null) {
$sheet->setColumn($col, $col, $width); $sheet->getColumnDimension($this->col($col))->setWidth($width);
} }
} }
$status_map = ManiphestTaskStatus::getTaskStatusMap(); $status_map = ManiphestTaskStatus::getTaskStatusMap();
$pri_map = ManiphestTaskPriority::getTaskPriorityMap(); $pri_map = ManiphestTaskPriority::getTaskPriorityMap();
$date_format = null;
$rows = array(); $rows = array();
$rows[] = array( $rows[] = array(
'ID', 'ID',
@ -128,21 +136,25 @@ final class ManiphestExportController extends ManiphestController {
'URI', 'URI',
'Description', 'Description',
); );
$formats = array(
null, $is_date = array(
null, false,
null, false,
null, false,
$date_format, false,
$date_format, true,
null, true,
null, false,
null, false,
null, false,
false,
); );
$header_format = $workbook->addFormat(); $header_format = array(
$header_format->setBold(); 'font' => array(
'bold' => true,
),
);
foreach ($tasks as $task) { foreach ($tasks as $task) {
$task_owner = null; $task_owner = null;
@ -166,28 +178,40 @@ final class ManiphestExportController extends ManiphestController {
$task->getTitle(), $task->getTitle(),
$projects, $projects,
PhabricatorEnv::getProductionURI('/T'.$task->getID()), PhabricatorEnv::getProductionURI('/T'.$task->getID()),
$task->getDescription(), phutil_utf8_shorten($task->getDescription(), 512),
); );
} }
foreach ($rows as $row => $cols) { foreach ($rows as $row => $cols) {
foreach ($cols as $col => $spec) { foreach ($cols as $col => $spec) {
$cell_name = $this->col($col).($row + 1);
$sheet->setCellValue($cell_name, $spec);
if ($row == 0) { if ($row == 0) {
$fmt = $header_format; $sheet->getStyle($cell_name)->applyFromArray($header_format);
} else { }
$fmt = $formats[$col];
if ($is_date[$col]) {
$code = PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2;
$sheet
->getStyle($cell_name)
->getNumberFormat()
->setFormatCode($code);
} }
$sheet->write($row, $col, $spec, $fmt);
} }
} }
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
ob_start(); ob_start();
$workbook->close(); $writer->save('php://output');
$data = ob_get_clean(); $data = ob_get_clean();
$mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
return id(new AphrontFileResponse()) return id(new AphrontFileResponse())
->setMimeType('application/vnd.ms-excel') ->setMimeType($mime)
->setDownload('maniphest_tasks_'.date('Ymd').'.xls') ->setDownload('maniphest_tasks_'.date('Ymd').'.xlsx')
->setContent($data); ->setContent($data);
} }
@ -198,4 +222,8 @@ final class ManiphestExportController extends ManiphestController {
return ($epoch + $offset) / $seconds_per_day; return ($epoch + $offset) / $seconds_per_day;
} }
private function col($n) {
return chr(ord('A') + $n);
}
} }